Here's a function that I wrote that will loop through the controls in a
form, check to see if the control should be validated, highlight fields
with a zero-length or Null value, provide a return value, and position
the cursor at the first control in the Tab sequence. To use the function...
1. Add the function to a module
2. In the .TAG property of the fields that are required, add the text
'required'
3. In the forms BeforeUpdate event add and the following statement
If ValidateCriteriaFields(Me.Name, "required") = False Then Cancel = True
I never used the function to validate a form (bizzare as that sounds) so
you may have to tweek the IF...THEN and its placement, but it should
work. Also, be certain that you DO NOT put any thing into the .TAG
property of items such as command buttons. The function does not check
the type of control being examined before looking at the value of the
control. Since command buttons do not have a value, the function will
crap out.
Function ValidateCriteriaFields(varFormName As Variant, strTagCriteria
As String)
'varFormName - Name of form on which the controls to be validated exists
'strTagCriteria - Value in the controls tag property to search for
Dim frm As Form
Dim flagFirstControl As Integer
Dim flagFirstControlTabIndex As Integer
Dim flagValidateCriteriaFields As Integer
On Error GoTo Err_ValidateCriteriaFields
ValidateCriteriaFields = True
flagValidateCriteriaFields = 0
flagFirstControlTabIndex = 999
Set frm = Forms(varFormName)
For I = 0 To (frm.Count - 1)
If frm(I).Tag Like "*" & strTagCriteria & "*" Then
If frm(I) = "" Or IsNull(frm(I)) = True Then
flagMissingInformation = 1
frm(I).BackColor = 16711680
frm(I).ForeColor = 16777215
flagValidateCriteriaFields = 1
If flagFirstControlTabIndex > frm(I).TabIndex Then
flagFirstControl = I
flagFirstControlTabIndex = frm(I).TabIndex
End If
End If
End If
Next I
If flagValidateCriteriaFields = 1 Then
frm(flagFirstControl).SetFocus
MsgTxt = "The highlighted fields must be completed in" & Chr$(10) &
Chr$(13)
MsgTxt = MsgTxt & "order to execute the indicated query."
MsgBox MsgTxt, 64
ValidateCriteriaFields = False
Else
ValidateCriteriaFields = True
End If
Exit_ValidateCriteriaFields:
Exit Function
Err_ValidateCriteriaFields:
ErrorMsg = "Validate Criteria Fields:" & Chr$(10) & Chr$(13) & Error$
MsgBox ErrorMsg, , "Validate Criteria Fields" & varFormName
Resume Exit_ValidateCriteriaFields
End Function