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!

How to get rid of annoying spaces 1

Status
Not open for further replies.

GLENEE

Programmer
Feb 9, 2005
64
GB
Is there a way of removing spaces from the middle of text fields:

examples:
"ABBG TP 13 AASR RLC"
"A (AD) TP 47 AD SQN RLC"
"MECH COY UK CSS STN"

I'm talking thousands of records so obviously the manual option is out of the question. I could use the find/replace option and replace space space space with one space etc, however the end user will not want to do this and besides the data will be read only to them. Ideally I would like to resolve it automatically on import of the records. Thanks

 
I'd use replace in an update query.

Import then run the query.
 
How are ya GLENEE . . .

Example:
Code:
[blue]   Dim Txt As String
   
   Txt = "A  (AD) TP        47 AD SQN RLC"
   Txt = Replace(Txt, "  ", "")[/blue]

Calvin.gif
See Ya! . . . . . .
 
Do you want to remove ALL spaces or just convert multiple spaces to a single space? In the first instance, use TheAceMan1's suggestion. If it's the second then write a public function
Code:
Public Function SingleSpaceIt(myString As String) As String
SingleSpaceIt = Trim(myString)
Do Until Len(SingleSpaceIt) = Len(Replace(SingleSpaceIt,"  "," "))
   SingleSpaceIt = Replace(SingleSpaceIt,"  "," ")
Loop
Exit Function
Then in your SQL
Code:
Select ..., SingleSpaceIt([FieldWithSpaces]) As [Newfield],
 
Golom . . .

Yeah . . . I forgot to loop . . .

[blue]GLENEE[/blue] . . . A cleaned up idea of what you want:
Code:
[blue]   Dim Txt As String
   
   Txt = "A  (AD) TP        47 AD SQN RLC"
   
   
   Do Until InStr(1, Txt, "   ") = 0
      Txt = Replace(Txt, "   ", "")
   Loop

   Txt = Replace(Txt, "  ", " ")
   Debug.Print Txt[/blue]

Calvin.gif
See Ya! . . . . . .
 
GLENEE . . .

Had 4 test simulations.Copied the wrong one. Code should be as follows:
Code:
[blue]   Dim Txt As String
   
   Txt = "A  (AD) TP        47   AD    SQN                 RLC"
   
   
   Do Until InStr(1, Txt, "  ") = 0
      Txt = Replace(Txt, "  ", " ")
   Loop

   Debug.Print Txt[/blue]

Calvin.gif
See Ya! . . . . . .
 
Sorry i haven't responded, i've been away.
I want to replace multiple spaces with a single space. I've written the function suggested by Golom but not quite sure how i use it. Can you explain further the bit about using with my SQL.

Ex:
The Table name is "tblSites"
The Field with spaces is "txtName"

Thanks
 
Why not use Trim() ?

SQL:

Code:
SELECT Trim([txtName]) as txtName_Trimmed

FROM tblSites


~Melagan
______
"It's never too late to become what you might have been.
 
Why not use Trim() ?
Because Trim don't care spaces in the middle of the string.
 
After importing the records I used an update query and Goloms function to achieve the result. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top