I was wondering if there is a way to, in either a query or report, count the number of records in a column while only counting duplicate records once.
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
-
Count Duplicate Records Once (Access 2000)
Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Count Duplicate Records Once (Access 2000)
- This topic has 4 replies, 4 voices, and was last updated 22 years, 6 months ago.
Viewing 1 reply threadAuthorReplies-
WSSteve_in_Kent
AskWoody LoungerDecember 11, 2002 at 9:32 pm #637939I’ve done this myself with 2 queries. just setting the second query to count.
see this post :-
“]http://www.wopr.com/cgi-bin/w3t/showthread…p;vc=1#Post5717%5D%5B/url%5DRegards
-
WSMarkD
AskWoody LoungerDecember 12, 2002 at 1:24 pm #637968(Edited by charlotte on 12-Dec-02 06:24. to Remark out notifications in error handler)
One way to do this w/o having to create multiple queries is create user-defined function to count unique records for specified field in a table. Example – add this function to a standard code module:
Public Function CountUniqueRecords(strTbl As String, strFld As String) As Long
On Error GoTo Err_HandlerDim strMsg As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As StringSet db = CurrentDb
strSQL = “SELECT DISTINCT [” & strFld & “] FROM [” & strTbl & “];”
Set rst = db.OpenRecordset(strSQL)With rst
If Not .EOF Then
.MoveLast
End If
CountUniqueRecords = .RecordCount
End WithExit_Sub:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Set db = Nothing
Exit Function
Err_Handler:
strMsg = “Error No ” & Err.Number & “: ” & Err.Description
‘ Beep
‘ MsgBox strMsg, vbExclamation, “ERROR MESSAGE”
Resume Exit_SubEnd Function
Example of use in query (using Order Details table in Northwind.mdb):
SELECT Count([Order Details].ProductID) AS [Count All], CountUniqueRecords(“Order Details”,”ProductID”) AS [Count Unique]
FROM [Order Details];This totals query returns 1 row, showing total count of ProductID field in 1st column and count of unique ProductID’s in 2nd column. (In my copy of Northwind these totals were 2155 and 77, respectively.) Note if function used in a select query, it’ll be executed for each row returned by query.
Example of use in report: Add an unbound textbox to report and set its ControlSource to following expression (using same example):
=CountUniqueRecords(“Order Details”,”ProductID”)
When report runs textbox will display “77” (in this case). You would replace table and field names with actual table & field names in your database. If you want to count unique records with Where criteria applied the CountUniqueRecords function would have to be modified accordingly. If using A2K or later ensure a reference to Microsoft DAO 3.6 Object Library has been set.
HTH
-
WSHansV
AskWoody LoungerDecember 12, 2002 at 1:19 pm #638023Great function, Mark. Thanks!
Here is a version that takes an optional Where condition:
‘ *** Optional argument strCnd added ***
Public Function CountUniqueRecords(strTbl As String, strFld As String, _
Optional strCnd As String) As LongOn Error GoTo Err_Handler
Dim strMsg As String
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As StringSet db = CurrentDb
strSQL = “SELECT DISTINCT [” & strFld & “] FROM [” & strTbl & “]”
‘ *** Modification starts ***
If strCnd “” Then
strSQL = strSQL & ” WHERE ” & strCnd
End If
‘ *** Modification ends ***
Set rst = db.OpenRecordset(strSQL)With rst
If Not .EOF Then
.MoveLast
End If
CountUniqueRecords = .RecordCount
End WithExit_Sub:
If Not rst Is Nothing Then rst.Close
Set rst = Nothing
Set db = Nothing
Exit FunctionErr_Handler:
Beep
‘ See remark about displaying a message box below
‘ strMsg = “Error No ” & Err.Number & “: ” & Err.Description
‘ MsgBox strMsg, vbExclamation, “ERROR MESSAGE”
Resume Exit_Sub
End FunctionNote: I am not sure that having a MsgBox in the error handler is a good idea here. If an error occurs in the query you give as example, you’ll probably have to click OK 2155 times…
MarkD’s examples will work with the modified function; if the strCnd argument is omitted, the function acts as Mark’s original function.
Example of use with Where condition in a query based on the Products table in the Northwind database:
SELECT Products.CategoryID, CountUniqueRecords(“Products”,”SupplierID”,”CategoryID=” & [CategoryID]) AS CountOfSuppliers
FROM Products
GROUP BY Products.CategoryID;Example of use with Where condition in a report: create a report based on the Products table, grouped by CategoryID. Place an unbound text box in the group footer and set its Control Source to
=CountUniqueRecords(“Products”,”SupplierID”,”CategoryID=” & [CategoryID])
-
WSMarkD
AskWoody LoungerDecember 12, 2002 at 10:56 am #638027You’re correct about the MsgBox in error handler – not a good idea if used in select query that may list many records. I didn’t think to modify generic error handler I use for new subs. Function is really intended for a single-row totals query as shown in example posted. Having many times wound up clicking “OK” over & over again when function in query generated error I should’ve caught this!
-
-
Viewing 1 reply thread -

Plus Membership
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Get Plus!
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Search Newsletters
Search Forums
View the Forum
Search for Topics
Recent Topics
-
Extended Windows Built-in Disk Cleanup Utility
by
bbearren
29 minutes ago -
Win 11 24H2 June 2025 Update breaks WIFI
by
dportenlanger
10 hours, 50 minutes ago -
Update from WinPro 10 v. 1511 on T460p?
by
CatoRenasci
22 minutes ago -
System Restore and Updates Paused
by
veteran
13 hours, 20 minutes ago -
Windows 10/11 clock app
by
Kathy Stevens
26 minutes ago -
Turn off right-click draw
by
Charles Billow
16 hours, 34 minutes ago -
Introducing ChromeOS M137 to The Stable Channel
by
Alex5723
20 hours, 5 minutes ago -
Brian Wilson (The Beach Boys) R.I.P
by
Alex5723
3 hours, 10 minutes ago -
Master patch listing for June 10, 2025
by
Susan Bradley
21 hours, 42 minutes ago -
Suggestions for New All in One Printer and a Photo Printer Windows 10
by
Win7and10
34 minutes ago -
Purchasing New Printer. Uninstall old Printer Software First?
by
Win7and10
1 day, 3 hours ago -
KB5060842 Issue (Minor)
by
AC641
1 day, 8 hours ago -
EchoLeak : Zero Click M365 Copilot leak sensitive information
by
Alex5723
1 day, 10 hours ago -
24H2 may not be offered June updates
by
Susan Bradley
3 hours, 25 minutes ago -
Acronis : Tracking Chaos RAT’s evolution (Windows, Linux)
by
Alex5723
1 day, 23 hours ago -
June 2025 updates are out
by
Susan Bradley
11 hours, 40 minutes ago -
Mozilla shutting Deep Fake Detector
by
Alex5723
2 days, 14 hours ago -
Windows-Maintenance-Tool (.bat)
by
Alex5723
1 day, 23 hours ago -
Windows 11 Insider Preview build 26200.5641 released to DEV
by
joep517
2 days, 16 hours ago -
Windows 11 Insider Preview build 26120.4250 (24H2) released to BETA
by
joep517
2 days, 16 hours ago -
Install Office 365 Outlook classic on new Win11 machine
by
WSrcull999
2 days, 16 hours ago -
win 10 to win 11 with cpu/mb replacement
by
aquatarkus
2 days, 8 hours ago -
re-install Windows Security
by
CWBillow
2 days, 19 hours ago -
WWDC 2025 Recap: All of Apple’s NEW Features in 10 Minutes!
by
Alex5723
2 days, 23 hours ago -
macOS Tahoe 26
by
Alex5723
2 days, 17 hours ago -
Migrating from win10 to win11, instructions coming?
by
astro46
8 hours, 32 minutes ago -
Device Eligibility for Apple 2026 Operating Systems due this Fall
by
PKCano
2 days, 8 hours ago -
Recommended watching : Mountainhead movie
by
Alex5723
2 days, 9 hours ago -
End of support for Windows 10
by
Old enough to know better
7 hours, 56 minutes ago -
What goes on inside an LLM
by
Michael Covington
8 hours, 53 minutes ago
Recent blog posts
Key Links
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.