Solved

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

Posted on 2009-04-09
4
1,317 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

733 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