Ask Me Help Desk

Ask Me Help Desk (https://www.askmehelpdesk.com/forum.php)
-   Access (https://www.askmehelpdesk.com/forumdisplay.php?f=441)
-   -   Access VBA IF Statement Multiple Criteria (https://www.askmehelpdesk.com/showthread.php?t=568340)

  • Apr 6, 2011, 12:44 AM
    myoungii
    Access VBA IF Statement Multiple Criteria
    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.

    Code:

    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?
  • Apr 6, 2011, 03:38 AM
    ScottGem

    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:
    Code:

    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:
    Code:

    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.

  • All times are GMT -7. The time now is 02:59 AM.