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
9
Medium Priority
?
136 Views
Last Modified: 2010-04-07
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
Comment
Question by:Noddingdog
9 Comments
 
LVL 44

Expert Comment

by:bruintje
ID: 8003187
Hello Noddingdog,

you could try

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

HAGD:O)Bruintje
0
 

Author Comment

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

ND
0
 
LVL 2

Expert Comment

by:Mennovdh
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 70

Expert Comment

by:Éric Moreau
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

by:bruintje
ID: 8003239
hmmmm misread the q
0
 
LVL 5

Accepted Solution

by:
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

by:Noddingdog
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

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

Thanks again,
Noddingdog
0
 

Author Comment

by:Noddingdog
ID: 8003328
Thanks - good answer.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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

571 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