PDA

View Full Version : Access VBA IF Statement Multiple Criteria


myoungii
Apr 6, 2011, 12:44 AM
I am attempting to create an if statement with multiple variables. I have a form that user will fill out but I do not want them to be able to save any information if any number of the three controls are left blank.


Private Sub cmdSave_Click()


On Error GoTo HandleError
If me.txtdate.value <> "" And Me.cboShift.Value <> "" And me.cboSection.value <> = "" Then

intResponse = MsgBox("Are you sure you want to save the shift summary information " & Me.cboShift And "shift - " & me.cboSection & " for " & Me.txtDate & ".", vbYesNo)


Exit Sub

Else


'Call CheckMaintanceSchedule


If lngShiftSummary = 0 then



Call InsertShiftSummary


Else



Call UpdateShiftSummary



Call Form_Load



End If


End If
Else

MsgBox "A date, shift, and section must be provided in order to save the shift summary information on the form. Please provide this information.", vbOKOnly

Me.txtDate.SetFocus
End If

HandleError:

If Err.Number <> 0 Then


GeneralErrorHandler Err.Number, Err.Description, SHIFT_SUMMARY_DETAILS_FORM, "cmdSave_Click"


Exit Sub
End If

End Sub


The code appears to runs through the IF statement fine but then skips the "intResponse line and immediately jumps to the Handle error code. Does anyone have any suggestions?

ScottGem
Apr 6, 2011, 03:38 AM
Well there are three problems. First, your Error handling code should produce an message telling you what the problem is. Somewhere youy need a like like this:
MsgBox Err.Number & "-" & Err.Description

But the problem appears to be in your building of the text string for the MsgBox function. You have:


intResponse = MsgBox("Are you sure you want to save the shift summary information " & Me.cboShift And "shift - " & me.cboSection & " for " & Me.txtDate & ".", vbYesNo)

It should be:

intResponse = MsgBox("Are you sure you want to save the shift summary information " & Me.cboShift & " shift - " & me.cboSection & " for " & Me.txtDate & ".", vbYesNo)
You typed the word AND instead of an &.

The third problem is a minor point. You don't need the intReponse=. You are not doing anything with the user response to the MsgBox so you can use the Msg Box statement just replace the parentheses with spaces.