Results 1 to 7 of 7
  1. #1
    3 Star Lounger
    Join Date
    Mar 2006
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    289
    Thanks
    0
    Thanked 1 Time in 1 Post

    Error handling (2003 sp2)

    I use the Click event of a label on a form to execute a parameter query. If I choose cancel in the parameter dialog box I get error 2001, You canceled the previous operation. How can I capture this error so it does not display? I tried a Select Case statement in various places to no avail.

    Private Sub Label10_Click()
    Dim strMessage As String
    Dim DataErr As Integer, Response As Integer
    Dim bytChoice As Byte
    On Error GoTo Label10_Click_Error

    DoCmd.SetWarnings False
    strMessage = "If the first step did not result in a report of sold assets, " _
    & "this step may be skipped! Select Cancel" & vbCrLf & vbCrLf _
    & " " & "Else, choose OK to proceed to remove the sold Asset."
    bytChoice = MsgBox(strMessage, vbOKCancel + vbExclamation, "Please Read")
    If bytChoice = vbCancel Then
    DoCmd.CancelEvent
    ElseIf bytChoice = vbOK Then
    DoCmd.OpenQuery "qry_REMOVE_ASSET_FROM_PRECHECK_TO_AO"
    DoCmd.SetWarnings True
    End If
    Label10_Click_Error:
    'Exit Sub
    Select Case DataErr
    Case 2001
    Exit Sub
    Case Else
    Response = acDataErrDisplay
    End Select
    MsgBox "Error " & Err.Number
    End Sub
    Thanks
    chuck

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error handling (2003 sp2)

    I'd do it like this:

    Private Sub Label10_Click()
    Dim strMessage As String
    Dim DataErr As Integer, Response As Integer
    Dim bytChoice As Byte
    On Error GoTo Label10_Click_Error

    ...

    Label10_Click_Exit:
    DoCmd.SetWarnings True
    Exit Sub

    Label10_Click_Error:
    Select Case DataErr
    Case 2001
    ' Do nothing
    Case Else
    MsgBox "Error " & Err.Number
    End Select
    Resume Label10_Click_Exit
    End Sub

    The essential bit is that the message box is displayed in the Case Else part. I removed the line

    Response = acDataErrDisplay

    because that only applies to some very specific events, such as the On Not In List event of a combo box and the On Error event of a form, but not to the On Click event of a label.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2006
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    289
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Error handling (2003 sp2)

    I don't understand why you exit the click event. How does Exit differ from End?
    Thanks
    chuck

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error handling (2003 sp2)

    You don't want to execute the error handling part if no error has occurred. The line Exit Sub takes care of that.

    Exit Sub lets you leave a procedure before the end of the source code. End Sub denotes the end of the source code for the procedure.

  5. #5
    3 Star Lounger
    Join Date
    Mar 2006
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    289
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Error handling (2003 sp2)

    hmm...That should have been obvious. Thank You.

    Unfortunately, there is still a querk in the works. The message still displays. When I step through I expect to jump over Case Else but it does not. Any suggestions?
    Thanks
    chuck

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Error handling (2003 sp2)

    Sorry, should have seen that. Change

    Case DataErr

    to

    Case Err

    Like Response, DataErr is only applicable to some events; Err is the general error object.

  7. #7
    3 Star Lounger
    Join Date
    Mar 2006
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    289
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Error handling (2003 sp2)

    That did the the trick!
    Thanks
    chuck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •