Hi S,
How are your text boxes laid out on the form? Are they bound or unbound?
This is kinda' just for sport. It uses unbound textboxes laid out left to right, moving down the form. Since both fields aren't required, the code scans the form for all text box controls, loads them into a collection, evaluates one pair at a time (top two collection items) for Null, removes the first pair of items from the collection, and loops through the collection till all pairs of text boxes have been evaluated. Then displays a message like:
The following required entries were left blank:
txtA
txtB
Please fill in one of these values.
The following required entries were left blank:
txtC
txtD
Please fill in one of these values.
Code:
Option Compare Database
Private m_col As Collection
Private m_strMsg As String
Private Sub cmdOK_Click()
Dim ctl As Control
Dim i As Integer
Set m_col = New Collection
For Each ctl In Me.Controls
With ctl
If .ControlType = acTextBox Then
m_col.Add (.Name)
End If
End With
Next
Do Until m_col.Count = 0
ValidateControls
Loop
If Len(m_strMsg) <> 0 Then
MsgBox (m_strMsg)
m_strMsg = ""
End If
End Sub
Private Sub ValidateControls()
Dim strMsg As String
Dim intIndex As Integer
Call NullCheck(Me, m_col(1), m_col(2))
For intIndex = 1 To 2
m_col.Remove (1)
Next
End Sub
Public Sub NullCheck(frm As Form, _
ParamArray varListTextBoxes())
Dim intI As Integer
Dim ctl As Access.Control
Dim strMsg As String
Dim strFirstNullControl As String
Dim blnNullFound As Boolean
Dim blnMultipleNullsFound As Boolean
For intI = LBound(varListTextBoxes) _
To UBound(varListTextBoxes)
Set ctl = frm(varListTextBoxes(intI))
If ctl.ControlType = acTextBox Then
blnNullFound = IsNull(ctl)
End If
If blnNullFound Then
strMsg = strMsg & varListTextBoxes(intI) & vbCrLf
If Len(strFirstNullControl) = 0 Then
strFirstNullControl = varListTextBoxes(intI)
Else
'More than one null was found
blnMultipleNullsFound = True
End If
End If
Next intI
If blnMultipleNullsFound Then
'There was more than one null Control found.
m_strMsg = m_strMsg & "The following required entries were left blank:" _
& vbCrLf & vbCrLf & strMsg & vbCrLf _
& "Please fill in one of these values." & vbCrLf & vbCrLf
End If
End Sub
Thanks for the puzzle.
Bob