An AutoNumber primary key guarantees that every record has its own unique ID. It does not guarantee that the information in that record makes sense. You can still accidentally register the same employee for the same training session twice, add the same product to an order twice, or assign the same warehouse item to the same location more than once. The trick is to define which combination of fields must be unique.
In Access, the usual solution is a unique multi-field index, sometimes called a composite unique index. It lets each individual value repeat where it should, while preventing the specific combination of values that represents a duplicate. No VBA is required, and you do not have to get rid of your AutoNumber primary key.
Consider a registration table. You might have a RegistrationID AutoNumber field as the primary key, plus EmployeeID and SessionID fields. One employee can attend many sessions, so EmployeeID must be allowed to repeat. One session can have many employees, so SessionID must also be allowed to repeat.
What you do not want is the same EmployeeID and SessionID appearing together twice. If employee 7 is registered for session 101, that exact pair should only exist once. Employee 7 can still register for session 102, and employee 8 can still register for session 101. Those are valid combinations.
A common beginner mistake is setting EmployeeID to Indexed: Yes (No Duplicates). That certainly prevents duplicates, but it prevents far too much. Now each employee can only appear once in the entire table. Congratulations, your employees are allowed one training class for the rest of their careers. Probably not the business rule you had in mind.
The same problem occurs if you make SessionID unique by itself. You would only be able to register one employee in each session, which makes for a very quiet classroom.
Instead, leave your AutoNumber field as the primary key and create a separate index that includes both fields. Open the table in Design View, then click the Indexes button on the ribbon. Access will show the existing primary-key index and any other indexes already defined for the table.
On a new row, enter a meaningful index name, such as EmployeeSession. In the Field Name column on that same row, select EmployeeID. On the row directly underneath it, leave the Index Name column blank and select SessionID in the Field Name column. That blank index-name cell is important. It tells Access that SessionID belongs to the same index as EmployeeID, rather than starting a brand-new index.
With the first row of that index selected, set the Unique property to Yes. Save the table. From that point forward, Access will reject a record that repeats the same EmployeeID and SessionID pair.
When a user tries to enter the duplicate, Access will display its standard message indicating that the change would create duplicate values in an index, primary key, or relationship. It is not the friendliest message in the universe, but it does enforce the rule at the table level. That means the protection applies whether records are entered through a form, datasheet, query, import, or some other part of the database.
This is sometimes described as a composite key because the rule is made from multiple fields. A composite primary key is also possible, where the combined fields become the table's actual primary key. That can be a perfectly valid design in some databases. In many Access applications, however, keeping a simple AutoNumber primary key and adding a separate unique composite index is easier to work with, especially when other tables need to refer to one specific record.
The important part is deciding exactly what constitutes a duplicate in your application. Sometimes two fields are enough. Sometimes they are not. Suppose an employee can be assigned to the Navigation duty on Mission 501 and also to Navigation on Mission 502. A unique index on EmployeeID and DutyID would incorrectly block the second assignment. In that case, the real rule is the combination of EmployeeID, DutyID, and MissionID.
The same technique works with three or more fields. Give the index a name on the first row, add the first field, then place each additional participating field on the rows below it with the index name left blank. Set Unique to Yes, and Access will make sure that the complete combination is not repeated.
Before adding a unique index to an existing table, make a backup and work on a test copy if possible. If duplicate combinations already exist, Access will not be able to create the unique index until you clean them up. You may need to run a Find Duplicates query first, review the old records, and decide which ones should remain.
Also remember that uniqueness and required data are two different rules. If every registration must include both an employee and a session, set the participating foreign-key fields to Required: Yes as appropriate. Do not assume that making an index unique automatically means users must fill in every field.
Once the index is in place, test both sides of the rule. Try entering the same employee and same session twice. That should fail. Then try the same employee in a different session, and a different employee in the original session. Those should work normally. The goal is not to block everything. The goal is to block the mistake while allowing legitimate work to continue.
The embedded video walks through the Indexes window in Access and shows the setup in action. Once you understand unique multi-field indexes, they become one of those simple little database tools that can save you from a lot of cleanup later.
Live long and prosper,
RR