Combination of two fields must be unique.

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,258
Office Version
365
Platform
Windows
I know if I set two fields as Primary Keys then the combination of those two fields cannot be duplicated within the table.
Is there a way of achieving this without setting the fields as Primary Keys?

I have a table with four fields:
IDAutoNumber and PK
sPostalSectorText
lBranchIDNumber
lGroupNameNumber

<tbody>
</tbody>

A combination of sPostalSector and lBranchID field cannot be duplicated.
As sPostalSector is text I'm pretty sure it's not the best idea to have it as part of a Primary Key.

Any help would be greatly appreciated.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,493
Office Version
365
Platform
Windows
As sPostalSector is text I'm pretty sure it's not the best idea to have it as part of a Primary Key.
Why not? I use text fields as primary keys all the times (i.e. my client codes are often alphanumeric).
I have multi-field primary keys where both fields are text, and it works fine.

So using a mult-field primary key is certainly the easiest way. Otherwise, you may have to use VBA, and control all data entry through Forms.
Or try to figure out how to place Constraints on your tables/fields (easy in SQL, not so easy in Access). See: foreign key relationship - Uniqueness Constraints in MS Access - Stack Overflow
 

xenou

MrExcel MVP, Moderator
Joined
Mar 2, 2007
Messages
16,553
Office Version
2013
Platform
Windows
Also just to note, in the indexes dialog you can create any index:
How to create a composite unique index (not as a primary key) in MS Access
Create Table Index In Access 2010


For a composite index, you have to add the first row with the name of the index. Set the property value for the index to unique. In the row(s) below it, leave the name BLANK but continue to select any other fields that are part of the index, until you have all the fields included with the index, but the name of the index shown only in the first of those rows.

Agree that there's no real issue with making this a primary key - it should work either way. A primary key is basically a unique index, but it has the extra requirement that there be no null values.
 
Last edited:

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,258
Office Version
365
Platform
Windows
Thanks to both of you - I'll look into it. I've used Constraints before in MySQL (I think it was), but has been so long since I've built a database that I'm very rusty in anything other than fairly simple queries.
Need to get my books out again methinks.

Still haven't managed to test the code you gave me yesterday Xenou - will be after Easter now.
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
51,493
Office Version
365
Platform
Windows
So, based on all that we said, is there still some reason you don't want the use a multi-field primary key?
 

Darren Bartrup

Well-known Member
Joined
Mar 13, 2006
Messages
1,258
Office Version
365
Platform
Windows
Sorry for taking a few days - had to have a few days off to bask in the glory of a zombified son of a sky wizard.

So - no reason for not using a multi-field primary key, even if one of those is a text field. Problem solved, except I'm thinking of doing the back-end in MySQL now as our IT department will support that (I've always wondered what their definition of 'supporting' software is - I keep meaning to send them a spreadsheet and see if they can support anything more confusing than a simple VLOOKUP).

Thanks for the help and pointers.
 

Forum statistics

Threads
1,082,269
Messages
5,364,148
Members
400,783
Latest member
sambills

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top