Ask Experts Questions for FREE Help !
Ask
    myoungii's Avatar
    myoungii Posts: 12, Reputation: 1
    New Member
     
    #1

    Apr 6, 2011, 12:44 AM
    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?
    ScottGem's Avatar
    ScottGem Posts: 64,966, Reputation: 6056
    Computer Expert and Renaissance Man
     
    #2

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

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

VBA Code for creating multiple worksheets [ 11 Answers ]

Hey guys - Ok, so I found some code on the Internet that I have been trying to tweak to my liking but I'm having some trouble. Here's what I am trying to do. I want to create a worksheet for each day of a given month and name the worksheet for each respective day. The naming convention...

Represh Report with vba in access [ 3 Answers ]

Hi Guy's, How do I refresh a report with vba in access me.refresh does not work. My problem is I'm opening a report in acViewNormal and the report is longer than the page but I don't get a scroll bar unless I manually press refresh all (access2007). Thanks Joe

VBA Access Hide Subform, Box [ 1 Answers ]

Hello all, I'm trying to create an application using access VBA. Unfortunately, I'm a novice programming in VBA. I'd like to know the VBA code to hide and show a subform or a box created in a form. Thanks Sean

Counting blank cells with multiple criteria [ 1 Answers ]

Hello, I need some help with counting blank cells. Here's my data: Name Feb 07 Roger x Lisa Bob x Lisa Lisa x Lisa


View more questions Search