Extracting single word from string

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

How can I extract the first word from a string regardless of if the string
has one or more words in it?

Thanks

Regards
 
Hi

How can I extract the first word from a string regardless of if the string
has one or more words in it?

Thanks

Regards
= IIf(inStr([FullString]," ")>0,
Left([FullString],InStr([FullString]," ")-1),[FullString])
 
John,

There is a simpler way, but this is what I use. Call it like this:
strMyWord = GetAllWordsAsCollection("This is a test")(1)
...or...
strMyWord = GetAllWordsAsCollection("This is a test").Item(1)

Public Function GetAllWordsAsCollection(ByVal strPhrase As String, Optional
strDelimiter As String = " ") As VBA.Collection
Dim col As VBA.Collection
Dim strTemp As String
Dim intPos As Integer

Set col = New VBA.Collection

strPhrase = Trim(strPhrase)
If Right(strPhrase, 1) = strDelimiter Then strPhrase = Left(strPhrase,
Len(strPhrase) - 1)
strPhrase = Trim(strPhrase)

Do While Len(strPhrase) > 0
intPos = InStr(1, strPhrase, strDelimiter)
If intPos = 0 Then
If Len(strPhrase) > 0 Then
col.Add strPhrase
strPhrase = ""
Else
Set col = Nothing
GoTo Proc_Exit
End If
Else 'intPos > 0
strTemp = Trim(Left(strPhrase, intPos - 1))
col.Add strTemp
strPhrase = Trim(Mid(strPhrase, Len(strTemp) + 1))
End If

If Left(strPhrase, 1) = "," Then strPhrase = Mid(strPhrase, 2)
strPhrase = Trim(strPhrase)
Loop

Proc_Exit:
If col.Count > 0 Then Set GetAllWordsAsCollection = col
End Function

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 

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