Solved

VBA DCount unique values in Access 2007

Posted on 2011-02-22
10
1,054 Views
Last Modified: 2012-05-11
I have a DCount function that counts a particular people field in a table that satisfy some criteria. It returns the total number of records in that table. To clarify (!?), there are three records with "fred" in that people field, two with "bill". The Dcount return 5, ie all the Freds and the Bills. I want it to tell me that there are 2 different people, regardless that there are three instances of Fred and two of Bill. So I am looking for a count of unique values.

Can this be done in DCount?

Many thanks.
0
Comment
Question by:Peborgh
  • 5
  • 3
  • 2
10 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 34950321
I cover getting distinct counts in Access in my article http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_2417-Calculating-Distinct-Counts-in-Access.html


For example, to get distinct counts with no criteria:


SELECT Count(y.People) AS DistinctCount
FROM
    (SELECT DISTINCT x.People
    FROM SomeTable x) AS y

Open in new window



With criteria:

SELECT Count(y.People) AS DistinctCount
FROM
    (SELECT DISTINCT x.People
    FROM SomeTable x
    WHERE Status = "Active" And AsOfDate >= #1/1/2008#) AS y

Open in new window

0
 
LVL 77

Expert Comment

by:peter57r
ID: 34950330
You cannot do this just in a Dcount().
You need a query to get the distinct values first and then you can count them.

For example..  qryNames =

Select distinct fullname from mytable

Then you can use
Dcount("*", "qryNames")
0
 

Author Comment

by:Peborgh
ID: 34950610
matthewspatrick,

You have just what I wanted: DcountDistinct. I will try it before week's end and let you know.

Thank you,

peter
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34950921
That would work too :)
0
 

Author Comment

by:Peborgh
ID: 34953095
matthewspatrick,

I am getting #Error out of DCountDistinct.

I then noticed that it required a reference to DAO (3.6 in my case). When I try to add this to references, I get name conflicts and the reference does not install. Funny because my app had DOA.recordsets all over the place before DCountDistinct ever arrived.

Btw, If I remove the "DAO." from your recordset declaration, that makes no difference either to the #Error.

Any thoughts?

Thanks,

peter
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 34954042
Peter,

Can you post a sample file?  Please use the MDB format if you do--the computer I have with me has Access 2003.

Patrick
0
 

Author Comment

by:Peborgh
ID: 34959017
Very difficult as the whole thing is rather complex (yes I know!). Can you venture any suggestions without the actual code?

Thanks,

peter
0
 
LVL 77

Expert Comment

by:peter57r
ID: 34959223
You must leave the DAO.  prefixes in place.

A2007 doesn't use DAO3.6  -  it uses the Access database engine instead.
You will already have a reference set for that.  You do not need the DAO3.6 library.

You should create a query which does ONLY the "select Distinct...." part first (the bit in brackets) and make sure that is working.  Then you can create a new query using the first query which counts the records.

If you still have issues you should post the sql of the query(ies) you are using.
0
 

Author Comment

by:Peborgh
ID: 34971422
matthewspatrick,

Problem solved. The #ERROR was because I had the multi-word name of the table without []s. In my ordinary Dcount it accepted "blah de blah". DCountDistinct required "[blah de blah]".

Now all is well... Thanks,

peter
0
 

Author Closing Comment

by:Peborgh
ID: 35009087
the DCountDistinct worked fine, once I had the table name in brackets...
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now