Solved

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

Posted on 2009-04-09
4
1,273 Views
Last Modified: 2013-11-27
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?
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

Open in new window

0
Comment
Question by:Bill543
  • 2
4 Comments
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 250 total points
ID: 24113051
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
0
 

Author Comment

by:Bill543
ID: 24113084
Thanks Kelvin - I'll try that and let you know. If its not too slow, I'll do it - my concern is that there are some "speed" issues due to server location.
0
 
LVL 44

Assisted Solution

by:Leigh Purvis
Leigh Purvis earned 250 total points
ID: 24113098
Personally, I'd have to say that an initial check of the Count is both an extra hit on the database which is best avoided if possible and also open to the possibility of another insert performed between the check and the subsequent attempt to insert (however small a timeframe that may be, it's still a legitimate concern ;-)
Instead you can intercept the apparent complaint in the form's Error event.
Using code - something like:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
    If DataErr = 3146 Then
        MsgBox "The Primary Key value already exists for another record", vbExclamation
        Response = acDataErrContinue
    End If
End Sub

Open in new window

0
 

Author Comment

by:Bill543
ID: 24113238
Thanks LPurvis. I've incorporated Kelvin's solution on the "After Update" property of the primary key field and it seems quick enough (probably because the database only has around 1000 records).

I'll try your solution once I get some shut eye.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question