Error handling in a Case Statement

  • Thread starter Thread starter Rod
  • Start date Start date
R

Rod

I have a case statment:

Select Case optgrpFrm
Case Is = 0
Me.RecordSource = "qCalling"
Me.optCalling.ForeColor = 32768 '(green - pressed)
Me.grpScripts = 1
Case Is = 1
Me.RecordSource = "qLookup"
Me.optLookup.ForeColor = 32768
Me.grpScripts = 2
Case Is = 2
...
End Select

If the user hits escape when the query window is asking him for the name &
number prompted by qLookup an error comes up:

Run-time error '2001': You canceled the previous operation.
At this point the user can end, debug, or help.

If the user presses escape I would like the operation to just halt and
return to whatever was being done. How do I do this within the above code?

Thanks for your help!
 
Generally speaking, you should have error handling in all your
procedures so you can trap for certain errors and tell your app
how to deal with errors. Your complete sub with error handling
might look like;

Private Sub optgrpFrm_AfterUpdate()
On Error GoTo HandleError

Select Case optgrpFrm
Case Is = 0
Me.RecordSource = "qCalling"
Me.optCalling.ForeColor = 32768 '(green - pressed)
Me.grpScripts = 1
Case Is = 1
Me.RecordSource = "qLookup"
Me.optLookup.ForeColor = 32768
Me.grpScripts = 2
Case Is = 2
'etc
End Select

ExitHere:
Exit Sub

HandleError:
If Err.Number = 2001 Then
Resume ExitHere
Else
MsgBox Err.Number & "(" & Err.Description & ")"
Resume ExitHere
End If

End Sub
 
Back
Top