Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Count the Number of Specific Character in a Field 1

Status
Not open for further replies.

TheTeaMan

Technical User
Jun 26, 2007
14
US
I have string data in one of my fields, separated by “,”. I would like to count how many times the “,” occurred in the field. If none, then I need to put 1.
Would you help please! Thanks..
 
A way would be to convert the data to an array based on comma separators and find the highest element in the array...


Code:
Ubound(Split([i]<Test Value>[/i],","))

Of course you need to replace <Test Value> with your value whether that is a field in a query or a variable in code.

The other way would be to write a function to do this. I have no idea which would ultimately be faster. If it is literally a value then I would just use the method above. If you are using it on thousands of rows in a query, it may make a noticable difference.
 
yourSubFieldsCount: 1+Len([your field])-Len(Replace([your field],',',''))

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If you do want to use a user defined function (and a different approach than lameid posted), you could use something like (will work with , by default but you can specify others to count):
Code:
Function CountChar(strField As String, Optional CharToCount As String = ",") As Integer
Dim re As Object
Dim mc As Object

If InStr(1, "[\^$.|?*+(){}", CharToCount) > 0 Then CharToCount = "\" & CharToCount

Set re = CreateObject("VBScript.RegExp")

With re
    .Global = True
    .MultiLine = True
    .IgnoreCase = True
    .Pattern = CharToCount
    Set mc = .Execute(strField)
    
End With

If mc.Count > 0 Then
CountChar = mc.Count
Else
CountChar = 1
End If

Set mc = Nothing
Set re = Nothing

End Function
Hope this helps

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I would agree, except it doesn't return correctly if there's more than 0 comma's in the field...

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thanks to all. PHV's solution was the best for me. It was straight forward. Thank you again.
 
I suppose if you're happy with an incorrect solution (for the original question at least) then more power to you... [wink]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Thank you HarleyQuinn for your response. I did add IIF statement for 0 comma's in the field.
 
Oops I missed the OP requirement...

If none, then I need to put 1.

My solution and PHV's both fundamentally count the commas. PHV adds one which effectively yields the number elements separated by commas.

To do what the OP states and incorporating PHV's method which is better than mine...

Code:
yourSubFieldsCount: IIF(instr(1,"Test",","),1, Len([your field])-Len(Replace([your field],',','')) )
 
Thanks for the reply TeaMan.

Could you show us (just for the completeness of the thread, so people finding it in a search will see a complete solution)?

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
I thought I had the = 0 in the truepart of the IIF... Anyway this is actually right.
Code:
yourSubFieldsCount: IIF(instr(1,"Test",",") = 0 ,1, Len([your field])-Len(Replace([your field],',','')) )
 
Thanks lameid, that's what I was trying to prod us along to [wink]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Sadly though I didn't catch my other mistake until it was staring me in the face again... I didn't substitute the field for the test I used so it is consistent. [blush]

Code:
yourSubFieldsCount: IIF(instr(1,[your field],",") = 0 ,1, Len([your field])-Len(Replace([your field],',','')) )

Here is hoping I have all mistakes out of the way for the day! [morning]
I have already exceeded quota and all in the same place.
 
[smile]

HarleyQuinn
---------------------------------
Carter, hand me my thinking grenades!

You can hang outside in the sun all day tossing a ball around, or you can sit at your computer and do something that matters. - Eric Cartman

Get the most out of Tek-Tips, read FAQ222-2244: How to get the best answers before posting.

 
Final Code when “your field” has Null value is:

IIf(IsNull(InStr(1,[your field],",")=True),0,1+Len([your field])-Len(Replace([your field],',','')))

Thanks Everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top