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
-
Acronis : Tracking Chaos RAT’s evolution (Windows, Linux)
by
Alex5723
4 hours, 11 minutes ago -
Turning off OneDrive
by
CWBillow
8 hours, 40 minutes ago -
June 2025 updates are out
by
Susan Bradley
44 minutes ago -
Mozilla shutting Deep Fake Detector
by
Alex5723
18 hours, 57 minutes ago -
Windows-Maintenance-Tool (.bat)
by
Alex5723
4 hours, 20 minutes ago -
Windows 11 Insider Preview build 26200.5641 released to DEV
by
joep517
21 hours, 30 minutes ago -
Windows 11 Insider Preview build 26120.4250 (24H2) released to BETA
by
joep517
21 hours, 31 minutes ago -
Install Office 365 Outlook classic on new Win11 machine
by
WSrcull999
21 hours, 29 minutes ago -
win 10 to win 11 with cpu/mb replacement
by
aquatarkus
13 hours, 21 minutes ago -
re-install Windows Security
by
CWBillow
1 day ago -
WWDC 2025 Recap: All of Apple’s NEW Features in 10 Minutes!
by
Alex5723
1 day, 4 hours ago -
macOS Tahoe 26
by
Alex5723
22 hours, 40 minutes ago -
Migrating from win10 to win11, instructions coming?
by
astro46
10 hours, 45 minutes ago -
Device Eligibility for Apple 2026 Operating Systems due this Fall
by
PKCano
13 hours, 13 minutes ago -
Recommended watching : Mountainhead movie
by
Alex5723
13 hours, 57 minutes ago -
End of support for Windows 10
by
Old enough to know better
13 hours, 53 minutes ago -
What goes on inside an LLM
by
Michael Covington
8 hours, 6 minutes ago -
The risk of remote access
by
Susan Bradley
3 hours, 46 minutes ago -
The cruelest month for many Office users
by
Peter Deegan
20 hours, 9 minutes ago -
Tracking protection and trade-offs in Edge
by
Mary Branscombe
18 hours, 11 minutes ago -
Supreme Court grants DOGE access to confidential Social Security records
by
Alex5723
2 days, 2 hours ago -
EaseUS Partition Master free 19.6
by
Alex5723
1 day, 3 hours ago -
Microsoft : Edge is better than Chrome
by
Alex5723
2 days, 16 hours ago -
The EU launched DNS4EU
by
Alex5723
3 days, 4 hours ago -
Cell Phone vs. Traditional Touchtone Phone over POTS
by
280park
2 days, 19 hours ago -
Lost access to all my networked drives (shares) listed in My Computer
by
lwerman
3 days, 10 hours ago -
Set default size for pasted photo to word
by
Cyn
3 days, 16 hours ago -
Dedoimedo tries 24H2…
by
Cybertooth
3 days, 4 hours ago -
Windows 11 Insider Preview build 27871 released to Canary
by
joep517
4 days, 15 hours ago -
Windows 11 ad from Campaign Manager in Windows 10
by
Jim McKenna
2 days, 8 hours 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.