how to enter words in a text box and count the number of spaces

G

Guest

In microsoft access vba programming how do i enter the words in a text box
and count the number of spaces between the words to be displayed in a message
box?
 
D

Dirk Goldgar

In
JudithSpurlock said:
In microsoft access vba programming how do i enter the words in a
text box and count the number of spaces between the words to be
displayed in a message box?

Here's one way:

MsgBox UBound(Split([YourTextBox]))

Be aware that the above expression will raise an error if the text box's
value is Null, and will return -1 if the text box contains a zero-length
string. You can easily program around those issues, if necessary.
 
F

fredg

In microsoft access vba programming how do i enter the words in a text box
and count the number of spaces between the words to be displayed in a message
box?

To count the spaces in the text:
=Len([Fieldname])-Len(Replace([FieldName]," ",""))

"This is my text."
3
 
A

Albert D. Kallal

Assuming you have a text box where the user enterd the words and spaces...

The code behind a buttion to show the word count would be:


msgbox "spaces = " & ubound(split(me.Mytextboxname," "))
 
D

Dirk Goldgar

In
fredg said:
In microsoft access vba programming how do i enter the words in a
text box and count the number of spaces between the words to be
displayed in a message box?

To count the spaces in the text:
=Len([Fieldname])-Len(Replace([FieldName]," ",""))

"This is my text."
3

I suspect, though I haven't tested it, that this method is faster than
the Split method I proposed.
 
A

Albert D. Kallal

I suspect, though I haven't tested it, that this method is faster than the
Split method I proposed.

Gee, a rather different approach.....one of those out of the box type
solutions....

Very neat-o

A small cracker jack prize for this one...I never seen this solution......
 
S

Stefan Hoffmann

hi Dirk,

Dirk said:
I suspect, though I haven't tested it, that this method is faster than
the Split method I proposed.
Just a simple test with GetTickCount():

Const MAX_COUNT As Long = 100000
Const STR_TEST As String = "this is a test."

Dim lngChar As Long
Dim lngChars As Long
Dim lngCount As Long
Dim lngNumSpaces As Long
Dim lngTickEnd As Long
Dim lngTickStart As Long

lngTickStart = GetTickCount()
For lngCount = 1 To MAX_COUNT
lngNumSpaces = UBound(Split(STR_TEST))
Next lngCount
lngTickEnd = GetTickCount()

Debug.Print "First: "; lngTickEnd - lngTickStart; _
" Num Spaces: "; lngNumSpaces


lngTickStart = GetTickCount()
For lngCount = 1 To MAX_COUNT
lngNumSpaces = Len(STR_TEST) - Len(Replace(STR_TEST, " ", ""))
Next lngCount
lngTickEnd = GetTickCount()

Debug.Print "Second: "; lngTickEnd - lngTickStart; _
" Num Spaces: "; lngNumSpaces


lngTickStart = GetTickCount()
lngChars = Len(STR_TEST)
For lngCount = 1 To MAX_COUNT
lngNumSpaces = 0
For lngChar = 1 To lngChars
If Mid$(STR_TEST, lngChar, 1) = " " Then
lngNumSpaces = lngNumSpaces + 1
End If
Next lngChar
Next lngCount
lngTickEnd = GetTickCount()

Debug.Print "Third: "; lngTickEnd - lngTickStart; _
" Num Spaces: "; lngNumSpaces


The Split() methode seems to be the fastest. Also using a very long
string will yield the same ranking.


mfG
--> stefan <--
 
D

Dirk Goldgar

In
Stefan Hoffmann said:
hi Dirk,

Dirk said:
I suspect, though I haven't tested it, that this method is faster
than the Split method I proposed.
Just a simple test with GetTickCount(): [...]
The Split() methode seems to be the fastest. Also using a very long
string will yield the same ranking.

Interesting. Thanks, Stefan. I just ran a benchmark using
QueryPerformanceCounter in kernel32, and found Split to be marginally
faster than the Replace method. I'm surprised. It just goes to show
you can't rely on intuition when it comes to performance questions.
 
G

Guest

freg do i put MsgBox =Len([Fieldname])-Len(Replace([FieldName]," ","")) to
count the number of spaces between the words in a text box? I just want to
make sure i understand what to do. Judith Spurlock

fredg said:
In microsoft access vba programming how do i enter the words in a text box
and count the number of spaces between the words to be displayed in a message
box?

To count the spaces in the text:
=Len([Fieldname])-Len(Replace([FieldName]," ",""))

"This is my text."
3
 
F

fredg

freg do i put MsgBox =Len([Fieldname])-Len(Replace([FieldName]," ","")) to
count the number of spaces between the words in a text box? I just want to
make sure i understand what to do. Judith Spurlock

fredg said:
In microsoft access vba programming how do i enter the words in a text box
and count the number of spaces between the words to be displayed in a message
box?

To count the spaces in the text:
=Len([Fieldname])-Len(Replace([FieldName]," ",""))

"This is my text."
3

Do you wish a message to appear after a user finishes entering the
text in the control?
if so, code the control's (into which the user is typing the text)
AfterUpdate event:

MsgBox "You entered " & Len([FieldName])-Len(Replace([FieldName],"
","")) & " spaces."

If you wish to know how many words were typed then:

MsgBox "You entered " & Len([FieldName])-Len(Replace([FieldName],"
","")) + 1 & " words."
 
G

Guest

Fredg I guess i didn't explain the whole process because it wouldn't fit in
the search area. the program is supposed to allow a user to enter multiple
lines in a text box. and in the click event of the command button, use a for
loop and the Len function to iterate through each character in the text box.
Every time a space character is found, increment a procedure-level variable
by 1. after the loop has completed, output the number of spaces found in a
message box. how would i do that. I'm really unsure about this. Judith
Spurlock

fredg said:
freg do i put MsgBox =Len([Fieldname])-Len(Replace([FieldName]," ","")) to
count the number of spaces between the words in a text box? I just want to
make sure i understand what to do. Judith Spurlock

fredg said:
On Wed, 28 Mar 2007 14:02:05 -0700, JudithSpurlock wrote:

In microsoft access vba programming how do i enter the words in a text box
and count the number of spaces between the words to be displayed in a message
box?

To count the spaces in the text:
=Len([Fieldname])-Len(Replace([FieldName]," ",""))

"This is my text."
3

Do you wish a message to appear after a user finishes entering the
text in the control?
if so, code the control's (into which the user is typing the text)
AfterUpdate event:

MsgBox "You entered " & Len([FieldName])-Len(Replace([FieldName],"
","")) & " spaces."

If you wish to know how many words were typed then:

MsgBox "You entered " & Len([FieldName])-Len(Replace([FieldName],"
","")) + 1 & " words."
 
G

Guest

To anyone that can help.
The program requires a form and a text box and one command button. Its
supposed to allow the user to enter multiple lines of text into the box. In
the click event of the command button, use a For loop and the Len function to
iterate through each character in the text box. every time a space character
is found, increment a procedure-level variable by 1. After the for loop has
completed, output the number of spaces found in a message box. Could some one
please help me with this program. I wasn't able to explain the whole program
in the subject line so i had to resubmit this information. Thanks Judith
Spurlock
 
A

AccessVandal via AccessMonster.com

Hi Judith,

Why do you want to use the For Loop?
And why do you want increment?
“every time a space character is found, increment a procedure-level variable by 1.â€

Just use the Onclick Event of the command button. Like

Private Sub command_click()

Dim lngCount As Long
lngCount = UBound(Split(Me.Text0))
MsgBox "Total Spaces Found = " & lngCount

End Sub

As you have said, you only want to know how many spaces.
Is there anything you missed out?
 

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

Top