Multiple IF functions with button click

dlackey74

New member
Local time
Today, 12:56
Joined
Dec 31, 2012
Messages
7
Hi all!

Need a little help with a problem concerning multiple IF functions in a form. I have a button that on click will print a report if certain fields in the form have data in them. If not, a msgbox pops up to let the user know that data must be filled and then sets focus on whichever field needs data in the form. There are many IF statements, and they all work fine until I get to a certain one then the remaining IF does not continue.

Here is some code:

If IsNull([Alert]) Then
MsgBox "An alert time is required." _
, vbCritical, "Data Required"
Me.Text591.SetFocus
Exit Sub
Else
If IsNull([Enroute]) Then
MsgBox "An enroute time is required." & vbNewLine & "If you do not have an enroute time, enter four(4) dashes (-)." _
, vbCritical, "Data Required"
Me.Text593.SetFocus
Exit Sub
Else
If IsNull([On Scene]) Then
MsgBox "An on scene time is required." & vbNewLine & "If you do not have an on scene time, enter four(4) dashes (-)." _
, vbCritical, "Data Required"
Me.Text595.SetFocus
Exit Sub
Else
If IsNull([Available]) Then
MsgBox "An available time is required." _
, vbCritical, "Data Required"
Me.Text597.SetFocus
Exit Sub
Else
If IsNull([Event Address/Location]) Then
MsgBox "An event address/location is required." _
, vbCritical, "Data Required"
Me.Text599.SetFocus
Exit Sub
Else

The next IF argument will bring up the msgbox when there is no data, thats great. However, when the user fills in the field and continues with the button click to print the report, the button does nothing.

Here is the next IF, the one with the problem:

If IsNull([Combo608]) Then
If IsNull([Combo620]) Then
MsgBox "You must select either a planned event or an emergency event." _
, vbCritical, "Information"
Me.Combo608.SetFocus
Exit Sub
Else

Any ideas on how to get it to work, to continue with the remaining IF arguments after this one (there are several more)?

Or are there a better way to code this and not have the many IF arguments?
 
How about simplifying and only displaying ONE message instead of making the user try once, they go back and fill in, try again, go back and fill in the next. Also, the

If Len(Me.ControlName & vbNullstring) = 0 Then

is better than

If IsNull(Me.ControlName) Then

because some controls can have an empty string and not be null so it would not catch it. The Len one takes care of both Nulls and Empty Strings.

Code:
    If Len(Me.[Alert] & vbNullString) = 0 Then
        strMsg = "Alert Time" & vbCrLf
    End If
 
    If Len(Me.[Enroute] & vbNullString) = 0 Then
        strMsg = strMsg & "Enroute Time" & vbCrLf
    End If
 
    If Len(Me.[On Scene] & vbCrLf) = 0 Then
        strMsg = strMsg & "On Scene Time" & vbCrLf
    End If
 
    If Len(Me.[Available] & vbCrLf) = 0 Then
        strMsg = strMsg & "Available Time" & vbCrLf
    End If
 
    If Len(Me.[Event Address/Location] & vbNullString) = 0 Then
        strMsg = strMsg & "Event Address/Location" & vbCrLf
    End If
 
    If Len(Me.[Combo608] & vbNullString) = 0 And Len(Me.[Combo620] & vbNullString) = 0 Then
        strMsg = strMsg & "Planned Event OR Emergency Event" & vbCrLf
    End If
 
    If strMsg <> vbNullString Then
        MsgBox "You are missing data.  Please fill in these:" & vbCrLf & strMsg, vbExclamation, "Entry Error"

    End If
 
Thanks for the reply and code. That seems to be a better option and helps simplify things. I will try your suggestion and see how it works.
 

Users who are viewing this thread

Back
Top Bottom