I have a VB6 program that adds a column to an MS Access database thusly:
alter table x add column y long constraint z unique
The program goes through a number of databases without error; however, on the one that I am looking at now, it gives me “The changes you requested to the table were not successful because they would create duplicate values in the index, primary key, or relationship…”
In case it makes a difference, I add values to the column by forming a recordset of the primary key and new column values, then going through each record to add a value to this column. I do recordSet.updateBatch when I’m all done.
If I remove the constraint, it completes normally; I have put all 1600 values into a spreadsheet, sorted by the values I’ve added, and used a formula to check for duplicates. There aren’t any. All rows get a new value, none of the new values are the same as any other new value.
Are there other reasons why I might get this error? I really don’t want to remove the constraint, but I don’t know how to get past this.
Since you’re certain you’re not attempting to insert rows which violate the table’s index constraints, perhaps you have a corrupted index. See whether Compact & Repair cures the problem. But first make a backup of the database.
You could also recreate the table in a new database and test it there.
You can find further information about corruption at Tony Towes’ Corrupt Microsoft Access MDBs FAQ.