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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Counting Spaces in a Text Field 1

Status
Not open for further replies.

Brinny

Programmer
Aug 18, 2001
3
AU
How can you count the number of spaces (eg " ") that are within the one text field?

I'm interested in code only.
 
Hi, Brinny!

There are codes what you can use for spaces as well as anyone other symbol counting in the text string.

Public Function CharCountInString(strText As String, Optional strChar As String = " ") As Integer
Dim n As Byte
Dim intCharCount As Integer

'If strChar is omited spaces in the text are count
Do
'Look for spaces in text
'and if found plus to intCharCount
n = InStr(1, strText, strChar, vbTextCompare)
If n > 0 Then
intCharCount = intCharCount + 1
strText = Mid(strText, n + 1)
Else
Exit Do
End If
Loop
CharCountInString = intCharCount
End Function


Example:

? CharCountInString("This is a space count test")
5
? CharCountInString("This is a space count test","t")
4
? CharCountInString("This is a space count test","n")
1


Aivars

 
Too EASY!.

Glorified it to "count" what ever You want!

Code:
Public Function basCountChar(strIn As String, Optional Delim As String = " ") As Long

    'Usage:
    '? basNumSpaces("My dog has NO! fleas")
    '4

    'basCountChar("My dog has NO! fleas", "o")
    '2

    '? basCountChar("My dog has NO! fleas", "dog")
    '1


    Dim varStr As Variant

    varStr = Split(strIn, Delim)
    basCountChar = UBound(varStr)

End Function
MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Hi, Michael!

I think Ac97 don't support function Split().

Aivars
 
I didn't see any (other) reference to '97. But IF YOU MUST remain in RETRO land, There are are several versions of User functions which are posted here in Tek-Tips to do the same. For at least one, do a search on "basSplit", which I posted long away and far ago.

MichaelRed
mred@att.net

There is never time to do it right but there is always time to do it over
 
Lot of people use Ac97, int.al. Tek-Tips forum members. Each improvement charge some money...
Therefore it's not remain in RETRO land.

Aivars
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top