John, Mike,
I'm still learning Access so I'm trying to learn how to write efficient
Access code. I would appreciate it if you could comment on which method is
more efficient.
I think the first is more efficient because it makes less loops, but I don't
know how costly the InStr function is.
The second method does character by character manipulation.
Any comments would be greatly appreceiated.
=========================================
Function PickTrim(strInput As String) As String
Dim strOut As String
Dim intSpPos As Integer
strOut = Trim(strInput)
intSpPos = InStr(strOut, " ")
Do While intSpPos > 0
strOut = Left(strOut, intSpPos - 1) & Mid(strOut, intSpPos + 1)
intSpPos = InStr(strOut, " ")
Loop
PickTrim = strOut
End Function
=========================================
Function TrimSpaces(strInput) As String
Dim strCharRemove As String ' assigns
the char to look for & replace
Dim strCurChar As String ' Current
character to match up
Dim intNoChars As Integer ' Number of
characters in string
Dim x As Integer ' count of
chars in string
Dim intSpCt As Integer ' # of
spaces in a row.
strCharRemove = " " ' looks for
a space (can be any character)
intSpCt = 0
' trim spaces between text
strInput = Trim$(strInput)
intNoChars = Len(strInput) ' Get the
length of the string
For x = 1 To intNoChars
strCurChar = Mid(strInput, x, 1) ' Get curr
char from original string
If strCurChar = " " Then ' if the
character matches a space
intSpCt = intSpCt + 1
Else
intSpCt = 0 ' Reset flag
when find first non-matching character
End If
If intSpCt <= 1 Then
TrimSpaces = TrimSpaces & strCurChar ' create
parsed string
End If
' Debug.Print intNoChars, x, strCurChar, intSpCt, strInput, TrimSpaces
Next x