+ Reply to Thread
Results 1 to 26 of 26

CountIf in Access - HELP

  1. #1
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Angry CountIf in Access - HELP

    I need to do countifs and sumifs in access and cannot find a solution.

    I need to count how many times a value in one field of one table appears in the field of another table. I've tried several things but keep getting errors. What I am trying to do is exactly what a COUNTIF does in Excel but in Access.

    How many times does the order number in Table 1, Order No. field appear in Table 2 Order No. field?

    Table 1

    Order No. Order Count
    ABCD 3
    1234 2
    EFGH 1
    5678 2
    IHJK 4
    91011 5

    Table 2

    Order Number
    ABCD
    ABCD
    ABCD
    1234
    1234
    EFGH
    5678
    5678
    IHJK
    IHJK
    IHJK
    IHJK
    91011
    91011
    91011
    91011
    91011

  2. #2
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    You can just use Group by in query.

    Ex:
    Please Login or Register  to view this content.
    Result:
    0.JPG
    ?Progress isn't made by early risers. It's made by lazy men trying to find easier ways to do something.?
    ― Robert A. Heinlein

  3. #3
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    Your formula doesn't reference table 1 so I'm confused.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,851

    Re: CountIf in Access - HELP

    Same results using this SQL statement

    Please Login or Register  to view this content.
    There is no need to reference table 1 as all the data is in table 2 and is counted using the group by functionality
    Attached Images Attached Images
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  5. #5
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    Not a formula, it's a query. Since Table1 in your sample was just Distinct records from Table2. No need to reference Table1.

    If you want to check with Table1, one way to do it is use nested selects.

    I.E. Select Order Number from Table1. And Left Join on Order Number of the query I posted.

    Since Firewall is preventing me from posting query... see attached text file and image.

    1.JPG
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    I guess I wasn't clear. Table 2 has many order numbers that do not exist in table 1. So I only need to count how many of the orders from Table 1 that appear in Table 2.

  7. #7
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    Alternately you can write it as...
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    Thank you very much. Do SUMIFs work similarly? I'm more excel oriented so the cross-over from one to the other gets fuzzy to me.

  9. #9
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,851

    Re: CountIf in Access - HELP

    Yes. Here is a link to Group By so that you will have a better understanding

    https://www.techonthenet.com/sql/group_by.php

  10. #10
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    Actually it's not working at all for me, but rather deleting every record from my table 1.

    Table 1 has a column called "Order Number" (each value is different) there is a column beside it called "Order Count". I need the Order Count field to reflect the number of times the values from the Order Number field in table 1 are found in the Order number field in table 2.

  11. #11
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    Don't use calculated field if you can help it.

    What you are after is derived record from existing tables. Hence, our recommendation to use query to return records.

    Use query designer, and then go to SQL view and paste in the query (replace column/table reference as needed).

    And save the query. You are not altering the existing table at all.

  12. #12
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    I'm not sure what you are referencing with the last post. I just used the formula for SQL and it deleted all my records (I changed the table names and field names as necessary). I'm just trying to do a simple COUNTIF between to columns of data as if I were using Excel and for 2 days haven't been able to find a way to do it. It shouldn't be this difficult.

  13. #13
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    You are thinking like Excel. But this is database.
    There are some limited case where Calculated field is used in database, but not many.
    Have a read of...
    https://www.datanumen.com/blogs/one-...access-tables/
    https://btabdevelopment.com/why-you-...in-the-tables/

    Also, I don't think Access supports calculated field using another table.

  14. #14
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    I'm not trying to use a calculated field. I'm simply trying to perform a countif from table to another.

  15. #15
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,851

    Re: CountIf in Access - HELP

    To help you understand a Data Base, here is a reference that will help you to understand how to set up and manage.

    http://www.accessmvp.com/strive4peace/

  16. #16
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    So the query I showed does that...
    It does not alter existing tables at all.

    Here's steps.
    Go to Create Ribbon tool -> Query Design -> Close the dialog window.
    Go to SQL View (in Results section of ribbon) and select SQL View.
    Paste in the query (from post #7).
    Hit Run button.

    You will see result like below.
    0.JPG

    If you want to name Expr1001 as Order Count. Alter query to...
    Please Login or Register  to view this content.
    And save query.

    See sample db attached.
    Attached Files Attached Files

  17. #17
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,851

    Re: CountIf in Access - HELP

    Here is a database zip file that replicates your tables and a query that sums the values that CK and I have been trying to explain to you.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    Is there a way to do this that you know of? I need to count how many times the values in one field from one table, appear in another field in another table.

  19. #19
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    But "why" is the question here. It's really bad practice to store derived/calculated value in a database table.

    Hence, we've been trying to show you how to do it using query.

    What end result (or process) that you are after, makes it necessary for you to store calculated value in a table?

  20. #20
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    Yes. It will in fact be a query. I have built multiple queries for this project within this database.

    For this specific query, I need it to function as a countif from one table to another.

    Table 1 has 2 columns:

    Order No Order Count


    Table 2 has 1 column:

    Order No


    I need it to count (and show the result of that count in the "Order Count" field of Table 1) the number of times the value in the "Order No" field in Table 1, appears in the "Order No' field in Table 2.

    I don't know if I am over complicating my explanation but if this were an Excel formula it would be quite simple. I just can't seem to find a way to do this in Access and I've searched for days.

  21. #21
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    You already have that in previous posts.

  22. #22
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    It isn't working for one reason or another, but rather deleting all of the values in my table. I have replaced the table names and field names as necessary.

  23. #23
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    but rather deleting all of the values in my table
    I'm guessing that by above, you mean that query isn't returning any data. If that's the case, it means that no item in Table 1 match "EXACTLY" to items in Table 2.

    The query syntax is correct and works when there are matching Order Number found on both tables (as demonstrated by two sample db files).
    I'd recommend you query each table independently and export it out to csv, and compare returned strings to ensure that there are exact matches found.

    Oh and if you want to retain ALL items from Table 1. You should use Left Join instead of Inner Join.

  24. #24
    Registered User
    Join Date
    05-21-2019
    Location
    SC, USA
    MS-Off Ver
    2013
    Posts
    20

    Re: CountIf in Access - HELP

    That simply isn't the case. the values in table 1 are all in table 2. I am just trying to count how many times they appear in table 2. But the query, as presented, is deleting my data, not counting it. I will look elsewhere for an answer. Thanks for trying.

  25. #25
    Forum Expert CK76's Avatar
    Join Date
    06-16-2015
    Location
    ONT, Canada
    MS-Off Ver
    Office 365 ProPlus
    Posts
    5,877

    Re: CountIf in Access - HELP

    . But the query, as presented, is deleting my data
    This simply cannot happen with queries we provided. As query only returns record set that matches criteria, and does not alter source tables in any way.

    Best of luck finding your answer.

  26. #26
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,851

    Re: CountIf in Access - HELP

    I agree with CK. I am guessing that while you believe that you have matching data, you really don't. Capitalization of one and the other could be an issue. Spaces before or after one set of values may be the issue. Your data may not be formatted in the same manner. We have attached sample data showing you that it does work. You have only told us it does not work. Why not show us your database with the two tables. Put only a representative amount of records in each. Zip the file and upload to this site by clicking on the Go Advanced button at the bottom of the reply window. Follow instructions after scrolling down to Manage Attachments.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. List of Access database paths in Excel - Need to return Access version
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-29-2019, 12:19 PM
  2. Running an access append query with parameters from Excel. Access version 2010
    By anthony1312002 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-14-2017, 04:48 PM
  3. Double Handling within working code Write to access and fire access macro
    By robtuby in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-11-2015, 12:19 PM
  4. Access form works with access Viewer but not full version of Access?
    By Shanyn in forum Access Tables & Databases
    Replies: 1
    Last Post: 10-22-2014, 01:14 PM
  5. Excel 2010 userform to access and edit the data in MS ACCESS DB tables
    By anand_y59 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-10-2012, 07:46 AM
  6. [SOLVED] What is the Access equivalent of Excel's COUNTIF?
    By RedStep in forum Excel General
    Replies: 1
    Last Post: 04-06-2005, 06:06 PM
  7. Excel countif based on size of numbers in one column in Access
    By Kristjan_Thor in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 03-17-2005, 07:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1