Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Newbie Question: Count the number of occurences of a character in a textbox

Posted on 2003-02-23
Medium Priority
136 Views
Hi
I need to find out how to count the number of occurences of a character (say, 1) in a textbox, and save the number to a variable. Is there a quick easy way of doing this?

Thanks
Noddingdog
0
Question by:Noddingdog

LVL 44

Expert Comment

ID: 8003187
Hello Noddingdog,

you could try

Dim i as integer
i=Len(Textbox1.Text)

HAGD:O)Bruintje
0

Author Comment

ID: 8003207
Hi
I haven't come across the Len command before, what does it do?

ND
0

LVL 2

Expert Comment

ID: 8003226
Private Function CountOccurences(Text As String, searchChar As String) As Long
Dim loc As Long

loc = InStr(Text, searchChar)
CountOccurences = 0

Do While loc > 0
loc = InStr(loc + 1, Text, searchChar)
CountOccurences = CountOccurences + 1
Loop

End Function

I have no idea if this is the best way to do it, though.

len() will give you the length of a string.
0

LVL 70

Expert Comment

ID: 8003229
you can use this:

Private Sub Command1_Click()
MsgBox CountChars("This is a test!", "s")
End Sub

Private Function CountChars(ByVal pstrSource As String, ByVal pstrSearchChar As String) As Integer
Dim intI As Integer
Dim intTotal As Integer

For intI = 1 To Len(pstrSource)
If Mid\$(pstrSource, intI, 1) = pstrSearchChar Then
intTotal = intTotal + 1
End If
Next intI
CountChars = intTotal
End Function
0

LVL 44

Expert Comment

ID: 8003239
0

LVL 5

Accepted Solution

Rhaedes earned 300 total points
ID: 8003254
Try this one. It compares substracts the length of the string with all the characters you are searching for removed, with the length of the original string.

Private Function countChars(myString As String, myChar As String)
countChars = Len(myString) - Len(Replace(myString, myChar, ""))
End Function

Kindest regards,
Rhaedes
0

Author Comment

ID: 8003302
Hi,
Thanks for all your help. Rhaedes seems to have a short answer that has proved useful, but is there any way to store the results of that function to a variable?

ND
0

Author Comment

ID: 8003327
Sorry, penny just dropped. I know where I was going wrong >_<

Thanks again,
Noddingdog
0

Author Comment

ID: 8003328
0

## Featured Post

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month10 days, 20 hours left to enroll