Trim after the first space

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

Guest

I have a function that works whenever anyone puts more than one word in a box
but now the staff are getting used to filling in a box correctly the function
gives debugs:

Public Function FirstWord(InputString As String) As String
Dim FirstSpace As String
FirstSpace = InStr(1, InputString, " ")
FirstWord = Left(InputString, FirstSpace - 1)
End Function

How do I tweak it to check that there is a space or ignore the input?
 
If there is no space then the Instr will return 0, so you can check for it.

Public Function FirstWord(InputString As String) As String
Dim FirstSpace As Integer
FirstSpace = InStr(1, InputString, " ")
If FirstSpace > 0 Then
FirstWord = Left(InputString, FirstSpace - 1)
End If
End Function
 
That works unless the users input
" Look where the first space is"

You could trim Input String first to get rid of any leading spaces and then
process as Ofer has suggested.

--
John Spencer
Access MVP 2002-2005, 2007
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
Back
Top