Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA DCount unique values in Access 2007

Posted on 2011-02-22
10
Medium Priority
?
1,086 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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
LVL 93

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
 
LVL 93

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Explore the ways to Unlock VBA Project Password Excel 2010 & 2013 documents. Go through the article and perform the steps carefully to remove VBA Excel .xls file.
If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

772 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