• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

Better Handling Of Duplicate Key Error

See Attached - One table and one continuous form .

The "CustomerCode" is the unique identifier of of the table.

Therefore if I set up a NEW customer with a code that ALREADY exists I will get a nasty message when I try to exit from the record.  "The Changes you requested to the table were not not successful because they would create duplicate values .... etc"

How can I give a more elegant response to the users error.  ?
 This error does not seem to respond to normal VBA error handling.


Database1.accdb
0
Patrick O'Dea
Asked:
Patrick O'Dea
  • 3
2 Solutions
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
This error must be trapped in the Form Error event, like so:

Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Response = acDataErrContinue
    Select Case DataErr
       
        Case 3022   'Dup Field
            MsgBox "One or more required fields are missing. Please enter ALL required fields <Your Field List Here>"

            MsgBox "An unexpected error occurred " & DataErr & "  " & AccessError(DataErr)
    End Select
End Sub
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Actually ... with correct message for error

Private Sub Form_Error(DataErr As Integer, Response As Integer)
    Response = acDataErrContinue
    Select Case DataErr
       
        Case 3022   'Dup Field
            MsgBox "Duplicated entry bonehead - get it together"

            MsgBox "An unexpected error occurred " & DataErr & "  " & AccessError(DataErr)
    End Select
End Sub
0
 
Rey Obrero (Capricorn1)Commented:
use the beforeupdate event of the form to check for the duplicate

private sub form_beforeupdate(cancel as integer)

if dcount("*","tableName","[CustomerCode]=" & me.CustomerCode)>0 then
msgbox "Duplicate record"
cancel=true
exit sub

end if

end sub
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
"This error does not seem to respond to normal VBA error handling."
That's because that error is only rendered in the Form Error event, which occurs prior to the Before Update event ...

mx
0
 
Patrick O'DeaAuthor Commented:
Thanks gents,

Great responses as always.

I was not aware of the Form_Error facility.
I guess it is self explanatory.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now