If I have a field in a table in Access......

cchen

New Member
Joined
Aug 11, 2003
Messages
1
The field has a bunch of text in it. Is there any way to remove all the excess white spaces?

It'll look something like this: asdf (space) (space) (space) (space) asdf
when i want it to look like this: asdf adf asdf etc

any way to do this besides doing it manually?
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Use the Edit -> Replace utility. You can replace a single space with nothing to remove all of them, or you can replace two spaces with one (repeatedly) to remove duplicates.
 
Upvote 0
Yes, possibly, but my first thought seems messy and requires code.
You'd have to start with parsing the field character by character.

How about this:

1) Read field value into variable
2) LTrim (trim all spaces to left)
3) InStr to first space. Subtract one. Extract the characters up to and including that position into a new field adding a single space afterwards
4) Repeat step 1/2 until done
5) .Edit/.Update the new value into table

So, did this real quickly so it might not be the easiest way to accomplish it.

Code:
Public Function reParse()
Dim dbs As DAO.Database
Dim rs As DAO.Recordset
Dim strAString As String
Dim strW As String
Dim x As Integer

Set dbs = CurrentDb()

Set rs = dbs.OpenRecordset("Select * from tblparse", dbopendynaset)

With rs
  Do Until rs.EOF
    strAString = LTrim(.Fields(0).Value)
    strW = ""
    Do Until Len(strAString) = 0
      x = InStr(strAString, " ")
      If x = 0 Then Exit Do
      strW = strW & Left(strAString, x - 1) & " "
      strAString = Mid(strAString, x + 1, Len(strAString) - x + 1)
      strAString = LTrim(strAString)

      .Edit
      .fields(0).Value = strW
      .Update

    Loop
    .MoveNext
  Loop
End With

Set rs = Nothing
Set dbs = Nothing
End Function

Oh, you can use the fieldname instead of .fields(0).Value

Mike
 
Upvote 0

Forum statistics

Threads
1,221,550
Messages
6,160,459
Members
451,648
Latest member
SuziMacca

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