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, 5 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
-
Microsoft: Troubleshoot problems updating Windows
by
Alex5723
56 minutes ago -
Woman Files for Divorce After ChatGPT “Reads” Husband’s Coffee Cup
by
Alex5723
1 hour, 5 minutes ago -
Moving fwd, Win 11 Pro,, which is best? Lenovo refurb
by
Deo
1 hour, 11 minutes ago -
DBOS Advanced Network Analysis
by
Kathy Stevens
10 hours, 54 minutes ago -
Microsoft Edge Launching Automatically?
by
healeyinpa
1 hour, 19 minutes ago -
Google Chrome to block admin-level browser launches for better security
by
Alex5723
13 hours, 35 minutes ago -
iPhone SE2 Stolen Device Protection
by
Rick Corbett
5 hours, 50 minutes ago -
Some advice for managing my wireless internet gateway
by
LHiggins
1 hour, 31 minutes ago -
NO POWER IN KEYBOARD OR MOUSE
by
HE48AEEXX77WEN4Edbtm
7 hours, 47 minutes ago -
A CVE-MITRE-CISA-CNA Extravaganza
by
Nibbled To Death By Ducks
23 hours, 4 minutes ago -
Sometimes I wonder about these bots
by
Susan Bradley
19 hours, 21 minutes ago -
Does windows update component store “self heal”?
by
Mike Cross
9 hours, 24 minutes ago -
Windows 11 Insider Preview build 27858 released to Canary
by
joep517
1 day, 13 hours ago -
Pwn2Own Berlin 2025: Day One Results
by
Alex5723
1 day, 12 hours ago -
Windows 10 might repeatedly display the BitLocker recovery screen at startup
by
Susan Bradley
39 minutes ago -
Windows 11 Insider Preview Build 22631.5409 (23H2) released to Release Preview
by
joep517
1 day, 15 hours ago -
Windows 10 Build 19045.5912 (22H2) to Release Preview Channel
by
joep517
1 day, 15 hours ago -
Kevin Beaumont on Microsoft Recall
by
Susan Bradley
1 day, 4 hours ago -
The Surface Laptop Studio 2 is no longer being manufactured
by
Alex5723
1 day, 23 hours ago -
0Patch, where to begin
by
cassel23
1 day, 17 hours ago -
CFPB Quietly Kills Rule to Shield Americans From Data Brokers
by
Alex5723
2 days, 13 hours ago -
89 million Steam account details just got leaked,
by
Alex5723
2 days, 1 hour ago -
KB5058405: Linux – Windows dual boot SBAT bug, resolved with May 2025 update
by
Alex5723
2 days, 22 hours ago -
A Validation (were one needed) of Prudent Patching
by
Nibbled To Death By Ducks
2 days, 13 hours ago -
Master Patch Listing for May 13, 2025
by
Susan Bradley
2 days ago -
Installer program can’t read my registry
by
Peobody
16 hours, 57 minutes ago -
How to keep Outlook (new) in off position for Windows 11
by
EspressoWillie
2 days, 10 hours ago -
Intel : CVE-2024-45332, CVE-2024-43420, CVE-2025-20623
by
Alex5723
2 days, 18 hours ago -
False error message from eMClient
by
WSSebastian42
3 days, 9 hours ago -
Awoke to a rebooted Mac (crashed?)
by
rebop2020
3 days, 18 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.