Microsoft KB Archive/164358

= PRB: Handling Violation of Field Rules in Forms =

Article ID: 164358

Article Last Modified on 10/15/2003

-

APPLIES TO


 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q164358



SYMPTOMS
If the REVERT or HELP buttons are not desirable in the dialog box that Visual FoxPro 5.0 presents for violations of Field Rules, it is possible to trap the error and present a custom dialog box. These errors were not trappable in either Visual FoxPro 3.0 or 3.0b.



RESOLUTION
There are basically three potential workarounds for this situation:

The first can be with or without the use of classes. Using classes, however, is recommended and the implementation below allows you to use the workaround on a form not using any custom classes.

 Create a custom Form Property called lFlagError.  In the Error Method of the Form, add the following code: IF nError = 1582 && Field "name" validation rule is violated THIS.lFlagError = .T.     ENDIF   In the LOSTFOCUS of any TextBox that is based on a field with a field rule, add the following code: IF THISFORM.lFlagError NODEFAULT && Do not move Focus to Next Object THISFORM.lFlagError = .F.     ENDIF 

The second option involves modifying the Field Rule. Instead of having a field rule that is merely an expression or a function that checks the condition and then returns a True or False, change the expression into a Function (or modify the existing function) so that it checks for the desired condition. In addition, if the rule fails, REPLACE the field with a valid value or the DEFAULT VALUE, as in the following example: Current Rule: .NOT. EMPTY

Modified Rule*: MyCustomRule *Best placed in Store Procedures of the Database container

FUNCTION MyCustomRule IF EMPTY REPLACE WITH ; EVAL(DBGETPROP('<alias.fieldname','FIELD','DEFAULTVALUE')) ENDIF The third option makes use of a Timer. The SETFOCUS of the object that caused the Rule violation cannot be called from within the Form's ERROR method due to the Visual FoxPro Object Model. At the time of the error the current object is still the object that caused the error and its LOSTFOCUS event has not yet occurred; therefore, when the ERROR method code is finished, the object's LOSTFOCUS occurs, moving it to the next object in the tab order, negating any SETFOCUS command in the ERROR method of the Form. The Timer allows the Object Model to progress through normally and then move the focus back to the object that caused the rule violation. Use the following steps to implement this workaround:

 From the File menu, click New, and then click Class. From the "Based On" combo box click Timer (for a name, use ErrTimer). Choose a Class library to store the class in and click OK.</li> From the Class menu, click Property and name it - oObjRef.</li>  In the Timer Method, enter the following code: && Turn the Timer back off THIS.INTERVAL = 0

&& Tell the user there was an error =MESSAGEBOX(MESSAGE,48)

&& Put the user back in the control THIS.oObjRef.SETFOCUS THIS.oObjRef=.NULL. </li> Create a form.</li>  Add the following code to the form's ERROR method: && Check and see if the error was due to Rule Validation IF nError = 1582

&& Store an object reference of the Current Control to the && custom property oObjRef THIS.Errtimer1.oObjRef = THIS.ACTIVECONTROL

&& The interval can be critical, setting it to low and timing && problems occur, such as the Timer's custom property && not getting set. THIS.Errtimer1.INTERVAL = 100 ENDIF </li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce Behavior
  Issue the following commands in the Command window: CREATE DATABASE beakrul CREATE TABLE tTemp ; (cFld1 c(3) CHECK .NOT. EMPTY(cFld1) ERROR 'Cannot be empty!') CREATE FORM breakrul </li> Set the Form's BUFFERMODE to "2 - Optimistic."</li> Right-click on the form and click "Data Environment."</li> Right-click in the DataEnvironment Window and click "Add Table."</li> Select the above table.</li> Click Close to close the "Add Table or View" dialog box.</li> Click on a field in the Data Environment window and drag it onto the form.</li> Add a CommandButton to the Form.</li> Double-Click on the CommandButton.</li>  In the CommandButton's CLICK method add the following code: APPEND BLANK THISFORM.REFRESH </li> <li>Save and run the form.</li> <li>Click on the CommandButton.</li> <li>Tab to the TextBox.</li> <li>Type anything in the TextBox and hit Enter</li> <li>Tab back to the TextBox.</li> <li>Delete the contents of the TextBox and press Enter. The Field Validation dialog box appears.</li></ol>

<div class="references_section">