• Error trapping

    Author
    Topic
    #463811

    I have a main form and a subform. I want to trap the error that pops up if there is a duplicate in the primary key of the main form (I believe it is error 3022 from my research correct me if I am wrong) and give a more user friendly message box. One of the events I need to trap it in is when you move from the main form to the subform. I tried putting the code into the Lost Focus event of the main form as well as On Enter in the subform and neither of those is trapping the Access error and giving me the custom message. Any pointers to the right event to get this error trapping into would be appreciated.

    Viewing 0 reply threads
    Author
    Replies
    • #1185170

      Try the On Error event of the main form. The event procedure has two arguments: DataErr is the error number, and Response is an argument that you can change to tell Access how to react.

      The default value for Response is acDataErrDisplay, meaning that Access will display the built-in error message. If you set Response to acDataErrContinue, the built-in error message will be suppressed.

      Code could look like this:

      Code:
      Private Sub Form_Error(DataErr As Integer, Response As Integer)
       Select Case DataErr
       Case 3022
        MsgBox "You tried to add a record with a value for the primary key that has already been used.", vbCritical
        Response = acDataErrContinue
       Case Else
        ' Let Access handle it
       End Select
      End Sub
    Viewing 0 reply threads
    Reply To: Error trapping

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

    Your information: