Results 1 to 6 of 6
  1. #1
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376

    Case statement not working as Expected

    Hello all,

    I am trying to use a Case Statement which I haven't used in the past so I am not sure I am doing it correctly. I have the following code,

    Private Sub CmdAddToInventory_Click()On Error GoTo CmdAddToInventory_Click_Err
    Dim Part As String
    Part = CmbPNSelect.Column(1)

    Select Case CmbPNSelect()

    Case Part = "M26617"


    MsgBox "Found M26617"


    End Select

    I have a stop put in at "End Select" so I can see what "Part" is equal to and it is equal to "M26617" however I am not getting the message box as expected. Can someone tell me what I am missing or have incorrect? This is just a test at this point and once I get it working I want to put in a lot of other case possibilities.

    Thanks in advance

    Dave

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2016
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,530
    Quote Originally Posted by Dave14867 View Post
    Hello all,

    I am trying to use a Case Statement which I haven't used in the past so I am not sure I am doing it correctly. I have the following code,

    Private Sub CmdAddToInventory_Click()On Error GoTo CmdAddToInventory_Click_Err
    Dim Part As String
    Part = CmbPNSelect.Column(1)

    Select Case CmbPNSelect()

    Case Part = "M26617"
    MsgBox "Found M26617"


    End Select

    I have a stop put in at "End Select" so I can see what "Part" is equal to and it is equal to "M26617" however I am not getting the message box as expected. Can someone tell me what I am missing or have incorrect? This is just a test at this point and once I get it working I want to put in a lot of other case possibilities.

    Thanks in advance

    Dave
    Dave
    When posting code please use the code tags and indentation.
    You are not showing the complete code.
    In the following code I have changed the name of your variable and added what I think should be the rest of the code that is missing.
    Code:
    Private Sub CmdAddToInventory_Click()
    
    On Error GoTo CmdAddToInventory_Click_Error
    
    
    Dim strPart As String
    
    
        strPart = CmbPNSelect.Column(1)
        
        Select Case CmbPNSelect()
        
            Case strPart = "M26617"
                MsgBox "Found M26617"
            
        
        End Select
    
    
        
        
    CmdAddToInventory_Click_Exit:
        Exit Sub
    
    
    CmdAddToInventory_Click_Error:
    
    
        MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Form: " & Me.Name & " in procedure CmdAddToInventory_Click, line " & Erl & "."
        Resume CmdAddToInventory_Click_Exit:
    
    
    
    
    End Sub
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    This way.

    Code:
    Private Sub CmdAddToInventory_Click()
    	On Error GoTo CmdAddToInventory_Click_Err
    	Dim Part As String
    	Part = CmbPNSelect.Column(1)
    
    
    	Select Case Part
    		Case "M26617"
    		MsgBox "Found M26617"
    	End Select
    End Sub

  4. #4
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    Bob,
    You are correct as usual, I didn't put in the code at the end, my mistake, I had the extra code at the end of the sub as you put in already, I was just trying to get the message box section to work to show "Found M26617" but it doesn't come up, not sure why. the Value of strPart is indeed "M26617" (actually M26617 in the combo box). I changed Part to strPart as you suggested but it still isn't working.

  5. #5
    Dave14867's Avatar
    Dave14867 is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Nov 2016
    Location
    Upstate NY
    Posts
    376
    Bob,

    I found what I was doing incorrectly after reading davegri post which wasn't seen until I responded to your first one. All is good now thanks to both of you for your assistance.

    Dave

  6. #6
    davegri's Avatar
    davegri is offline Excess Access
    Windows 10 Access 2016
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,388
    Dave

    Learn Something new everyday. A day in which you learn nothing is a wasted day.
    Can't argue with that. Glad to help. Good luck with the rest of the project.

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

Similar Threads

  1. Compile Error : Expected End of Statement
    By Shamli in forum Queries
    Replies: 12
    Last Post: 08-20-2018, 12:47 PM
  2. Not Like statement not working as expected
    By nick404 in forum Queries
    Replies: 4
    Last Post: 07-20-2015, 03:02 PM
  3. Compile error: Expected end of statement
    By ritati in forum Macros
    Replies: 1
    Last Post: 12-05-2014, 05:33 AM
  4. Expected End of Statement error
    By SamNotSoWise in forum Programming
    Replies: 5
    Last Post: 06-25-2012, 04:41 PM
  5. compile error: expected end of statement
    By RedGoneWILD in forum Programming
    Replies: 5
    Last Post: 07-29-2010, 10:12 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