Multiple Required Fields Behind Button

OliverWilton

Registered User.
Local time
Today, 23:54
Joined
Nov 25, 2008
Messages
15
Microsoft have offered support upon giving the VBA code for a required field behind an exit command button for 1 box. I'm very new and would like it to check behind multiple boxes.

The code Offered was:

If IsNull(Me![Field1]) Then
If MsgBox("'Field1' must contain a value." & Chr(13) & Chr(10) & _
"Press 'OK' to return and enter a value." & Chr(13) & Chr(10) & _
"Press 'Cancel' to abort the record.", _
vbOKCancel, "A Required field is Null") = vbCancel Then
DoCmd.Close

Else
Me![Field1].SetFocus
End If
Else
DoCmd.Close
End If

Any help on having that for say 4-5 box checks? thank you!
 
I can get you started, perhaps, but it may need some debugging as I don't normally do this sort of thing.


Code:
Dim oControl As Variant
Dim txt As TextBox
For Each oControl In Array(Me.Controls("Field1"), Me.Controls("Field2"), Me.Controls("Field3"), Me.Controls("Field4"), Me.Controls("Field5"))
    Set txt = oControl
    If IsNull(txt.Value) Then
            If MsgBox("The field " & txt.Name & " must contain a value." & Chr(13) & Chr(10) & _
            "Press 'OK' to return and enter a value." & Chr(13) & Chr(10) & _
            "Press 'Cancel' to abort the record.", _
             vbOKCancel, "A Required field is Null") = vbCancel Then
                    DoCmd.Close
                    Exit Sub
            Else
                txt.SetFocus
                Exit Sub
            End If
    End If
Next oControl
DoCmd.Close
 
works an absolute treat, thank you :)
 

Users who are viewing this thread

Back
Top Bottom