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

x
?
Solved

How can I count records in Access?

Posted on 2008-10-30
3
Medium Priority
?
619 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 1500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

916 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