Finding Duplicate Records

I'm using a Microsoft Access database (not created in Access) in VB6. I need to be able to detect duplicate records using SQL.

Each record has an InvoiceNumber field which is a Long. Each InvoiceNumber should be unique but I don't want to use an Index to make the InvoiceNumber unique. (I didn't design the database, I just have to work with it.)

I want to be able to create a recordset that contains all duplicate InvoiceNumbers in the database.
Who is Participating?
RainUKConnect With a Mentor Commented:
Try this SQL Statement :

SELECT InvoiceNumber
FROM tablename
GROUP BY InvoiceNumber
count(*) > 1

This will return a recordset of all invoice numbers which have been duplicated in the DB. By the way if you want to make the invoice number a unique, just set it as the Primary Key. This will throw an error if somebody then trys to insert another row with the same invoice number.

But remember to clear the duplicates first, otherwise primary key won't work.
the sql would be

select InvoiceNumber, count(*) as Duplicatecount from tablename
group by Invoicenumber
where count(*) > 1

this shall return you all the duplicated Invoice Numbers as well as No of time each invoice number has been duplicated
bhlangAuthor Commented:
Your code didn't work for me.
bhlangAuthor Commented:
This code worked exactly as I wanted.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.