Error handling in a Case Statement

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!
 
B

Beetle

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
 
B

BruceM

I use this to trap a single error:

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

I like the freeware MZ Tools 3.0 utility for one-click error handling and
other useful stuff:
http://www.mztools.com/v3/mztools3.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top