Loop Through Controls to Require User Entry

dgj32784

Registered User.
Local time
Today, 10:46
Joined
Mar 22, 2011
Messages
21
This should be easy but I've tried several versions of a For Each statement and have had no success. The following code works, but I'd rather loop through the controls without naming them and have the code do the same thing. Basically, if any of the controls are null, the On Click to pull up the next form is not carried out. However, if all controls have values, close the existing form and pull up the next.

Code:
Private Sub cmdGoToLoggerForm_Click()
 
    If IsNull(Me.cboCountyName) Then
        MsgBox "You must enter information into all fields before continuing."
    ElseIf IsNull(Me.cboPopulatedPlaces) Then
        MsgBox "You must enter information into all fields before continuing."
    ElseIf IsNull(Me.cboRoadName) Then
        MsgBox "You must enter information into all fields before continuing."
    ElseIf IsNull(Me.cboRiverBasin) Then
        MsgBox "You must enter information into all fields before continuing."
    ElseIf IsNull(Me.intEstimatedAcres) Then
        MsgBox "You must enter information into all fields before continuing."
    Else
    'Close existing form and open Logger Main Form
    DoCmd.Close
    DoCmd.OpenForm "frmGeneralSiteInfoLogger"
    End If
End Sub

Any assistance would be appreciated.

-David
 
This type of thing, perhaps using the Tag property of controls to limit which are tested:
Code:
  Dim ctl As Control

  For Each ctl In Me.Controls  
    'your test code here
  Next ctl
 
Thanks Paul. That was the general syntax I've been trying. The following code looks at each control and pops up the message box as many times as there are controls on the form. How do I get it to basically loop through and if any are null have the message box display once?

Code:
Private Sub cmdGoToLoggerForm_Click()
 
    Dim ctl As Control
 
    For Each ctl In Me.Controls
        If IsNull(ctl) Then
        MsgBox "You must enter information into all fields before continuing."
        End If
    Next ctl
 
    'Close existing form and open Logger Main Form
    DoCmd.Close
    DoCmd.OpenForm "frmLoggerCertificationQuestion"
 
End Sub
 
Try this
Code:
        If is null(ctl.value) or ctl.value = "" or ctl.value = null Then
        MsgBox "You must enter information into all fields before continuing."
        End If
 
To have it stop when it finds one, add

Exit Sub

after the message box. If you have an error handler, you'd go to the exit handler.
 

Users who are viewing this thread

Back
Top Bottom