Results 1 to 3 of 3

Thread: Error trapping

  1. #1
    4 Star Lounger
    Join Date
    Dec 2002
    Location
    Kitchener, Ontario, Canada
    Posts
    421
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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

  3. #3
    4 Star Lounger
    Join Date
    Dec 2002
    Location
    Kitchener, Ontario, Canada
    Posts
    421
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Hans, that is exactly what I needed.

Posting Permissions

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