Trimming Extra Spaces within a Text String

jobb

Board Regular
Joined
Apr 24, 2003
Messages
78
The Trim function in Access only removes extra spaces at the beginning and ending of a text string.

If I have a text string like this,

"JOHN Q PUBLIC"

Is there any way I can remove the extra spaces inside the string, so it reads like this:

"JOHN Q PUBLIC"
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
that's odd, this website seems to do exactly what I'm trying to do :eek:

Okay, I will substitute dashes for spaces..

I want to trim

"JOHN-------------Q----------------PUBLIC"

to

"JOHN Q PUBLIC"
 
Upvote 0
thanks for the solution..

I have little experience with modules. Can you show me how this would work in a module?

thanks again
 
Upvote 0
Go into the Modules object, click on New, and paste teh following code in the resulting window (probably called Module1):

Code:
Function MyTrim(str As String) As String

    'Start by trimming leading/trailing spaces
    str = Trim(str)
    
    'Now, while we have 2 consecutive spaces, replace them
    'with a single space...
    Do While InStr(1, str, "  ")
      str = Replace(str, "  ", " ")
    Loop

    MyTrim = str

End Function

Save and close the module.

Now you can use the "MyTrim" function exactly as you would the "Trim" function in queries or VBA, i.e.
MyTrim(value)

Note: value will probably be a field name
 
Upvote 0
Thanks,

I tried this, but it doesn't recognize the "REPLACE" function.

I'm using Access 97
 
Upvote 0
Hmmm... I am using Access 2002, so I can't recreate the problem. I did find some links which look like they have more code to create the Replace function. Here they are:

http://support.microsoft.com/default.aspx?scid=kb;en-us;109330&Product=acc97

http://www.tek-tips.com/gfaqs.cfm/lev2/4/lev3/27/pid/705/fid/1824

http://forums.devshed.com/t98830/s6c092598a64781a322a1bf69172d6405.html

You would need to place one of these in the module as well. Then, wherever the MyTrim function calls "Replace", replace it with the name of this new module (i.e. sReplace, fReplace, or whatever it is called).
 
Upvote 0

Forum statistics

Threads
1,221,668
Messages
6,161,163
Members
451,687
Latest member
KENNETH ROGERS

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top