• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Prevent Entering Duplicate Record

On a form with fields of FirstName and LastName I would like to prevent user from entering the same person twice based solely upon those two fields. I assume this would occur in the afterupdate event for each control.  Could you please provide the code and possibly a message to alert the user.  Thanks
3 Solutions
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
see image ... create a Unique Index on those two fields ... and Access will automatically prevent this from happening ... no code.

To check for duplicate record to intruduce your defined messages, try this:
Table a (f1, f2). If the current record is a duplicate, it displays the message and cancels the saving of the record.

Private Sub Form_BeforeUpdate(Cancel As Integer)
    Dim c As Integer
    c = DCount("f1", "a", "f1='" & Me.f1 & "' and f2='" & Me.f2 & "'")
    If c > 0 Then
        MsgBox ("duplicate record, please modify")
        Cancel = True
    End If
End Sub
Jeffrey CoachmanMIS LiasonCommented:
This question has been discussed many times.

What hnasr and MX posted will address your specific request.

Just some "FYI" Notes here:
The issue with any "Prevent Duplicates" request is that the definition of "Duplicates" is open to interpretation.

First Name: Jeffrey
Last Name: Coachman

By your definition, only Jeffrey Coachman would constitute a duplicate.
This means that all of the following would be allowed even though it is the same person:
Jeffery Coachman
Jeffrey Coachmen
Jeff Coachman
J Coachman
Geoffrey Coachman
First Name: Jeffrey (Last Name blank)
Last Name: Coachman (First name Blank)
What's your plan for records like this?

Sure you can add more field to the check, but the same issues with misspellings still apply...
And also the more fields you try to check, the longer this will take (especially if the list is fairly long)

Also remember that in the current Business environment many people will want to set up two accounts, one Business and one Personal.
In this case both sets of names need to be the same, and only the "Type" will be different (Cust Type, Account Type, Member type, ...etc)
What;'s your plan for this contingency?

Again, Just FYI



zpotokAuthor Commented:
Thank you all..
Jeffrey CoachmanMIS LiasonCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now