In the BeforeUpdate event on my field, I have a dcount set up to disallow duplicates in the field SurveyID. This works great, except in the event that, in a past record, the user starts editing their SurveyID by accident and tries to cancel (accidentally selects the SurveyID field, starts typing, then erases it and enters the SurveyID). It reads that SurveyID as a duplicate (even though it's not really) and won't allow them to keep the ID. I've already done a half workaround by having the SurveyID reset after this, so if they're in the above scenario they can at least retain their ID. What I really want to know if there's a way with my code to prevent this from happening. The SurveyId is NOT the primary key, as there are two different ID codes they could have used. Maybe an extra piece of logic saying that if the primary key in that record in the form matches with the primary key of the "duplicate" (aka itself) then it doesn't get processed at a duplicate?
Here is my current code
Private Sub SurveyID_BeforeUpdate(Cancel As Integer)
'checks for duplicates'
If DCount("SurveyID", "test", "SurveyID=" & Nz(Me.SurveyID, 0)) > 0 Then
Beep
MsgBox "The Survey ID number you have entered is a duplicate. Please double check that the number you entered is correct. If it is correct, please X."
Me.SurveyID.Undo
Cancel = True
End If
End Sub