Results 1 to 6 of 6
  1. #1
    JennyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2017
    Posts
    51

    How to add an error handling code to Select Case statements?

    Hi

    I am trying to add an error handling code to Select Case, but not sure where to add. I have error handling for on click but don't seem it works on Select Case below. B

    Below is a short version of my Select Case VBA. I have case up to 30 options. Any recommendations on how to add the Error Handling? thanks,

    Private Sub Frame109_AfterUpdate()
    Dim strFilter As String
    Select Case Frame109

    Case 0
    Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = False
    Me.Refresh



    Case 1 'this is the radio button option number
    strFilter = "[Category] = 1" 'this is ID in the category table

    Me.Sfrm_CustomerFeeSchedule.Form.Filter = strFilter
    Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = True

    Case 2
    strFilter = "[Category] = 2"

    Me.Sfrm_CustomerFeeSchedule.Form.Filter = strFilter
    Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = True

    Case 3
    strFilter = "[Category] = 3"

    Me.Sfrm_CustomerFeeSchedule.Form.Filter = strFilter
    Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = True

    Case 4
    strFilter = "[Category] = 4"


    End Select
    End Sub

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,549
    carve out a block at the bottom to handle the errors.
    Code:
    Private Sub Frame109_AfterUpdate()
    Dim strFilter As String
    On Error GoTo ErrStop
    Select Case Frame109
    Case 0
        Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = False
        Me.Refresh
    Case 1 'this is the radio button option number
        strFilter = "[Category] = 1" 'this is ID in the category table
        
        Me.Sfrm_CustomerFeeSchedule.Form.Filter = strFilter
        Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = True
    Case 2
        strFilter = "[Category] = 2"
        
        Me.Sfrm_CustomerFeeSchedule.Form.Filter = strFilter
        Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = True
    Case 3
        strFilter = "[Category] = 3"
        
        Me.Sfrm_CustomerFeeSchedule.Form.Filter = strFilter
        Me.Sfrm_CustomerFeeSchedule.Form.FilterOn = True
    Case 4
        strFilter = "[Category] = 4"
    End Select
    Exit Sub
    
    ErrStop:
        'resumes go here
    if err = 13 then
       resume next
    else
    
    MsgBox Err.Description, , Err
    endif End Sub

  3. #3
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    I'm slightly puzzled what error handling you would need, as surely your case statement caters for all possible options in your option group?

    I'm not saying you shouldn't have any error handling, just I can't see how it would ever be called for this event.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,861
    couldnt you just use
    Code:
    strFilter = "[Category] = " & Frame109
    also with a select case statement you can use a "case else" to catch any unknown values

  5. #5
    JennyL is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2017
    Posts
    51
    Quote Originally Posted by moke123 View Post
    couldnt you just use
    Code:
    strFilter = "[Category] = " & Frame109
    also with a select case statement you can use a "case else" to catch any unknown values
    Does this mean radio option number equals to category number? I will to make sure the two numbers are equal in the future if I end up adding more radio buttons, correct? what will happen if I use "case else"? Will I get a error message?

    Thanks,

  6. #6
    Minty is offline VIP
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,144
    As Mokes off line - At the end of your Case statements

    Code:
    Case Else 
          MsgBox "You have managed to not choose a sensible option"
    End Select
    And yes if your option group numbers match then you just need to keep them in line with the filter conditions.

    To be honest 30 option buttons is a lot and if you maybe need to redesign to accommodate another potential choice, perhaps not the best route to go.
    Maybe a List box or combo that would accommodate any number of choices might be better?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

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

Similar Threads

  1. Replies: 1
    Last Post: 10-16-2016, 12:54 PM
  2. Replies: 1
    Last Post: 03-06-2015, 11:16 AM
  3. Debugging a Select Case Statements
    By dccjr in forum Access
    Replies: 4
    Last Post: 03-05-2013, 04:14 PM
  4. Debugging a Select Case Statements (Still)
    By dccjr in forum Programming
    Replies: 13
    Last Post: 02-28-2013, 09:47 PM
  5. Replies: 4
    Last Post: 01-05-2013, 04:32 PM

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