Saving Duplicate ID Causes Runtime Error (1 Viewer)

jleval

Registered User.
Local time
Today, 04:42
Joined
May 16, 2012
Messages
53
My form has a command button to allow me to save a record. Whenever I save a duplicate record I get a runtime error that throws me into debug mode. How can I keep this from happening and perhaps just have a standard, "you cant save this record because it will cause a duplicate"?
 

jleval

Registered User.
Local time
Today, 04:42
Joined
May 16, 2012
Messages
53
Never mind guys, I used the following code on the after update event to check for duplicates in the table.

If Not IsNull(DLookup("[PartNumber]", "CutterToolSpecs", "[PartNumber] = '" & Me.PartNumber & "'")) Then
MsgBox "Duplicate Tool Number! Must Add A Differant Part Number"
DoCmd.GoToControl ("PartNumber")

End If
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 20:42
Joined
Jan 20, 2009
Messages
12,854
Hopefully you used the correct spelling in your code and your post is just a typo.
"Different"

I work with applications that contain spelling mistakes and it is not a good look.
One of them has fields in one table spelled correctly but wrong in another. It makes for rather silly looking SQL that is easy to get wrong.

ON A.PRIVILEGE = B.PRIVELEDGE
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:42
Joined
Feb 19, 2002
Messages
43,457
As you can see, error trapping is critical if you are going to use the runtime engine to run the application. The runtime engine will simply shut down the database if it encounters an untrapped error.

Although checking for duplicates ahead of time solved this immediate problem, a better solution is to trap the error. That way, you don't have to anticipate what piece of code might throw an error, you just trap all of them. As you run into errors that you want to ignore or explain better, add them to your select case statement.

Code:
Select Case Err.Number
    Case 2501   'user cancelled action - no error message necessary
        Resume Next
    Case ....
        Msgbox "better message"
        Exit Sub
    Case ....
        Msgbox "better message"
        Exit Sub
    Case Else
        Msgbox Err.Number & "--" & Err.Description
        Exit Sub
End Select
 

jleval

Registered User.
Local time
Today, 04:42
Joined
May 16, 2012
Messages
53
Thanks but were would you suggest I place this code?
 

jleval

Registered User.
Local time
Today, 04:42
Joined
May 16, 2012
Messages
53
Yah, it was a tipo in the code above. In my program it is corect.
 

jleval

Registered User.
Local time
Today, 04:42
Joined
May 16, 2012
Messages
53
Yah, it was a tipo in the code above. In my program it is corect. I apriciate ur help. :)

Hopefully you used the correct spelling in your code and your post is just a typo.
"Different"

I work with applications that contain spelling mistakes and it is not a good look.
One of them has fields in one table spelled correctly but wrong in another. It makes for rather silly looking SQL that is easy to get wrong.

ON A.PRIVILEGE = B.PRIVELEDGE
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 06:42
Joined
Feb 19, 2002
Messages
43,457
Thanks but were would you suggest I place this code?

In whatever procedure is getting the error. If it is a data error, put the code in the form's Error event.
 

Users who are viewing this thread

Top Bottom