Word Count in a field

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I have a straight simple but nasty issue troubling me.

I want to have a word count in a every row on a text field.

Guru's please help!

Thanks

AA
 
AA,

A custom function placed in a global module should do the trick:

Public Function WordCount(strParam) As Long
Dim astrParsedField() As String

If Not IsNull(strParam) Then
' Parse string into an array
astrParsedField = Split(strParam, " ")
WordCount = UBound(astrParsedField) + 1
Else
WordCount = 0
End If

End Function

Set the textbox' ControlSource to:

=WordCount([YourTextbox])

Sprinks
 
Unfortunately, that's going to give an incorrect count if more than one
space exists between words:

?UBound(Split("This should be five words.", " "))+1
5
?UBound(Split("This should be five words.", " "))+1
9

You may also want a function to change all multiple spaces to a single
space, like

Function SingleSpaces(InputString As String) As String
Dim strWorkString As String

strWorkString = InputString

Do While InStr(strWorkString, " ") > 0
strWorkString = Replace(strWorkString, " ", " ")
Loop

SingleSpaces = strWorkString

End Function

?UBound(Split(SingleSpaces("This should be five words."), " "))+1
5
?UBound(Split(SingleSpaces("This should be five words."), " "))+1
5


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Sprinks said:
AA,

A custom function placed in a global module should do the trick:

Public Function WordCount(strParam) As Long
Dim astrParsedField() As String

If Not IsNull(strParam) Then
' Parse string into an array
astrParsedField = Split(strParam, " ")
WordCount = UBound(astrParsedField) + 1
Else
WordCount = 0
End If

End Function

Set the textbox' ControlSource to:

=WordCount([YourTextbox])

Sprinks

AA said:
Hi,
I have a straight simple but nasty issue troubling me.

I want to have a word count in a every row on a text field.

Guru's please help!

Thanks

AA
 
Thanks, Doug. I temporarily forgot the cardinal rule of programming: "The
user can type ANYTHING"!

Sprinks

Douglas J. Steele said:
Unfortunately, that's going to give an incorrect count if more than one
space exists between words:

?UBound(Split("This should be five words.", " "))+1
5
?UBound(Split("This should be five words.", " "))+1
9

You may also want a function to change all multiple spaces to a single
space, like

Function SingleSpaces(InputString As String) As String
Dim strWorkString As String

strWorkString = InputString

Do While InStr(strWorkString, " ") > 0
strWorkString = Replace(strWorkString, " ", " ")
Loop

SingleSpaces = strWorkString

End Function

?UBound(Split(SingleSpaces("This should be five words."), " "))+1
5
?UBound(Split(SingleSpaces("This should be five words."), " "))+1
5


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Sprinks said:
AA,

A custom function placed in a global module should do the trick:

Public Function WordCount(strParam) As Long
Dim astrParsedField() As String

If Not IsNull(strParam) Then
' Parse string into an array
astrParsedField = Split(strParam, " ")
WordCount = UBound(astrParsedField) + 1
Else
WordCount = 0
End If

End Function

Set the textbox' ControlSource to:

=WordCount([YourTextbox])

Sprinks

AA said:
Hi,
I have a straight simple but nasty issue troubling me.

I want to have a word count in a every row on a text field.

Guru's please help!

Thanks

AA
 
Sprinks said:
Thanks, Doug. I temporarily forgot the cardinal rule of programming:
"The user can type ANYTHING"!

Sprinks

Douglas J. Steele said:
Unfortunately, that's going to give an incorrect count if more than
one space exists between words:

?UBound(Split("This should be five words.", " "))+1
5
?UBound(Split("This should be five words.", " "))+1
9

You may also want a function to change all multiple spaces to a
single space, like

Function SingleSpaces(InputString As String) As String
Dim strWorkString As String

strWorkString = InputString

Do While InStr(strWorkString, " ") > 0
strWorkString = Replace(strWorkString, " ", " ")
Loop

SingleSpaces = strWorkString

End Function

?UBound(Split(SingleSpaces("This should be five words."), " "))+1
5
?UBound(Split(SingleSpaces("This should be five words."), "
"))+1 5


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


Sprinks said:
AA,

A custom function placed in a global module should do the trick:

Public Function WordCount(strParam) As Long
Dim astrParsedField() As String

If Not IsNull(strParam) Then
' Parse string into an array
astrParsedField = Split(strParam, " ")
WordCount = UBound(astrParsedField) + 1
Else
WordCount = 0
End If

End Function

Set the textbox' ControlSource to:

=WordCount([YourTextbox])

Sprinks

:

Hi,
I have a straight simple but nasty issue troubling me.

I want to have a word count in a every row on a text field.

Guru's please help!

Thanks

AA

Or, take it a step further, what about tab, linefeed, carriage return
or other stuff? If scripting is allowed, then one could play with
Regular Expressions - air code - also excluding non word characters

' in declaration section of module
Private gre As Object

Function GetWordCount(ByVal v_strIn As String) As Integer

Dim mc As Object

If (gre Is Nothing) Then
Set gre = CreateObject("vbscript.regexp")
End If
With gre
.Pattern = "\b\w+"
.Global = True
.MultiLine = True
Set mc = .Execute(v_strIn)
End With
GetWordCount = mc.Count
Set mc = Nothing

End Function

? GetWordCount("Just testing" & vbtab & "a bit" & vbcrlf & "stuff")
 
Thanks folks! you are great

RoyVidar said:
Sprinks said:
Thanks, Doug. I temporarily forgot the cardinal rule of programming:
"The user can type ANYTHING"!

Sprinks

Douglas J. Steele said:
Unfortunately, that's going to give an incorrect count if more than
one space exists between words:

?UBound(Split("This should be five words.", " "))+1
5
?UBound(Split("This should be five words.", " "))+1
9

You may also want a function to change all multiple spaces to a
single space, like

Function SingleSpaces(InputString As String) As String
Dim strWorkString As String

strWorkString = InputString

Do While InStr(strWorkString, " ") > 0
strWorkString = Replace(strWorkString, " ", " ")
Loop

SingleSpaces = strWorkString

End Function

?UBound(Split(SingleSpaces("This should be five words."), " "))+1
5
?UBound(Split(SingleSpaces("This should be five words."), "
"))+1 5


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


AA,

A custom function placed in a global module should do the trick:

Public Function WordCount(strParam) As Long
Dim astrParsedField() As String

If Not IsNull(strParam) Then
' Parse string into an array
astrParsedField = Split(strParam, " ")
WordCount = UBound(astrParsedField) + 1
Else
WordCount = 0
End If

End Function

Set the textbox' ControlSource to:

=WordCount([YourTextbox])

Sprinks

:

Hi,
I have a straight simple but nasty issue troubling me.

I want to have a word count in a every row on a text field.

Guru's please help!

Thanks

AA

Or, take it a step further, what about tab, linefeed, carriage return
or other stuff? If scripting is allowed, then one could play with
Regular Expressions - air code - also excluding non word characters

' in declaration section of module
Private gre As Object

Function GetWordCount(ByVal v_strIn As String) As Integer

Dim mc As Object

If (gre Is Nothing) Then
Set gre = CreateObject("vbscript.regexp")
End If
With gre
.Pattern = "\b\w+"
.Global = True
.MultiLine = True
Set mc = .Execute(v_strIn)
End With
GetWordCount = mc.Count
Set mc = Nothing

End Function

? GetWordCount("Just testing" & vbtab & "a bit" & vbcrlf & "stuff")
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top