Forum Discussion
Tony2021
Nov 20, 2021Steel Contributor
Delete Duplicates (leave 1)
Hello, I have duplicates in my table and I want to delete only 1 of them. I have searched on the net for some code and found the below and fashioned it to my case. I have an error though and it appe...
- Nov 20, 2021
Hi
I'm just about to go out but have a look at the 2 example apps in the attached zip file which show two different approaches to thisHope one/both are of help
BrianWS1O
Nov 24, 2021Brass Contributor
Let's all write to Microsoft and ask them to add a "DELETE DUPLICATES" query wizard in the next release. We've all had to come up with hacky workarounds to do this at some point or other and it shouldn't be such a chore!
The other way I kluge it together is to make a new empty copy of the data table, select the fields (columns) that should constitute a unique record and make them into a Primary Key and then build a query to insert the records from the table with the dupes into it. When Access squawks about not being able to append all the dupes just tell it to go ahead anyway. Then you wind up with a table with no dupes.
The other way I kluge it together is to make a new empty copy of the data table, select the fields (columns) that should constitute a unique record and make them into a Primary Key and then build a query to insert the records from the table with the dupes into it. When Access squawks about not being able to append all the dupes just tell it to go ahead anyway. Then you wind up with a table with no dupes.
isladogs
Nov 25, 2021MVP
To use the method you described, you should use a unique index for the fields that should not contain duplicates. Your suggestion of a composite primary key isn't necessary
- BrianWS1ONov 25, 2021Brass Contributor
True, either way works...if you're only using one field to identify duplicates. If you have to combine several fields to determine a unique record then as far as I know you'd need to create a multi-field PK
- isladogsNov 25, 2021MVP
Not true.
Setting a unique index works on a single field or multiple fields.
There is no need to set those fields as a primary key for this purpose.
In general, it is usually better to avoid the use of composite primary keys.
- BrianWS1ONov 26, 2021Brass ContributorI don't follow; how do you set an index in MS Access across multiple fields? For example, if I want to avoid duplicates and I have records with First Name, Last Name, City, and State, but I don't want that same person at that location entered twice, how do I set up an index to prevent that?
Also, you can create a composite PK just temporarily to append the records and weed out the dupes, then change back to using a different PK.
Can you explain why a composite primary key should be avoided? Is it a processing thing? Does it matter with record sets below a certain size? Does it appreciably slow down processing when adding records? I'd like to see documentation to determine when this is or isn't a concern.