OK, first of all, thank you for coming trying to help.
anyways, i need help on making a program that counts how many words you have typed.
REstriction: Must use Strings...

Please help me, thx in advance.;)

Hi,

U need to count words from a textbox or a text file or a Word file..?

Regards
Veena

I hope it could help.

Private Function CountWords(strText As String) As Long
    Dim x As Long
    Dim words As Long
    Dim lenStr As Long
    Dim lenPrevWord As Long
    Dim currentAscii As Integer
    
    ' Number of chars in string
    lenStr = Len(strText)
    ' Number of chars in the last word processed
    lenPrevWord = 0
    
    For x = 1 To lenStr
        ' ASCII value of the char being processed
        currentAscii = Asc(Mid$(strText, x, 1))
        Select Case currentAscii
            ' If current char is space (ASCII value 32)...
            Case 32
                ' ...and we have processed at least a char before
                ' then we have found a word
                If lenPrevWord > 0 Then
                    words = words + 1
                    ' Clear count of characters in previous word
                    lenPrevWord = 0
                End If
            Case Else
                ' If current char is not a space, then add 1 to the
                ' count of chars of the current word.
                lenPrevWord = lenPrevWord + 1                                     
        End Select

        ' Test if last char is other than a space.
        ' If so then there is a word.
        If x = lenStr And currentAscii <> 32 Then
            words = words + 1
        End If
    Next x
    
    CountWords = words
End Function

Bye!

I have uploaded a sample VB6 project to demonstrate the use of this function.

I have uploaded a sample VB6 project to demonstrate the use of this function.

sadly Your program does not work.:sad:
but just now i came up with something:

Private Sub cmd_Click()
txt = txt.Text
txt.Text = Trim$(txt.Text)
spacecount = 1
For x = 1 To Len(txt)
If Mid(txt, x, 1) = " " Then spacecount = spacecount + 1
Next
MsgBox spacecount & " words found."
End Sub

This program does not count the number of words but the number of spaces. This will work too if i can just get it to stop counting the extra spaces as words betweeen words.

Hi,

U need to count words from a textbox or a text file or a Word file..?

Regards
Veena

I need to count the words from a text box :)
It has to be able to not count 2 or more spaces between words.

sadly Your program does not work.:sad:

Hi! Tell me why my program doesnt work so I can fix it.

Hi! Tell me why my program doesnt work so I can fix it.

Your need to define CountWords

Private Sub txtText_Change()
    lblWords.Caption = "You have tiped " & CStr([B]CountWords[/B](txtText.Text)) & " words."

CountWords is the function coded inside the module "mdlWordCount.bas".

Did you open de prjWordCount.vbp file to load the project in the VB6 IDE or just the frm file?

Hi,

Just use this simple Code:

Private Sub CmdCount_Click()
    Dim MyCntArray
    Dim MyStr As String
    Dim i As Integer
    '
    MyStr = TextBox1.Text
    '
    For i = 2 To 10
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    ' Replace All the multiple Spaces with single spaces 
    ' here i have given for max 10 spaces, 
    ' u can change it to a higher value
    '
    MyCntArray = Split(MyStr, " ")
    MsgBox "Number Of Words : " & UBound(MyCntArray) - 1
    '
End Sub

I hope it is clear.

Regards
Veena :lol:

Hi,

Sorry, Little bit of Modification in my previous post :
Just Change For Loop this way :

For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    MyCntArray = Split(MyStr, " ")
    MsgBox UBound(MyCntArray)

Regards
Veena

Hi,

Sorry, Little bit of Modification in my previous post :
Just Change For Loop this way :

For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    MyCntArray = Split(MyStr, " ")
    MsgBox UBound(MyCntArray)

Regards
Veena

Veena, you must be joking! :lol:
Don't you? :-|

Try this with your code "Hello world" and please post how many words your program find.

Veena, you must be joking! :lol:
Don't you? :-|

Try this with your code "Hello world" and please post how many words your program find.

i got 1

Hi,

Did u look into my last post???? I have corrected there.

MsgBox UBound(MyCntArray)


Remove "-1"

Regards

Veena

Hi

This is My Complete. Code.

Private Sub CmdCount_Click()
    Dim MyCntArray
    Dim MyStr As String
    Dim i As Integer
    '
    MyStr = TextBox1.Text
    '
    For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    ' Replace All the multiple Spaces with single spaces 
    ' here i have given for max 10 spaces, 
    ' u can change it to a higher value
    '
    MyCntArray = Split(MyStr, " ")
    MsgBox "Number Of Words : " & UBound(MyCntArray) 
    '
End Sub

I hope at least now, it is Clear.

Regards
Veena

Thx Guys, I got it to work.
Thank you for all your help.

Love You all.

I have uploaded a sample VB6 project to demonstrate the use of this function.

excellent code.
worked first time for me and is better than heaps of other examples.
Thanx 4 da code

excellent code.
worked first time for me and is better than heaps of other examples.
Thanx 4 da code

Hey, thanks :)

Hey, thanks :)

perhaps you have an answer for reading the total characters in a text box
and a way of limiting the number.

I've spent 93 mins so far and can not find a suitable answer.
Regards
Arty

perhaps you have an answer for reading the total characters in a text box
and a way of limiting the number.

I've spent 93 mins so far and can not find a suitable answer.
Regards
Arty

Yes,
This gives you length of Characters in the TextBox..
MsgBox Len(Text1.Text)

You can Limit the TextBox Setting its MaxLength
Text1.maxLength =10

Regards
Veena

Hi

This is My Complete. Code.

Private Sub CmdCount_Click()
    Dim MyCntArray
    Dim MyStr As String
    Dim i As Integer
    '
    MyStr = TextBox1.Text
    '
    For i = 10 To 2 Step -1
        MyStr = Replace(MyStr, Space(i), Space(1))
    Next
    ' Replace All the multiple Spaces with single spaces 
    ' here i have given for max 10 spaces, 
    ' u can change it to a higher value
    '
    MyCntArray = Split(MyStr, " ")
    MsgBox "Number Of Words : " & UBound(MyCntArray) 
    '
End Sub

I hope at least now, it is Clear.

Regards
Veena

Sorry for posting an old thread back.I search through google and found this particular thread.
Iam a beginner in programming and currently trying to make a simple word art(Iam adding certain extra feature).
My problem with the code above is that it counts the word starting with 0.I would like it to count the words from 1.Is there any way i could change the code above(I tried changing the numbers but not working)

Hi Sixa,

Can you give an example with text data..
so it would be easy to understand as of what you want..?

Regards
Veena

try using instr(1,text1.text," ") which will search for blanks and just subtract it from len(text1.text)

put this in click event

If Text1.Text <> "" Then
sp = InStr(1, Text1.Text, " ")
wordlen = Len(Text1.Text)
lnth = wordlen - sp
MsgBox "There are " & lnth & " words present"
End If

HOPE THIS HELPS IT WORKS FINE ON MY COMPUTER ;)

similarly, try adding other searches like commas or full stops in Instr function and keep subtracting it.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.