Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to determine number of words in a field 1

Status
Not open for further replies.

jimdevon

IS-IT--Management
Oct 14, 2002
25
US
I need a way of making sure a data entry person does not enter more than 3 names in a field, ie (John Larry Smith) is ok, but (John Smith Mary Smith) is not. My thinking is to count the number of spaces on an exit_event, and produce an error message if there are more than 2 spaces. Currently this exit_event splits the first and last names into two separate lastname and firstname fields. Thanks in advance for any help. Jimdevon
 
Put this code into a module...

To call it it will be like this...

varNumberOfSpacesInString = countspaces("text to count spaces)



ok, hope you can follow this, I just wrote it...

I didn't comment it because it's fairly simple and small... and I didn't put in error control so if you want these things you will need to take care of that part...


Hope this helps...

--James


Code Follows:


Function countspaces(Text As String) As Integer

Dim i As Integer
Dim x As Integer
Dim count As Integer
Dim z As Boolean
z = False
i = 1
x = 0
count = 0

While z = False
x = InStr(i, Text, " ", vbBinaryCompare)
i = x + 1
If x = 0 Then
z = True
Else
count = count + 1

End If


Wend

countspaces = count

End Function
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
I'm glad this was able to help you... I put it together in 10 minutes...

I find that I'm learning much more from helping others then when I am working on problems for my own databases...

I'm sure there's a better and faster way of doing this... so when you get around to it try and make it better:)

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
It could be simpler (this gets total words instead of spaces):

Code:
  Dim intTotal As Integer
  intTotal = TotalWords("Steven P. Jobs")

  

Function TotalWords(ByVal strToCheck As String) As Integer
  Dim varArray As Variant
  
  varArray = Split(strToCheck, " ")
  TotalWords = UBound(varArray) + 1
  
End Function

Let the built-in functions do the work! VBSlammer
redinvader3walking.gif
 
I've tried working with the split command, I couldn't get it to work...

Thanks...

--James
junior1544@jmjpc.net
Life is change. To deny change is to deny life.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top