Query returns Duplicate Key error

  • Thread starter Thread starter padmajakrishnan
  • Start date Start date
P

padmajakrishnan

I created a form with some fields. On clicking Save button on the form,
the data from the fields will be appended to a table, which has a
Unique Key called "Code No". If I enter a duplicate Code in the form
and try to save, MS access throws an error (which is as expected). Is
there a way to capture the error code returned while executing the
query, to customize the error message displayed to the user?

Thanks in advance!
 
Check out the Error property of the form. If you create an event procedure
there, you can display the error code like this:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
msgbox DataErr
End Sub

You can also use this sub to display a friendly error message based on the
error code.

Duane Hookom created an Add-In for Access97 called the BR Code Stuffer that
has as part of it a general form-level error trapping module. Works quite
well. You can find it here:
http://www.rogersaccesslibrary.com/OtherLibraries.asp#Hookom,Duane

--
--Roger Carlson
MS Access MVP
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
You can trap for a specif error in your error handler code. If you need
further info on how to do error handling, see VBA Help for On Error
Specifically, you can do this in the error handler:

If Err.Number = 3022 Then
MsgBox "This Value is Already in the Database"
Else
....
 
Back
Top