Hello all..

I'm looking for how to count words in textbox.
Any help will appreciated.
Thank you.

do you have any code that you are working on ?

i think to count space cause every word will separated with space.

Private Sub Command1_Click()
Dim Counts As Integer
If Text1.Text = "" Then
    Counts = 0
Else
    Counts = 1
    For i = 1 To Len(Text1.Text)
        If Then 'this part i don't know how to search the space
            Counts = Counts + 1
        End If
    Next
End If
MsgBox Counts
End Sub

Use Mid function to search space in text box:

Private Sub Command1_Click()
Dim Counts As Integer
If Text1.Text = "" Then
    Counts = 0
Else
    Counts = 1
    For i = 1 To Len(Text1.Text)
        If Mid(Text1.Text, i, 1) = " " Then ' use Mid to search space
            Counts = Counts + 1
        End If
    Next
End If
MsgBox Counts
End Sub
commented: Why do you insist on doing their homework? -3
commented: Thanks for Mid function +2
commented: Usefull +3

What if there are 2 or more spaces between some words?
Did you count the first word? It has no space in front of it.
Did you consider the way many people now incorrectly write? They never add a space after a sentence -- just run the 2 sentences together.

Things to think about.

By this below codes you can check how many words are there on single line or multi line text box even if user enter space in between and before or after words.

Private Sub Command1_Click()
Dim Counts As Integer
Dim dupText As String

dupText = Replace(Trim(Text1.Text), vbNewLine, " ")

If dupText = "" Then
    Counts = 0
Else
    Counts = 1
    For i = 1 To Len(dupText)
        If Mid(dupText, i, 1) = " " Then ' use Mid to search space
            If Mid(dupText, i - 1, 1) <> " " Then
                Counts = Counts + 1
            End If
        End If
    Next
End If
MsgBox Counts
End Sub
commented: agree +12
commented: Thanks for duplicate checking :) +2
commented: and thanks for doing his homework for him. Now he doesn't have to think. -3

Thanks all..its worked :)

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.