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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace random number of spaces in string 1

Status
Not open for further replies.

Sunwriter

IS-IT--Management
Jan 25, 2004
6
US
Hello:

Envrioment: Access 2002

I have a field with data similar to the following:

STUCK AT 55 TEMP N/INT N

Notice the spaces between 55 and TEMP. I would like to elimitate the random number of spaces and replace with 2 spaces such as:

STUCK AT 55 TEMP N/INT N

Thanks in advance for any assistance,

Jeff
 
Try this UDF
Code:
Public Function DropSpaces(s As String, _
       Optional MinSpaces As Integer = 2) As String

DropSpaces = s
Do Until Len(DropSpaces) = _
         Replace(DropSpaces, Space(MinSpaces + 1), Space(MinSpaces))
   DropSpaces = _
   Replace(DropSpaces, Space(MinSpaces + 1), Space(MinSpaces ))
Loop

End Function
And call it with
Code:
NewString = DropSpaces(SomeString)
or in an SQL statement as
Code:
Select DropSpaces([SomeField]), ...

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Sorry ... that should have been
Code:
Public Function DropSpaces(s As String, _
       Optional MinSpaces As Integer = 2) As String

DropSpaces = s
Do Until Len(DropSpaces) = _
         [COLOR=red]Len([/color]Replace(DropSpaces, Space(MinSpaces + 1), Space(MinSpaces))[COLOR=red])[/color]
   DropSpaces = _
   Replace(DropSpaces, Space(MinSpaces + 1), Space(MinSpaces ))
Loop

End Function

[small]No! No! You're not thinking ... you're only being logical.
- Neils Bohr[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top