?
Solved

How can I count records in Access?

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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

719 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