Question : MS Access - how to trap SQL primary key violation error?

I have an Access 2003 frontend database with linked tables to an SQL 2000 backend. When a user tries to insert a duplicate value in primary key fields during data entry, the SQL server error message pops up with some verbage (...violation of primary key contraint...) that's hard for the user to understand. My current VBA error trap routine does not capture the SQL errors.
How can I prevent the SQL error message from showing and show my own message instead?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   On Error GoTo Err_Form_BeforeUpdate
 
....VBA body code here...
 
Exit_Form_BeforeUpdate:
    Exit Sub
 
Err_Form_BeforeUpdate:
    MsgBox err.Number & " - " & err.Description
    Resume Exit_Form_BeforeUpdate
End Sub

Answer : MS Access - how to trap SQL primary key violation error?

I'd suggest you do a check for the duplicate (Count of the number of records where PK = your submitted data and if count >0 then tell the user else proceed.


Kelvin
Random Solutions  
 
programming4us programming4us