sfn149
asked on
MS Access Delete Dup Records
I have a access table that contains the following fields.
IndexID, TicketNumber, ItemNumber
I want to delete duplicate records matching on the two fields Ticketnumber, ItemNumber.
Leaving One of the two matching records.
IndexID, TicketNumber, ItemNumber
I want to delete duplicate records matching on the two fields Ticketnumber, ItemNumber.
Leaving One of the two matching records.
Assuming your IndexID is an autonumber, is it used as a foreign key in any other table? If not, you could try:
DELETE FROM yourTable WHERE ID NOT IN (SELECT Min(ID) as MinID FROM yourTable GROUP BY TicketNumber, ItemNumber)
DELETE FROM yourTable WHERE ID NOT IN (SELECT Min(ID) as MinID FROM yourTable GROUP BY TicketNumber, ItemNumber)
ASKER
Neither is working for me, it may help to give me a little more information as to how to incorporate the above into my database.
I tried just configuring a cmd button with VB running the above as sql scripts but each example comes back asking me for values.
I tried just configuring a cmd button with VB running the above as sql scripts but each example comes back asking me for values.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok I replaced tablex with the name of my table "Pawns"
I get an error on the last line that says
Run-Time Error '3061':
too few parameters. Expected 4.
I get an error on the last line that says
Run-Time Error '3061':
too few parameters. Expected 4.
post the codes you are using...
check for correct table name and field names, check for correct spelling too.
check for correct table name and field names, check for correct spelling too.
better create a blank .MDB file, import the table "Pawns"
upload the .MDB that was created..
upload the .MDB that was created..
ASKER
Private Sub cmdDelDup_Click()
Dim sql As String
sql = "delete t.*"
sql = sql & " from Pawns as t"
sql = sql & " where IndexId Not in (select min(Indexid) from Pawns t2 where t2.TicketNumber= t.TicketNumber and t2.ItemNumber=t.ItemNumber )"
CurrentDb.Execute sql, dbFailOnError
End Sub
Dim sql As String
sql = "delete t.*"
sql = sql & " from Pawns as t"
sql = sql & " where IndexId Not in (select min(Indexid) from Pawns t2 where t2.TicketNumber= t.TicketNumber and t2.ItemNumber=t.ItemNumber
CurrentDb.Execute sql, dbFailOnError
End Sub
the codes looks good,
check for correct table name and field names, check for correct spelling too.
check for correct table name and field names, check for correct spelling too.
I would save the SQL string (either Capricorn1's or my original post) as a query (call it qry_Delete_Pawn_Dups). Actually, in my original post I forgot to use IndexID, here is the corrected version
DELETE FROM Pawns WHERE IndexID NOT IN (SELECT Min(IndexID) as MinID FROM yourTable GROUP BY TicketNumber, ItemNumber)
Then, I would modify the click event as:
Private Sub cmdDelDup_Click
currentdb.querydefs("qry_D elete_Pawn _Dups").Ex ecute dbfailonerror
End Sub
DELETE FROM Pawns WHERE IndexID NOT IN (SELECT Min(IndexID) as MinID FROM yourTable GROUP BY TicketNumber, ItemNumber)
Then, I would modify the click event as:
Private Sub cmdDelDup_Click
currentdb.querydefs("qry_D
End Sub
ASKER
I originally had one of my field names incorrect which produced an error. After figuring that out, this worked perfect thanks
delete t.*
from tablex as t
where indexId Not in (select min(indexid) from tablex t2 where t2.TicketNumber= t.TicketNumber and t2.ItemNumber=t.ItemNumber