Results 1 to 6 of 6
  1. #1
    theperson is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    28

    If ElseIf Not Working

    Here is my code:



    Code:
    Private Sub Command38_Click()On Error GoTo Err_Command38_Click
    
    
    If InStr(1, Me.[Machine#], "-1") > 0 Or InStr(1, Me.[Machine#], "-6") > 0 Or InStr(1, Me.[Machine#], "-7") > 0 Or InStr(1, Me.[Machine#], "-8") > 0 Or InStr(1, Me.[Machine#], "-9") > 0 Or InStr(1, Me.[Machine#], "-13") > 0 Then
        
        Dim stDocName As String
    
    
        stDocName = "Process Info Main"
        DoCmd.OpenReport stDocName, acNormal
    
    
    ElseIf InStr(1, Me.[Machine#], "-2") > 0 Or InStr(1, Me.[Machine#], "-3") > 0 Or InStr(1, Me.[Machine#], "-4") > 0 Or InStr(1, Me.[Machine#], "-5") > 0 Or InStr(1, Me.[Machine#], "-11") > 0 Or InStr(1, Me.[Machine#], "-12") > 0 Or InStr(1, Me.[Machine#], "-14") > 0 Or InStr(1, Me.[Machine#], "-15") > 0 Or InStr(1, Me.[Machine#], "-16") > 0 Or InStr(1, Me.[Machine#], "-17") > 0 Or InStr(1, Me.[Machine#], "-18") > 0 Or InStr(1, Me.[Machine#], "-19") > 0 Then
        
        stDocName = "rptPrintMasterSetUpSheet"
        DoCmd.OpenReport stDocName, acNormal
        
    Exit_Command38_Click:
        Exit Sub
    
    
    Err_Command38_Click:
        MsgBox Err.Description
        Resume Exit_Command38_Click
    End If
    End Sub
    It is used on a print button on a form. It used to work when I didn't have to be so specific with the numbers (-16, -17, etc) but now I had to put in the ElseIf instead of just the Else it used to be and when a user clicks the print button, nothing happens. In the code editor the macro button keeps popping up when I click Run Code. Here is my old working code:

    Code:
    Private Sub Command38_Click()
    On Error GoTo Err_Command38_Click
    
    
     If InStr(1, Me.[ID#], "55.1") > 0 Or InStr(1, Me.[ID#], "110.1") > 0 Or InStr(1, Me.[ID#], "110.2") > 0 Or InStr(1, Me.[ID#], "110.3") > 0 Or InStr(1, Me.[ID#], "110.4") > 0 Or InStr(1, Me.[ID#], "165.1") > 0 Then
        
        Dim stDocName As String
    
    
        stDocName = "Process Info Main"
        DoCmd.OpenReport stDocName, acNormal
        
    Else
        
        stDocName = "rptPrintMasterSetUpSheet"
        DoCmd.OpenReport stDocName, acNormal
        
    Exit_Command38_Click:
        Exit Sub
    
    
    Err_Command38_Click:
        MsgBox Err.Description
        Resume Exit_Command38_Click
    End If
    End Sub
    What is my problem?!

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Move the Dim statement outside the If Then Else.

    The Exit_ and Err_ code should be outside the If Then Else.

    Can put the DoCmd.OpenReport outside the If Then Else.

    I don't think the code for -14, -15, -16, -17, -18, -19 will ever run because the -1 criteria is met first. You need to switch around the criteria.

    The macro dialog opens because the procedure is behind a form.

    Step debug. Refer to link at bottom of my post for guidance on debug techniques.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    theperson is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    28
    New code looks like this:

    Code:
    Private Sub Command38_Click()On Error GoTo Err_Command38_Click
    Dim stDocName As String
    
    
    If InStr(1, Me.[Machine#], "-13") > 0 Or InStr(1, Me.[Machine#], "-6") > 0 Or InStr(1, Me.[Machine#], "-7") > 0 Or InStr(1, Me.[Machine#], "-8") > 0 Or InStr(1, Me.[Machine#], "-9") > 0 Or InStr(1, Me.[Machine#], "test") > 0 Then
    
        stDocName = "Process Info Main"
        DoCmd.OpenReport stDocName, acNormal
    
    ElseIf InStr(1, Me.[Machine#], "-2") > 0 Or InStr(1, Me.[Machine#], "-3") > 0 Or InStr(1, Me.[Machine#], "-4") > 0 Or InStr(1, Me.[Machine#], "-5") > 0 Or InStr(1, Me.[Machine#], "-11") > 0 Or InStr(1, Me.[Machine#], "-12") > 0 Or InStr(1, Me.[Machine#], "-14") > 0 Or InStr(1, Me.[Machine#], "-15") > 0 Or InStr(1, Me.[Machine#], "-16") > 0 Or InStr(1, Me.[Machine#], "-17") > 0 Or InStr(1, Me.[Machine#], "-18") > 0 Or InStr(1, Me.[Machine#], "-19") > 0 Then
        
        stDocName = "rptPrintMasterSetUpSheet"
        DoCmd.OpenReport stDocName, acNormal
        
    End If
    
    Exit_Command38_Click:
        Exit Sub
    
    Err_Command38_Click:
        MsgBox Err.Description
        Resume Exit_Command38_Click
    
    End Sub
    It still does nothing when I click the button. If I moved that report command outside the ifthenelse statement, it gave me an error that it needs a report argument.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,926
    Then it is doing something, just not what you expect. So none of the criteria are being met and need to find out why. Have you step debugged? What is the value of Machine#?

    Advise not to use spaces or special characters/punctuation (underscore is exception) in names.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    In the code you have to move the On Error statement

    On Error GoTo Err_Command38_Click

    below the Sub Header; the above, with it on the same line, may be because of your copying and pasting the code here.

    The 'nothing happening' could be the result, as June7 said, of the [Machine#], for the Current Record, not having any of the Values (-14, -15, etc.) that you have in the IF...ElseIf construct; you've made no allowance for anything else. Try adding an Else Clause just before the End If, such as

    Code:
    Else
     Msgbox "No Machine Match Found!!!"
    End If


    and see what happens.

    BTW, it is imperative that all Dim Statements appear at the top of a Sub, above everything except the OnError GoTo statement! Don't really know why, but I have seen placement elsewhere in a Sub, such as inside of a If...Else construct, cause the Access Gnomes to have hissy fits!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  6. #6
    theperson is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2012
    Posts
    28
    Adding that no machine found statement in does return that error machine. I'm not sure why though, it must have something to do with me switching to "-#" instead of "55.1" like I was doing before. I type in '10138-2' in the field and it doesn't work. But if I changed it back to '10138-55.1' and changed the code back to trying to find "55.1", that will work just fine. It must be something to do with trying to find "-2".

    If I try to search for "10138" specifically in the code, it still returns no machine found.

    EDIT: Nevermind, I found the issue. I've been using [Machine#] instead of [ID#] for this certain button. But you guys helped fix another inevitable issue with the "-1". Thanks!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-29-2013, 04:34 AM
  2. Top 3 is not working
    By jyellis in forum Queries
    Replies: 7
    Last Post: 10-04-2012, 12:29 PM
  3. Help with ElseIf Statements
    By dshillington in forum Programming
    Replies: 6
    Last Post: 12-16-2011, 03:32 PM
  4. Easy one, If then ElseIf
    By Bruce in forum Forms
    Replies: 4
    Last Post: 12-01-2011, 12:44 PM
  5. Working with PDF's
    By Mitch87 in forum Access
    Replies: 1
    Last Post: 02-19-2010, 11:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums