3 fields together unique/no duplicates?

J

JBowler

Is it possible to make a combination of 3 fields be unique? I know you can
have the ID (autonumber) be unique or have a field set to be indexed and not
allow duplicates but I need to use 3 fields.

My scenario is a table (tblPeople) with Fname, Lname and postcode. I do not
want any duplicate people in my tblPeople. They all have multiple linked
records in tblProjects. The problem is that someone doing data entry keeps
creating duplicate people entries when they exist already. Can I set these 3
to be unique together? Not as individual uniques.

Would it be easier to make a field in the tblPeople that is a combination of
Fname Lname Postcode and that be unique? Then on the form it would use some
code to make the combination entered into that field. I could run a query to
make the 3000+ records have that combination field so its all up to date.

What is my best approach? Any help or thoughts are appreciated. Many thaks.

JBowler
 
N

Nikos Yannacopoulos

J.,

No need to use an extra field for concatenation, just open your table in
design view, select all three fields (by clicking on the little grey button
to the left of each field name while holding down Ctrl), and click on the
Primary Key tool button on the toolbar. This will create a composite PK that
will only allow unique combinations of the three field entries.

Note: this is an easy way to do it, but not necessarily the best design.
Ideally, you would use an autonumber PK field in tblPeople, which would make
joins to other tables much more efficient, and employ some other method to
avoid double entries, e.g. use a macro or some code in the Before Update
event of the data entry form to look for double entries. Alternartively, you
could stick with the composite PK on the three fields to prevent duplicates,
but still use an (indexed) autonumber field for joining with other tables.

HTH,
Nikos
 
L

Lynn Trapp

In addition to what Nikos has said, you will probably want to reconsider
your plan a bit. Do you suppose that it's possible for 2, or more, John
Smiths to live in the same Postal Code? You could possibly encounter a
situation where 2, or more, John Smiths live at the same street address, use
the same telephone number, etc. In most cases, it will take more than the 3
fields you suggested to guarantee a unique record.
 
J

JBowler

We considered that but in the UK a postcode is limited to very few
households. The chance exists but is rare. I dont have any other fields that
can be part of a unique. I will go with the advice and keep this in mind.

Many thanks for your help.

James
 
T

Tim Ferguson

We considered that but in the UK a postcode is limited to very few
households. The chance exists but is rare.

Don't believe it. Fathers often give their names to their sons (and mothers
to daughters) so it is going to be more-than-averagely likely for the same
name to be present at the same address. The existence of nulls (what, you
mean you never, ever, don't know the postcode?) will complicate matters
further.

By all means have your form check for likely clashes (for example, use the
Form_BeforeUpdate event) but using the db engine to prevent them is going
to lead to very disgruntled users, not to say the data subjects.

Hope that helps


Tim F
 
L

Lynn Trapp

Don't believe it. Fathers often give their names to their sons (and
mothers
to daughters) so it is going to be more-than-averagely likely for the same
name to be present at the same address.

Exactly. My father and my older brother were both named Robert Trapp and
lived at the same address until my brother went to college. Adding a middle
initial to the mix would have distinguished them, but that wouldn't always
be the case. It happens all the time.
 
J

John Vinson

Is it possible to make a combination of 3 fields be unique? I know you can
have the ID (autonumber) be unique or have a field set to be indexed and not
allow duplicates but I need to use 3 fields.

My scenario is a table (tblPeople) with Fname, Lname and postcode.

How would your table handle Fred Brown, postcode 83660, and his son
Fred Brown, postcode 83660?

Names ARE NOT UNIQUE, even within a household, much less a postcode.
I do not
want any duplicate people in my tblPeople. They all have multiple linked
records in tblProjects. The problem is that someone doing data entry keeps
creating duplicate people entries when they exist already. Can I set these 3
to be unique together? Not as individual uniques.

Certainly. You can include up to ten fields in a Primary Key -
ctrl-mouseclick the fields and then click the key icon.
Would it be easier to make a field in the tblPeople that is a combination of
Fname Lname Postcode and that be unique?

No. This is redundant, inefficient, and unnecessary!
Then on the form it would use some
code to make the combination entered into that field. I could run a query to
make the 3000+ records have that combination field so its all up to date.

What is my best approach? Any help or thoughts are appreciated. Many thaks.

I'd say the best approach is to use the BeforeUpdate event of the Form
in which the user is entering data to check for duplicates. Something
like:

Private Sub Form_BeforeUpdate(Cancel as Integer)
Dim vFound As Variant
vFound = DLookUp("PersonID", "your-table-name", "[FName] = """ & _
Me!txtFName & """ AND LName = """ & Me!txtLName & _
""" AND PostCode = '" & Me!txtPostCode & "'")
If Not IsNull(vFound) Then
MsgBox "This person's name already exists. Proceed anyway?", _
vbYesNo)
If iAns = vbNo Then Cancel = True
End If
End Sub

or you can get fancier and give the user the option of viewing the
existing record.

John W. Vinson[MVP]
Join the online Access Chats
Tuesday 11am EDT - Thursday 3:30pm EDT
http://community.compuserve.com/msdevapps
 
N

Nikos Yannacopoulos

James,

Regrettably, all above holds true, no question you'll hit duplications
sooner or later, it's just a matter of time. It's no coincidence the best
practice on the subject in the US is the use of the Social Security Number
as a PK. Don't you have something similar in the UK, like an NHS
registration number or something, assigned to a person virtually at birth?
It would be the perfect solution (Access can't handle DNA signatures yet!).

Nikos
 
N

Nikos Yannacopoulos

All,

We've missed one more possibility: misspellings. Take a look at my surname
and try to guess how many different spellings of it I've seen over the
years, then multiply by ten to be closer! How many records would I get?

Nikos
 
T

Tim Ferguson

Don't you have something similar in the UK, like an NHS
registration number or something, assigned to a person virtually at
birth?

Yes but...

It is forbidden to use them for linking outside the health
service, even to store them on non-NHS systems

Anyway, nobody knows their number

Anyway, they are not duplicate-free (but what do you expect from
Government IT systems?)...

We also have National Insurance Numbers but they are not widely used
outside the taxation/ employment spheres, for reasons similar to above.

All the best


Tim F
 
N

Nikos Yannacopoulos

Tim,

Another idea would be to use a fourth field along the three already in the
PK, to store year of birth... I would hope there would be no twins with the
same name, though first cousins with the same name living in the same
building and born in the same year is always a possibility!!!

HTH,
Nikos
 
L

Lynn Trapp

We've missed one more possibility: misspellings. Take a look at my surname
and try to guess how many different spellings of it I've seen over the
years, then multiply by ten to be closer! How many records would I get?

<g> Unfortunately, that is the inevitable problem with every Customer,
Supplier, Member, etc. table on the planet. I don't think anyone has found a
solution to it either...and they probably won't.
 
T

Tim Ferguson

I would hope there would be no twins with the
same name, though first cousins with the same name living in the same
building and born in the same year is always a possibility!!!

Christian & Christine; Francesca and Francis; Jan and Jon; etc.... Please,
just believe it -- names make rotten unique identifiers!

All the best


Tim F
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top