Results 1 to 8 of 8
  1. #1
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32

    Using a query to sum the number of times a string appears in a field


    How do I create a query to count the number of times a string appears in a field? When I try to count the number of times a string appears making the criteria the string, I get an error message that reads "data type mismatch in criteria expression."

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Depends what you mean by 'number of times a string appears in a field'.

    Is this how many times a string appears in a field for the same record? This would require a VBA custom function.

    Or how many records have a given string in a field? This would be an aggregate (GROUP BY) Totals query. Maybe:
    SELECT Count(*) AS CountRecords WHERE [fieldname] LIKE "*" & "some text" & "*";
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    I would like to count the number of times a string appears in a field for a different record but same table.

  4. #4
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    Quote Originally Posted by June7 View Post
    Depends what you mean by 'number of times a string appears in a field'.

    Is this how many times a string appears in a field for the same record? This would require a VBA custom function.

    Or how many records have a given string in a field? This would be an aggregate (GROUP BY) Totals query. Maybe:
    SELECT Count(*) AS CountRecords WHERE [fieldname] LIKE "*" & "some text" & "*";
    I tried the Query you recommended and it said that WHERE was misspelled.

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Ooops! I forgot: FROM tablename.

    I also forgot apostrophe delimiters.

    The following will trigger a parameter input prompt popup.

    SELECT Count(*) AS CountRecords FROM tablename WHERE [fieldname] LIKE "'*" & [enter some text] & "*'";
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    Thanks! it worked!

  7. #7
    radguy is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2014
    Posts
    32
    Actually it didn't work for me.

    Here is the problem... I have a table with records that have the same [tool location],as you can see from the screen shot.

    .Click image for larger version. 

Name:	Tool ID Table.JPG 
Views:	5 
Size:	79.6 KB 
ID:	17603 Click image for larger version. 

Name:	Tool ID Datasheet view.JPG 
Views:	5 
Size:	97.3 KB 
ID:	17604

    I entered what you told me to enter for the SQL:
    Click image for larger version. 

Name:	Query of Quantity..JPG 
Views:	5 
Size:	67.2 KB 
ID:	17605 Click image for larger version. 

Name:	Query of Quantity..JPG 
Views:	5 
Size:	67.2 KB 
ID:	17605

    There are 2 Blade 1s that are in storage but it only counts 1. How can I fix it?

    Click image for larger version. 

Name:	1 Blade 1s.JPG 
Views:	5 
Size:	30.8 KB 
ID:	17606

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    You have the query filtered for only the "Storage" records so it counts only those records. Only one of them is in "Storage".
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 06-10-2014, 11:50 AM
  2. Replies: 3
    Last Post: 12-01-2012, 12:37 PM
  3. Count How Many times a value appears in a table
    By younggunnaz69 in forum Queries
    Replies: 1
    Last Post: 10-19-2011, 10:29 PM
  4. report the # of times a name appears in a field
    By rankhornjp in forum Reports
    Replies: 25
    Last Post: 08-04-2011, 01:45 PM
  5. Replies: 3
    Last Post: 08-24-2010, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums