• Error handling (2003 sp2)

    Author
    Topic
    #448465

    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

    Viewing 0 reply threads
    Author
    Replies
    • #1096681

      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.

      • #1096687

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

        • #1096689

          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.

          • #1096691

            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?

            • #1096698

              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.

            • #1096703

              That did the the trick!

    Viewing 0 reply threads
    Reply To: Error handling (2003 sp2)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: