Question : How can "duplicate key" errors be handled easily?

I would like to handle duplicate key errors when they appear while my users are keying data in to a form/subform.

In the main form, if a user tries to add an event which already exists, the long message "The changes you requested to the table were not successful because they would create duplicate values in the primary key ...".  This error appears as the user moves from the main form to the subform. I would like to catch this error and simplify the instructions for my users, ie "this event already exists, please check the date."  What is the easiest way to do this?

In the sub-form, the user has multiple fields with drop down boxes from which they choose participants in this event.  The field name is [IHC Event Participants Subform]![HOUND NAME].  When the user clicks the "Save" button,  the same duplicate key message appears as above.  I would like to catch and handle this error as well.

 I am not a VB user so if it is a VB solution please be kind with your instructions.  Thanks.

Answer : How can "duplicate key" errors be handled easily?

Hi shamrin,

The After Update event is not being triggered because no records are being updated.

Your best bet is to use the Form Error event handler as this will catch errors based on the entire form, not just individual controls on the form. See below:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
  Select Case DataErr
    Case 3022
      DoCmd.SetWarnings False
      Response = acDataErrContinue
      MsgBox "This event already exists.  Please change the date and/or event you have added. Msg 3022"
    Case Else
      Response = acDataErrContinue
      MsgBox "Other error occured." & Chr(13) & "Error: " & DataErr
  End Select
End Sub
Random Solutions  
 
programming4us programming4us