N
nl
How do I highlight duplicate records in a report?
Thank you.
Thank you.
Firstly it depends on what is meant by 'duplicate'. Does it mean the value
in one column is repeated in two or more rows? Does in mean the values in
all columns are repeated? Or des it mean the values in a subset of columns
are repeated?
Taking as an example a report based of contacts, in which 'duplication' means
having the same FirstName and LastName values, and the RecordSource of the
report is a query named qryContacts. The query can have parameters
referencing a control or controls on a form to restrict its results, but not
a simple parameter such as [Enter company:].
You can determine if there are two or more rows with identical FirstName and
LastName values by calling the DCount function in the report's detail
section's Format event procedure and changing a property such as each
control's ForeColor property if it returns a value greater than 1. The
following for instance would show the 'duplicates' in red, the rest in black:
Dim strCriteria As String
Dim ctrl As Control
strCriteria = "LastName = """ & Me.LastName & _
""" And FirstName = """ & Me.FirstName & """"
If DCount("*", "qryContacts", strCriteria) > 1 Then
For Each ctrl In Me.Section(acDetail).Controls
ctrl.ForeColor = vbRed
Next ctrl
Else
For Each ctrl In Me.Section(acDetail).Controls
ctrl.ForeColor = vbBlack
Next ctrl
End If
If the report is filtered by means of the WhereCondition argument of the
OpenReport method then you'd also have to pass the string expression used to
filter the report into it, which can be done as the OpenArgs argument of the
OpenReport method. This can then be included in the criterion for the DCount
function, e.g.
strCriteria = "LastName = """ & Me.LastName & _
""" And FirstName = """ & Me.FirstName & """"
If Not IsNull(Me.OpenArgs) Then
strCriteria = strCriteria & " And " & Me.OpenArgs
End If
Ken Sheridan
Stafford, England
I do want the duplicates records in the Report, but highlight them in the
Report.