Solved

How can I count records in Access?

Posted on 2008-10-30
3
607 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

10 Experts available now in Live!

Get 1:1 Help Now