Checking for duplicate records

GreenshootProgrammer

Registered User.
Local time
Today, 12:56
Joined
Jan 16, 2013
Messages
74
What would be the Access 2007 VBA code for checking for duplicate records?
 
Last edited:
Have you tried using the "Find Duplicates" query wizard?
 
There are several methods for that. One would be indexing a field and not allow duplicates. Another is looking for duplicates in the BeforeUpdate event of a Control or Form.
 
Last edited:
There are several methods for that. One would be indexing a field and not allow duplicates. Another is looking for duplicates inn the BeforeUpdate event of a Control or Form.

They are indexed (no duplicates) but I'm still able to create duplicates using the form. I'm going to need to add an extra layer to prevent duplications. What event should I add to the BeforeUpdate for these controls?
 
@Dale: You may be correct.
@Green: Is this a duplicate thread on the same issue?
 
Thought one of these might have worked but I get the prompt even when a new ID is entered.

Code:
Private Sub txtCourseID_BeforeUpdate(Cancel As Integer)
   If DCount("CourseID", "tblCourse", [txtCourseID]) > 0 Then
      MsgBox "This Course ID is already being used."
      Me.txtCourseID.Undo
   End If
End Sub
Code:
Private Sub txtCourseID_BeforeUpdate(Cancel As Integer)
If Not IsNull(DLookup("CourseID", "tblCourse", [txtCourseID])) Then
    MsgBox "This Course ID is already being used."
    Me.txtCourseID.Undo
End If
End Sub
Any suggestions?
 
Last edited:
Sorry Green but this site did not let me know you posted again. FYI, *all* arguments to any of the Domain functions are strings.
 
np, I tried:

Code:
Private Sub txtCourseID_BeforeUpdate(Cancel As Integer)
If DCount("*", "tblCourse", "CourseID=" & Me.txtCourseID.Value) > 0 Then
    Cancel = True
    MsgBox "This Course ID is already being used."
    Me.txtCourseID.Undo
End If
End Sub
but I get the following error prompt:

Run-time error '3464':
Data type mismatch in criteria expression.
 
I think your CourseID field is text. Try:
If DCount("*", "tblCourse", "CourseID='" & Me.txtCourseID & "'") > 0 Then
 
greenshootProgrammer and RuralGuy, If I may impose here.
I do believe that the table ID is text if I remember correctly.

Dale
 
The default property of a control is the .Value property so it is not necessary to actually specify it.
 
You would not have the problem if you used an AutoNumber as a PK.
 
Put a MsgBox in the BeforeUpdate event before the DLookup and see what is in the value you are using.
 

Users who are viewing this thread

Back
Top Bottom