Hello,
I am trying to write a code that will look through a table for duplicates and delete them. I currently have a code I found on an older post in bytes that works fine for deleting duplicates but I am trying to figure out a way i can modify it to delete duplicates based on a criteria.
I have been trying to change it so that if I have strField(1) that is Not Null and another strField(2) that Is Null and strField(1) is a duplicate then delete it.
Any help would be appreciated.
Thanks,
Slen
below is the code i am working with (orginally posted by NeoPa)
I am trying to write a code that will look through a table for duplicates and delete them. I currently have a code I found on an older post in bytes that works fine for deleting duplicates but I am trying to figure out a way i can modify it to delete duplicates based on a criteria.
I have been trying to change it so that if I have strField(1) that is Not Null and another strField(2) that Is Null and strField(1) is a duplicate then delete it.
Any help would be appreciated.
Thanks,
Slen
below is the code i am working with (orginally posted by NeoPa)
Code:
'DelDups Removes duplicate records in strTable matched on strField
Public Sub DelDups(strTable As String, strField As String)
Dim strSQL As String, varLastVal As Variant
'Recordset must be full table but sorted by the field we're checking
strSQL = "SELECT * FROM [" & strTable & "] ORDER BY [" & strField & "]"
With CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
varLastVal = Null
'For each record, check against previous value in strField
'If same then this is a duplicate so can be removed
Do Until .EOF
If .Fields(strField) = varLastVal Then
Call .Delete
Else
varLastVal = .Fields(strField)
End If
Call .MoveNext
Loop
'Ending the 'With' releases the Recordset
End With
End Sub
Comment