For information on the tag property and its uses, I recommend just searching and read what intrigues you. As for code samples, review what I use (in one form or another). Note that some people do not advocate looping through a form with many controls on it as they seem to fear a performance hit. I've used this on forms with appx. 100 controls and found that the time it takes to execute is virtually un-noticeable.
In a standard module:
Code:
Function HasNoData(vCheckVal As Variant) As Boolean
HasNoData = False
If IsNull(vCheckVal) Or vCheckVal = "" Then HasNoData = True
End Function
If all you want to do is terminate when validating a few controls, in command button code:
Code:
If HasNoData(Me!cmbMonth) Or HasNoData(Me!cmbYear) Or HasNoData(Me!txtJobID) Then
MsgBox "Need to supply Month, Year and JobID values to continue."
Exit Sub
End If
If you want to validate a bunch AND present a list in a message box, then in command button code:
Code:
Dim ctl as Control
Dim svList as String, svList2 as String
On Error GoTo errHandler
svList = "Please enter a value for: " & vbCrLf
svlist2 = ""
For Each ctl In Me.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox 'add to Case as required
If ctl.Tag = "Reqd" Then
If HasNoData(ctl) Then svList2 = svList2 & ctl.Controls.Item(0).Caption & vbCrLf
End If
End Select
nxtCtl:
Next
If svList2 <> "" Then
MsgBox svList & svList2, vbOKOnly, "Missing Information"
Exit Sub
End If
errHandler:
If Err.Number = 2467 Then
svList2 = svList2 & ctl.Name & vbCrLf
Resume nxtCtl
End If
- where labels are not attached to controls, the error handler gets the control name, which may be cryptic
- the Select Case block allows restriction of control types rather than handling an error where a control in the loop has no tag property
Probably not written perfectly, but it works.