I am creating a description field that concatenates several other fields and pads each field with a space. Since not all fields are populated, there were gaps showing up in the concatenated field. For instance, if the follwoing 4 fields were one string f1:Ford; f2:Fairmont; f3:””; f4:1.0 liter it would yield “Ford Fairmont 1.0 liter” with two spaces between Fairmont and 1.0 liter.
I used the following code to replace any series of double spaces with a single space:
>>>>>
Function StripExtraChars(PassedStr, RemoveExtraChar$)
On Local Error GoTo StripExtraChars_Err
Dim i As Integer, GotChar As Integer
Dim HoldStr As String, HoldChar As String
‘ Exit if passed value is Null.
If IsNull(PassedStr) Then Exit Function
‘ Trim extra characters from passed string.
PassedStr = Trim$(PassedStr)
‘ Cycle through string and remove extra
‘ string characters specified in the
‘ RemoveExtraChar value.
For i = 1 To Len(PassedStr)
HoldChar = Mid$(PassedStr, i, 1)
If HoldChar = RemoveExtraChar Then
If GotChar = False Then
GotChar = True
HoldStr = HoldStr & HoldChar
End If
Else
GotChar = False
End If
If Not GotChar Then
HoldStr = HoldStr & HoldChar
End If
Next i
StripExtraChars = HoldStr
StripExtraChars_End:
Exit Function
StripExtraChars_Err:
MsgBox Error$
Resume StripExtraChars_End
End Function
>>>>
It worked great, however, I was recently asked to pad the fields with a blank space AND a comma which would yield:
“Ford, Fairmont, , 1.0 liter”.
Simply changing the
HoldChar = Mid$(PassedStr, i, 1) to
HoldChar = Mid$(PassedStr, i, 2) doesn’t work. I tried several variations to clean the space and comma but have had no luck. Any help would be appreciated.