Total Visual SourceBook

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office 2007 and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

Separate version for Access/Office 2003, 2002, and 2000.

Separate version for:
Access 97/95


View all FMS products for Microsoft AccessAll Our Microsoft Access Products

SourceBook Info:

Why SourceBook?

 

"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

 

 

 

Microsoft Access, Office/VBA, VB6 Modules royalty-free source code libraryMicrosoft Access Modules royalty-free source code libraryMicrosoft Access VBA and VB6 Module Library Free trial of Total Visual SourceBook for Microsoft Access, Office, VB6 and VBA

Class: ValidateControl in Category Microsoft Visual Basic 6 : Controls from Total Visual SourceBook

Class that provides the ability to perform validation on controls in their LostFocus events in VB6.

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 List

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 Code for Using Class: ValidateControl

' 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

Microsoft Access Module LibraryOverview of Total Visual SourceBook

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.

Additional Resources