Checking for duplicate records (1 Viewer)

GreenshootProgrammer

Registered User.
Local time
Today, 08:37
Joined
Jan 16, 2013
Messages
74
What would be the Access 2007 VBA code for checking for duplicate records?
 
Last edited:

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
Have you tried using the "Find Duplicates" query wizard?
 

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
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:

GreenshootProgrammer

Registered User.
Local time
Today, 08:37
Joined
Jan 16, 2013
Messages
74
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?
 

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
@Dale: You may be correct.
@Green: Is this a duplicate thread on the same issue?
 

GreenshootProgrammer

Registered User.
Local time
Today, 08:37
Joined
Jan 16, 2013
Messages
74
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:

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
Sorry Green but this site did not let me know you posted again. FYI, *all* arguments to any of the Domain functions are strings.
 

GreenshootProgrammer

Registered User.
Local time
Today, 08:37
Joined
Jan 16, 2013
Messages
74
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.
 

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
I think your CourseID field is text. Try:
If DCount("*", "tblCourse", "CourseID='" & Me.txtCourseID & "'") > 0 Then
 

rzw0wr

I will always be a newbie
Local time
Today, 03:37
Joined
Apr 1, 2012
Messages
489
greenshootProgrammer and RuralGuy, If I may impose here.
I do believe that the table ID is text if I remember correctly.

Dale
 

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
The default property of a control is the .Value property so it is not necessary to actually specify it.
 

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
You would not have the problem if you used an AutoNumber as a PK.
 

RuralGuy

AWF VIP
Local time
Today, 01:37
Joined
Jul 2, 2005
Messages
13,826
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

Top Bottom