Rod:
I appreciate the suggestions; they were very helpful.
I found that the checkbox's control source is linked to
the IsDuplicate column of my query to find duplicates.
The query itself is structured as such:
SELECT
[tblLeave].[IsDuplicate],
[tblLeave].[EmployeeID],
[tblLeave].[DateBeginning],
[tblLeave].[DateEnding],
[tblLeave].[NumberOfHours],
[tblLeave].[UnionID],
[tblLeave].[Purpose],
[tblLeave].[Agency],
[tblEmployee].[LastName],
[tblEmployee].[FirstName]
FROM
tblEmployee
INNER JOIN
tblLeave
ON
[tblEmployee].[EmployeeID]=[tblLeave].[EmployeeID]
WHERE
(((tblLeave.EmployeeID)
In
(SELECT
[EmployeeID]
FROM
[tblLeave] As Tmp
GROUP BY
[EmployeeID],
[DateBeginning],
[DateEnding],
[NumberOfHours]
HAVING
Count(*)>1
And [DateBeginning] = [tblLeave].
[DateBeginning]
And [DateEnding] = [tblLeave].[DateEnding]
And [NumberOfHours] = [tblLeave].
[NumberOfHours])))
ORDER BY
[tblLeave].[EmployeeID],
[tblLeave].[DateBeginning],
[tblLeave].[DateEnding],
[tblLeave].[NumberOfHours];
My problem now is loading a recordset that I can change
and update. I want to load this new query into the
recordset, and then run a while loop, searching for
all duplicates ONLY (leaving the originals unchecked) and
have check each record or row that is a duplicate on the
form.
So far, I have written this code to load and compare
records in a recordset, but it won't allow me to make
changes to tblLeave, which has the IsDuplicate column.
How can I put this check in the checkbox of the individual
row?
Private Sub Form_Load()
Dim db As Database
Dim qdf As queryDef
Dim rs As Recordset
Dim OldKey As String
Dim NewKey As String
Dim SQLCall As String
SQLCall = "SELECT [tblLeave].[IsDuplicate], [tblLeave].
[EmployeeID], " _
& "[tblLeave].[DateBeginning], [tblLeave].
[DateEnding], " _
& "[tblLeave].[NumberOfHours], [tblLeave].[UnionID], "
_
& "[tblLeave].[Purpose], [tblLeave].[Agency],
[tblEmployee].[LastName], " _
& "[tblEmployee].[FirstName] FROM [tblEmployee] INNER
JOIN [tblLeave]" & _
"ON [tblEmployee].[EmployeeID] = [tblLeave].
[EmployeeID]" & _
"WHERE ((([tblLeave].[EmployeeID]) In (SELECT
[EmployeeID]" & _
"FROM [tblLeave] As Tmp GROUP BY [EmployeeID],
[DateBeginning], " _
& "[DateEnding],[NumberOfHours] HAVING Count(*)>1" & _
"And [DateBeginning] = [tblLeave].[DateBeginning]" & _
"And [DateEnding] = [tblLeave].[DateEnding]" & _
"And [NumberOfHours] = [tblLeave].[NumberOfHours])))"
& _
"ORDER BY [tblLeave].[EmployeeID], [tblLeave].
[DateBeginning], " _
& "[tblLeave].[DateEnding], [tblLeave].[NumberOfHours]"
'this will read data from the current database UNION into
variable rs
Set db = CurrentDb
Set qdf = db.CreateQueryDef("qryDuplicate", SQLCall)
With qdf
Set rs = .OpenRecordset(dbOpenDynamic)
End With
rs.MoveFirst
NewKey = rs.Fields("EmployeeID").Value & "; " & rs.Fields
("DateBeginning").Value & _
"; " & rs.Fields("DateEnding").Value & "; " & rs.Fields
("NumberOfHours")
If rs.RecordCount = 1 Then
OldKey = " "
End If
'compare each record of the table to OldKey
Do While Not rs.EOF
'if oldkey = newkey, then it is a duplicate record, check
the duplicates ONLY, and then
'make oldkey the next record to compare to next record
If OldKey = NewKey Then
With rs
'rs.Edit
rs.Fields("IsDuplicate").Value = True
'rs.Update ("IsDuplicate")
End With
'DoCmd.RunSQL "UPDATE [tblLeave] SET [IsDuplicate] = TRUE"
OldKey = NewKey
rs.MoveNext
If rs.EOF = True Then
Exit Sub
Else
NewKey = rs.Fields("EmployeeID").Value & "; "
& rs.Fields("DateBeginning").Value & _
"; " & rs.Fields("DateEnding").Value & "; " & rs.Fields
("NumberOfHours")
End If
'if oldkey does not equal newkey, then it is not a
duplicate record and must check with
'the rest of the records in the table
Else
OldKey = NewKey
rs.MoveNext
If rs.EOF = True Then
Exit Sub
Else
NewKey = rs.Fields("EmployeeID").Value & "; "
& rs.Fields("DateBeginning").Value & _
"; " & rs.Fields("DateEnding").Value & "; " & rs.Fields
("NumberOfHours")
End If
End If
Loop
End Sub