If IsNull Then fo multiple controls (1 Viewer)

Prysson

Registered User.
Local time
Today, 10:20
Joined
Sep 23, 2002
Messages
45
I need to check for IsNull on four of the five controls on a form.

If there was only one control I would just write the code at the start of the procedure to

If IsNull Then
MsgBox "You must yadda yadda yadda", vbOKOnly + vbCritical, "Error"
Else
REST OF PROCEDURE


However the problem I run into is building a sequence of those If IsNull Then's

I need to check four of the five controls for a null value (but specifically not the fifth because it very well MIGHT have a null value)

Then I need the error messages to be control specific.

Any one able to give me a bit of sample code as to how this might be accomplished.
 

Drevlin

Data Demon
Local time
Today, 10:20
Joined
Jul 16, 2002
Messages
135
There is no need for nested if statements in this case. Just do a check for each of your Controls one after the other:

If IsNull(cntrl1) Then
MsgBox "You must yadda yadda yadda", vbOKOnly + vbCritical, "Error"
cntrl1.SetFocus
End
End If

If IsNull(cntrl2) Then
MsgBox "You must yadda yadda yadda", vbOKOnly + vbCritical, "Error"
cntrl2.SetFocus
End
End If

If IsNull(cntrl3) Then
MsgBox "You must yadda yadda yadda", vbOKOnly + vbCritical, "Error"
cntrl3.SetFocus
End
End If

so on and so fourth.
 

dgm

Registered User.
Local time
Today, 19:20
Joined
Sep 5, 2002
Messages
146
Code:
Private Sub cmdtest_Click()
    Dim intErr As Integer
    intErr = 0
    intErr = testCtls(txtSample1)
    intErr = testCtls(txtSample2)
    intErr = testCtls(txtSample3)
    intErr = testCtls(txtSample4)
    If intErr = 0 Then
        'perform some stuff
    End If
End Sub

Public Function testCtls(myCtl As Control) As Integer
    If IsNull(myCtl) Then
        MsgBox myCtl.Tag, vbCritical + vbOKOnly, "Error"
        testCtls = 1
    End If
End Function
Just add a custom message to each of the controls tag property.

Dave
 

Users who are viewing this thread

Top Bottom