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
Patrick O'DeaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
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 Architect / Systems AnalystCommented:
"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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.