Prevent Entering Duplicate Record

Posted on 2011-05-09
Medium Priority
Last Modified: 2013-11-27
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
Question by:zpotok
LVL 75

Accepted Solution

DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 800 total points
ID: 35725182
see image ... create a Unique Index on those two fields ... and Access will automatically prevent this from happening ... no code.

LVL 31

Assisted Solution

hnasr earned 800 total points
ID: 35725538
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
LVL 74

Assisted Solution

by:Jeffrey Coachman
Jeffrey Coachman earned 400 total points
ID: 35730242
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




Author Closing Comment

ID: 35733102
Thank you all..
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35733246

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question