Search code examples
loopsms-accessnullvbais-empty

Access Check for Blank, Null or Empty Fields


I have a form that has dozens of fields on it.

The fields are a combination of Text boxes and Combo boxes. I'm trying to figure out a single button solution to check for empty/blank/null fields.

If a blank is found, I would like it show a form; if none are found, I would like it close the current form.

My code is below.

It successfully loops through all the fields and shows a form when it finds a blank/empty/null field, but I can't figure out how to close the form if (and only if) there are no blank/empty/null fields on the form.

Private Sub Command146_Click()
    Dim ctl As Control

    With Me
        For Each ctl In .Controls
            If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
                If Len(ctl.Value & "") = 0 Then
                    DoCmd.OpenForm "PopMissingData"
                    Exit For
                End If ' Value
            End If ' ControlType
        Next
    End With
End Sub

Solution

  • Just check if the Control object has "run out":

    Private Sub Command146_Click()
    
        Dim ctl As Control
    
        With Me
            For Each ctl In .Controls
                If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
                    If Len(ctl.Value & "") = 0 Then
                        Exit For
                    End If ' Value
                End If ' ControlType
            Next
        End With
    
        If ctl Is Nothing Then
            ' All controls validated.
            DoCmd.Close acForm, Me.Name
        Else
            ' Open the other form.
            ' ctl will hold the non-validated control.
            DoCmd.OpenForm "PopMissingData"
        End If
    
    End Sub