Solved

Count instances of comma in a string

Posted on 2014-03-25
4
671 Views
Last Modified: 2014-03-25
I have strings like this:
"236 A 100, 236 A 100 B, A 100, A 100 R, A 100 S"

I would like to filter in access query grid for strings containing more than two comma's.

So above would get shown:

"236 A 100, 236 A 100 B" this would not.
0
Comment
Question by:PeterBaileyUk
4 Comments
 
LVL 20

Accepted Solution

by:
clarkscott earned 167 total points
ID: 39952787
I'd create a function.  This function splits your text into "sections".  You can count the sections. Since the array will start at zero, the numer that's actually returned should be the correct count.  
Put this in a query along with your text field to examine and query for the number of commas you're looking for. It will return a number.

Function CountCommas (byref YourField as string) as long
dim vArray () as string

vArray = Split(YourField, ",")
CountCommas = ubound(varray)

End Function


You query:  CommaCount:  = CountCommas([YourField])

if CommaCount > 2 then - do something.


Scott C
0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 167 total points
ID: 39952821
easiest way is something like:

CommaCount: Len([yourField] & "") - LEN(Replace([YourField] & "", ",", ""))
Criteria: > 2

This simply counts the number of characters in the string, and then the number of characters in the string after having replaced all of the commas with a zero length string.

I tend to add the:

& ""

inside all of my string function calls to avoid the possibility that the function will raise an error if a NULL is passed to it.
0
 
LVL 16

Assisted Solution

by:Kalpesh Chhatrala
Kalpesh Chhatrala earned 166 total points
ID: 39952838
you can count  like below

Function CntCharacterInString(strToSrch As String, strCharToCount As String) As Long
   Dim lPos As Long
   Dim lnTotal As Long
   For lPos = 1 To Len(strToSrch)
      If Mid$(strToSearch, lPos, Len(strCharToCount)) = strCharToCount Then
         lnTotal = lnTotal + 1
      End If
   Next
   CntCharacterInString = lnTotal
End Function

Open in new window

0
 

Author Closing Comment

by:PeterBaileyUk
ID: 39952850
thank you all equally valid, so shared
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

760 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

22 Experts available now in Live!

Get 1:1 Help Now