Solved

How can I count records in Access?

Posted on 2008-10-30
3
608 Views
Last Modified: 2013-11-29
I have a file (sample attached) where I have a bunch of colors and an associate dstate.  I would like to make either a query or report where it would give me a count of what I have and where.  For example, it would tell me:

COLOR  STATE  Count
Blue        NJ        2
Red         NJ        1
Green     NJ        1
Blue        NY       1
Red        NY        1

How would I go about doing this?  Thanks!!!
Sample.mdb
0
Comment
Question by:daisypetals313
3 Comments
 
LVL 7

Accepted Solution

by:
pr0t0c0l12 earned 500 total points
ID: 22844845

DCount counts the number of records that meet criteria specified in the criteria:
     DCount("FieldToCount","Table or Query Name","Criteria")
Examples: The first example counts the number of records in the customer table that have a conatact name greater than S. The second counts contact names less than S and the third counts contact names that have S as the first letter.

     DCount("[ContactName]","Customers","[ContactName] > 'S'")
 
     DCount("[ContactName]","Customers","[ContactName] < 'S'")

     DCount("[ContactName]","Customers","[ContactName] Like 'S*'")
 
For additional information about a similar problem like yours please go to:
 
http://www.experts-exchange.com/Programming/Languages/SQL_Syntax/Q_23477184.html 
0
 

Author Closing Comment

by:daisypetals313
ID: 31511842
Thanks - I actually wind up figuring it out a different way just now.  I used the Crosstab Query feature and got what I needed, but thanks anyway for your help!
0
 
LVL 10

Expert Comment

by:LennyGray
ID: 22845068
Below is code that you can add to a module.

Or, you can paste this code below into a query by clicking on the "sql" button when the query is in design mode:

SELECT TheTableName.sTheState, TheTableName.sTheColor, Count(TheTableName.sTheColor) AS TheCount
FROM TheTableName
GROUP BY TheTableName.sTheState, TheTableName.sTheColor
ORDER BY TheTableName.sTheState, TheTableName.sTheColor;

In all cases, change the column names and table name to conform to your naming conventions.

Good Luck,
Lenny Gray


Public Sub CountTheColorsInTheState()

Dim sSqlString As String
 

    sSqlString = "SELECT TheTableName.sTheState, TheTableName.sTheColor, Count(TheTableName.sTheColor) AS TheCount "

    sSqlString = sSqlString & "FROM TheTableName "

    sSqlString = sSqlString & "GROUP BY TheTableName.sTheState, TheTableName.sTheColor "

    sSqlString = sSqlString & "ORDER BY TheTableName.sTheState, TheTableName.sTheColor"
 

    DoCmd.RunSQL sSqlString

    

End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

911 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

20 Experts available now in Live!

Get 1:1 Help Now