Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Modules / DAO / VBA / March 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JudithSpurlock - 28 Mar 2007 22:02 GMT
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?
Dirk Goldgar - 28 Mar 2007 22:19 GMT
> 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.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

fredg - 28 Mar 2007 22:25 GMT
> 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
Signature

Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail

Dirk Goldgar - 28 Mar 2007 23:44 GMT
>> 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
[quoted text clipped - 5 lines]
> "This is my text."
> 3

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

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

Albert D. Kallal - 29 Mar 2007 09:32 GMT
> 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......

Signature

Albert D. Kallal    (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@msn.com

Stefan Hoffmann - 29 Mar 2007 09:58 GMT
hi Dirk,

> 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 <--
Dirk Goldgar - 29 Mar 2007 15:48 GMT
> hi Dirk,
>
>> 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.

Signature

Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)

JudithSpurlock - 29 Mar 2007 19:54 GMT
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

> > 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
[quoted text clipped - 5 lines]
> "This is my text."
> 3
fredg - 29 Mar 2007 20:33 GMT
> 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
[quoted text clipped - 9 lines]
>> "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."

Signature

Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail

JudithSpurlock - 29 Mar 2007 21:14 GMT
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

> > 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
[quoted text clipped - 22 lines]
> MsgBox "You entered " &  Len([FieldName])-Len(Replace([FieldName],"
> ",""))  + 1 & " words."
Albert D. Kallal - 28 Mar 2007 22:49 GMT
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," "))

Signature

Albert D. Kallal    (Access MVP)
Edmonton, Alberta Canada
pleaseNOOSpamKallal@msn.com

JudithSpurlock - 30 Mar 2007 06:08 GMT
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

> 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?
AccessVandal - 30 Mar 2007 09:31 GMT
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?

>To anyone that can help.
>The program requires a form and a text box and one command button. Its
[quoted text clipped - 6 lines]
>in the subject line so i had to resubmit this information. Thanks Judith
>Spurlock
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.