Many people assume that it is easy to validate VB6 controls in the LostFocus event. Unfortunately, this can be quite a problem. Consider two text boxes: Text1 and Text2. Both have code in their LostFocus events to check for valid data enteries. When the user tabs out of Text1, the LostFocus event fires, so the programmer displays a message box and attempts to set the focus back onto Text1.
However, before going back to Text1, Text2 receives GotFocus and LostFocus events. Unless the value in Text2 is already correct, the validation code in Text2 fires, and the user is stuck in an endless loop. This class keeps a state variable to track whether a validation test is currently executing. If a validation routine for Text1 is being performed, Text2 will NOT fire its validation messages, even if the validation for Text2 fails.
Procedure Name | Type | Description |
(Declarations) | Declarations | Declarations and private variables for the CValidateControl class. |
Class_Initialize | Initialize | Initialize class. |
Validate | Method | Check truth value of validation test and handle focus events to avoid deadlock LostFocus code. This function is normally called in the LostFocus event of controls which you wish to modify. |
' Example of the CValidateControl class ' ' To try this example, do the following: ' 1. Create a new form ' 3. Add a text box called 'txtAA' ' 3. Add a text box called 'txtBB' ' 3. Add a text box called 'txtCC' ' 8. Paste all the code from this example to the new form's module ' 9. Run the form, test the validation routines ' Declarations section code Private mValidateControl As CValidateControl Private Sub Form_Load() Set mValidateControl = New CValidateControl End Sub Private Sub txtAA_LostFocus() ' Validate txtAA, with a test in-line mValidateControl.Validate txtAA, txtAA.Text = "AA", "Value must be AA" End Sub Private Sub txtBB_LostFocus() ' validate txtBB with a separate test Dim fTest As Boolean If txtBB.Text = "BB" Then fTest = True Else fTest = False End If mValidateControl.Validate txtBB, fTest, "Value must be BB" End Sub Private Sub txtCC_LostFocus() ' provide custom handling rather than the built-in message If mValidateControl.Validate(txtCC, txtCC.Text = "CC") = True Then MsgBox "It's gotta be CC" End If End Sub
The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.
Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!
Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!
"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."
Van T. Dinh, Microsoft MVP