Delete Duplicate Function

Hi Experts

The below function is working however, it its not deleting the newest record.

Record 1 has a date of 5/17/2001 - I want to delete this date
Record 2 has a date of 10/15/2001 - This is the one that its deleting
Function DeleteDupes()
    Dim strSQL As String
    Dim strSQLDel As String
    Dim rs As DAO.Recordset
    Dim arr() As String
    
   ' This query selects the max date, grouping by Constraint Number, only considering records that have duplicates

     strSQL = "SELECT  [Constraint Number] & '~' & [Allocation Group] AS CompFields, Count([Date Added]) AS RecCount, Max([Date Added]) AS MaxDate FROM [tbl_GMNA Constraint Report Output]" _
             & " GROUP BY  [Constraint Number] & '~' & [Allocation Group]" _
             & " HAVING Count([Date Added]) > 1"
             
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    If rs.RecordCount = 0 Then
        MsgBox "No dupes found"
        Exit Function
    End If
    
    'Loop through the recordset deleting duplicates with the same constraint number but lesser dates
    Do Until rs.EOF
        arr = Split(rs!CompFields, "~")
        strSQLDel = "DELETE * FROM [tbl_GMNA Constraint Report Output] WHERE " _
             & "[Constraint Number] = '" & arr(0) & "' " _
             & "AND [Allocation Group] = '" & arr(1) & "' " _
             & "AND [Date Added] < #" & rs!MaxDate & "#"
       
        CurrentDb.Execute strSQLDel, dbFailOnError
        rs.MoveNext
    Loop
        
    rs.Close
    Set rs = Nothing
    
'MsgBox "Duplication Check Completed"
End Function

Open in new window

mato01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mbizupCommented:
Is DateAdded a true Date field or is it text?

Either way, give this modification a try (this explicitly converts it to a date type):

Function DeleteDupes()
    Dim strSQL As String
    Dim strSQLDel As String
    Dim rs As DAO.Recordset
    Dim arr() As String
    
   ' This query selects the max date, grouping by Constraint Number, only considering records that have duplicates

     strSQL = "SELECT  [Constraint Number] & '~' & [Allocation Group] AS CompFields, Count(CDate([Date Added])) AS RecCount, Max([Date Added]) AS MaxDate FROM [tbl_GMNA Constraint Report Output]" _
             & " GROUP BY  [Constraint Number] & '~' & [Allocation Group]" _
             & " HAVING Count(CDate([Date Added])) > 1"
             
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    If rs.RecordCount = 0 Then
        MsgBox "No dupes found"
        Exit Function
    End If
    
    'Loop through the recordset deleting duplicates with the same constraint number but lesser dates
    Do Until rs.EOF
        arr = Split(rs!CompFields, "~")
        strSQLDel = "DELETE * FROM [tbl_GMNA Constraint Report Output] WHERE " _
             & "[Constraint Number] = '" & arr(0) & "' " _
             & "AND [Allocation Group] = '" & arr(1) & "' " _
             & "AND CDate([Date Added]) < #" & CDate(rs!MaxDate) & "#"
       
        CurrentDb.Execute strSQLDel, dbFailOnError
        rs.MoveNext
    Loop
        
    rs.Close
    Set rs = Nothing
    
'MsgBox "Duplication Check Completed"
End Function 

Open in new window

0
mbizupCommented:
Sorry - misplaced some of the changes in that.

This is corrected:

Function DeleteDupes()
    Dim strSQL As String
    Dim strSQLDel As String
    Dim rs As DAO.Recordset
    Dim arr() As String
    
   ' This query selects the max date, grouping by Constraint Number, only considering records that have duplicates

     strSQL = "SELECT  [Constraint Number] & '~' & [Allocation Group] AS CompFields, Count([Date Added]) AS RecCount, Max(CDate([Date Added])) AS MaxDate FROM [tbl_GMNA Constraint Report Output]" _
             & " GROUP BY  [Constraint Number] & '~' & [Allocation Group]" _
             & " HAVING Count([Date Added]) > 1"
             
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    If rs.RecordCount = 0 Then
        MsgBox "No dupes found"
        Exit Function
    End If
    
    'Loop through the recordset deleting duplicates with the same constraint number but lesser dates
    Do Until rs.EOF
        arr = Split(rs!CompFields, "~")
        strSQLDel = "DELETE * FROM [tbl_GMNA Constraint Report Output] WHERE " _
             & "[Constraint Number] = '" & arr(0) & "' " _
             & "AND [Allocation Group] = '" & arr(1) & "' " _
             & "AND CDate([Date Added]) < #" & CDate(rs!MaxDate) & "#"
       
        CurrentDb.Execute strSQLDel, dbFailOnError
        rs.MoveNext
    Loop
        
    rs.Close
    Set rs = Nothing
    
'MsgBox "Duplication Check Completed"
End Function 

Open in new window

0
mato01Author Commented:
Got a Compile Error: Invalid use of Null

The below line is highlighted in yellow


 Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

mbizupCommented:
Compile error or runtime ??

Can you post a sample?
0
mbizupCommented:
Actually, if DateAdded is NULL in any records, give this a try first:


Function DeleteDupes()
    Dim strSQL As String
    Dim strSQLDel As String
    Dim rs As DAO.Recordset
    Dim arr() As String
    
   ' This query selects the max date, grouping by Constraint Number, only considering records that have duplicates

     strSQL = "SELECT  [Constraint Number] & '~' & [Allocation Group] AS CompFields, Count([Date Added]) AS RecCount, Max(CDate(nz([Date Added], #1/1/1900#))) AS MaxDate FROM [tbl_GMNA Constraint Report Output]" _
             & " GROUP BY  [Constraint Number] & '~' & [Allocation Group]" _
             & " HAVING Count([Date Added]) > 1"
             
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    If rs.RecordCount = 0 Then
        MsgBox "No dupes found"
        Exit Function
    End If
    
    'Loop through the recordset deleting duplicates with the same constraint number but lesser dates
    Do Until rs.EOF
        arr = Split(rs!CompFields, "~")
        strSQLDel = "DELETE * FROM [tbl_GMNA Constraint Report Output] WHERE " _
             & "[Constraint Number] = '" & arr(0) & "' " _
             & "AND [Allocation Group] = '" & arr(1) & "' " _
             & "AND CDate(NZ([Date Added],#1/1/1900#)) < #" & CDate(rs!MaxDate) & "#"
       
        CurrentDb.Execute strSQLDel, dbFailOnError
        rs.MoveNext
    Loop
        
    rs.Close
    Set rs = Nothing
    
'MsgBox "Duplication Check Completed"
End Function  

Open in new window

0
mato01Author Commented:
Here is a sample file?

test111.accdb
0
mbizupCommented:
This appears to work...

Function DeleteDupes()
    Dim strSQL As String
    Dim strSQLDel As String
    Dim rs As DAO.Recordset
    Dim arr() As String
    
   ' This query selects the max date, grouping by Constraint Number, only considering records that have duplicates

     strSQL = "SELECT  [Constraint Number] & '~' & [Allocation Group] AS CompFields, Count([Date Added]) AS RecCount, Max(CDate(NZ([Date Added],#1/1/1900#))) AS MaxDate FROM [tbl_GMNA Constraint Report Output]" _
             & " GROUP BY  [Constraint Number] & '~' & [Allocation Group]" _
             & " HAVING Count([Date Added]) > 1"
             
    Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenDynaset)
    If rs.RecordCount = 0 Then
        MsgBox "No dupes found"
        Exit Function
    End If
    
    'Loop through the recordset deleting duplicates with the same constraint number but lesser dates
    Do Until rs.EOF
        arr = Split(rs!CompFields, "~")
        strSQLDel = "DELETE * FROM [tbl_GMNA Constraint Report Output] WHERE " _
             & "[Constraint Number] = '" & arr(0) & "' " _
             & "AND [Allocation Group] = '" & arr(1) & "' " _
             & "AND CDate(NZ([Date Added],#1/1/1900#)) < #" & CDate(rs!MaxDate) & "#"
       
        CurrentDb.Execute strSQLDel, dbFailOnError
        rs.MoveNext
    Loop
        
    rs.Close
    Set rs = Nothing
    
'MsgBox "Duplication Check Completed"
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mato01Author Commented:
This solution seems to work.  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

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.