Function fncRemoveDupes(strTable As String, strFieldName As String, strFieldID As String, strOtherTables As String)
Dim rst As DAO.Recordset, rstDupe As DAO.Recordset
Dim i As Integer, numTables As Integer, strCleanTable As String
Dim strSQL As String, qdf As DAO.QueryDef
Dim dbLocal As DAO.Database
Dim strDupe As Variant
Set dbLocal = CurrentDb()
strSQL = "DELETE * FROM tblREMOVEDUPES"
Set qdf = dbLocal.CreateQueryDef("", strSQL)
qdf.Execute
Set rst = dbLocal.OpenRecordset(strTable, dbOpenTable)
If rst.RecordCount = 0 Then Exit Function
MsgBox "Starting process"
i = 0
Do Until rst.EOF
strDupe = Nz(rst(strFieldName).Value, "")
Set rstDupe = dbLocal.OpenRecordset("SELECT * FROM " & strTable & " WHERE " & strFieldName & " = """ & strDupe & """ AND " & strFieldID & " <> " & rst(strFieldID).Value & " AND " & strFieldID & " NOT IN (SELECT DupeID FROM tblREMOVEDUPES) AND " & strFieldID & " NOT IN(SELECT MainID FROM tblRemoveDupes)")
i = i + 1
Debug.Print "Row[" & i & "] dedups[" & rstDupe.RecordCount & "]"
If rstDupe.RecordCount <> 0 Then
Do Until rstDupe.EOF
strSQL = "INSERT INTO tblREMOVEDUPES (MainID, DupeID) VALUES (" & rst(strFieldID).Value & ", " & rstDupe(strFieldID).Value & ")"
Set qdf = dbLocal.CreateQueryDef("", strSQL)
qdf.Execute
rstDupe.MoveNext
Loop
End If
rst.MoveNext
Loop
MsgBox "done with de dupe search"
' Remove duplicates from other tables
numTables = fncParseString(strOtherTables, 1, ";", True)
If numTables = 0 Then numTables = 1
For i = 1 To numTables
strCleanTable = fncParseString(strOtherTables, i, ";")
Set rstDupe = dbLocal.OpenRecordset("tblREMOVEDUPES")
Do Until rstDupe.EOF
strSQL = "UPDATE " & strCleanTable & " SET " & strFieldID & " = " & rstDupe!MainID & " WHERE " & strFieldID & " = " & rstDupe!DupeID
Set qdf = dbLocal.CreateQueryDef("", strSQL)
[highlight]qdf.Execute[/highlight]
rstDupe.MoveNext
Loop
Next
' remove duplicates from the main table
If rstDupe.RecordCount = 0 Then Exit Function
rstDupe.MoveFirst
Do Until rstDupe.EOF
strSQL = "DELETE * FROM " & strTable & " WHERE " & strFieldID & " = " & rstDupe!DupeID
Set qdf = dbLocal.CreateQueryDef("", strSQL)
qdf.Execute
rstDupe.MoveNext
Loop
MsgBox "all done"
Exit Function
err_dupes:
MsgBox Err.Description
End Function
Function fncParseString(ByVal strToParse As String, numElement As Integer, Optional strSeparator As String, Optional ReturnCount As Boolean) As String
' Returns the numElement of the strToParse using the strSeparator (default is ';')
' if the ReturnCount flag is true, then the value returned is a string value of the actual number of elements in the "array"
Dim numSep As Integer, strTempL As String, strTempR As Variant
Dim i As Integer, strToReturn() As String
i = 0
If Nz(strSeparator) = "" Then strSeparator = ";"
If Not InStr(1, strToParse, strSeparator) > 0 Then
ReDim strToReturn(1)
strToReturn(i) = strToParse
If ReturnCount = True Then
fncParseString = i
Else
fncParseString = strToReturn(i)
End If
Exit Function
End If
Do While InStr(1, strToParse, strSeparator)
numSep = InStr(1, strToParse, strSeparator)
strTempL = Left(strToParse, numSep - 1)
strTempR = Right(strToParse, Len(strToParse) - (numSep + (Len(strSeparator) - 1)))
strToParse = strTempR
ReDim Preserve strToReturn(i)
strToReturn(i) = strTempL
If i = numElement And Not ReturnCount = True Then Exit Do
i = i + 1
Loop
If Nz(strToParse) = "" Then strToParse = ""
ReDim Preserve strToReturn(i)
strToReturn(i) = strToParse
If ReturnCount Then
fncParseString = Str(UBound(strToReturn) + 1)
ElseIf numElement > UBound(strToReturn) + 1 Then
fncParseString = "Err: Out of Range"
Else
fncParseString = strToReturn(numElement - 1)
End If
End Function