Solved

How can I count records in Access?

Posted on 2008-10-30
3
613 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Form performance table vs. query 27 71
MS Access query 16 61
CISCO WIFI 6 73
Outlook 2016 VB code syntax 4 38
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

751 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