Skip to content
Auscultby NBIT
EN
Free diagnostic

Access error reference

Access error 3022: duplicate values in index or key

A user creates a record, or a button adds rows, and Access refuses: the changes “would create duplicate values”. The value typed or computed already exists in a field that does not accept duplicates — often the primary key.

The message

Datasheet of the Clients table with number 12 twice, and the Microsoft Access box “The changes you requested to the table were not successful because they would create duplicate values…”
The real Access box, captured in Access for Microsoft 365 in English: a customer number typed in the datasheet already exists (12).

The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship. Change the data in the field or fields that contain duplicate data, remove the index, or redefine the index to permit duplicate entries and try again.

English Access

On a French Access (Microsoft 365): « Modifications non effectuées : risque de doublons dans champs index, clé principale ou relation interdisant les doublons. Modifiez les données des champs contenant les doublons, enlevez ou redéfinissez l'index pour permettre les doublons et recommencez. »

Where it shows up

When a record is saved in a data-entry form, in code that adds or edits rows (rs.AddNew then rs.Update, CurrentDb.Execute … , dbFailOnError), and in an append or update query run from code. Run by hand or through DoCmd.RunSQL, the same query shows a warning instead — “key violations” — and the duplicate rows are not added.

Common causes

  • an AutoNumber out of step: after an append query that copied the numbers, or a repaired database, the next number handed out already exists;
  • a no-duplicates index on a field that receives one: a name, an order number, a reference entered twice;
  • a primary key computed in code (DMax("ID", "T") + 1) that two PCs compute at the same moment;
  • an append query copying rows already there, for instance an import run twice;
  • a composite key (two fields) whose combination already exists, or a one-to-one relationship.

How to reproduce and check it

  1. Open the table in Design view, then Indexes (Table Design tab): note the indexes whose Unique property is Yes, and the primary key.
  2. Look for the duplicate value with a query: SELECT Ref, Count(*) FROM T GROUP BY Ref HAVING Count(*) > 1, or check whether the value being added already exists.
  3. For an AutoNumber, compare DMax("ID", "T") with the number Access tries to give a new record.

How to fix it

If the value must be unique, the data is what needs fixing: warn the user before saving. If the index has no reason to be unique, set it to “Yes (Duplicates OK)”. An AutoNumber out of step is sometimes reset by Compact and Repair; otherwise, by a statement that sets the next number.

Back up the database before running the ALTER TABLE statement, and run it when nobody is using the table. If the primary key is an AutoNumber linked to other tables by relationships, do not change its values: only reset the counter.

What Auscult reports

The diagnostic records 3022 wherever Access returns it during the run, and files it among the faults to fix, with the object that produces it. It does not run queries that would change data: a duplicate that only real data entry would trigger may not appear during the run. The report does flag, among the data risks, tables without a primary key, where duplicates get in without any message.

The Access error reference, code by code

Frequently asked questions

Why does it happen on a brand-new record where I typed nothing that already exists?

Because the duplicate is often the automatic number, not what you typed. After rows were copied in or the database was repaired, the counter can offer a number already taken.

Can I just delete the primary key?

No. Without a key, duplicates get in silently, relationships no longer hold, and a move to SQL Server or the web becomes harder. Fix the data or the counter.

Where are the other errors in your application hiding?

The diagnostic runs on your computer, on a copy of the application, without sending the data in your tables. The health grade and the summary are free.