Solved

Access VBA: replace spaces in string

Posted on 2008-10-17
4
2,000 Views
Last Modified: 2013-11-27
Hi,

I would like to take a string such as "Hello World,  Opps I just added 2 spaces after the comma" and replace it with "Hello+World,+Opps+I +just+added+2+spaces+after+the+comma"

The problem with the Replace() function is that it replaces sequential spaces so I get "Hello+World,++Opps". Thanks
0
Comment
Question by:BradleyOnTheRoad
  • 3
4 Comments
 
LVL 1

Accepted Solution

by:
MnInShdw earned 500 total points
ID: 22747242

Sub test3()

    

    Dim s As String

    Dim i As Integer

    Dim Result, currentletter As String

    

    

    s = "Hello World,  Opps I just added 2 spaces after the comma"

    

    For i = 1 To Len(s)

        currentletter = Mid(s, i, 1)

        If currentletter = " " Then currentletter = "+"

        Result = Result & currentletter

        If currentletter = "+" And Mid(s, i + 1, 1) = " " Then i = i + 1

    Next

    

    Debug.Print Result

    

End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:rgn2121
ID: 22747247
Couldn't you just do a replace for the double space first?

If currentletter = "  " Then currentletter = "+"     'Replaces the double space

code....

If currentletter = " " Then currentletter = "+"   'Replaces all single spaces...
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 22747252
oops...I just glanced up and thought that was part of the askers code...I didn't even look through it.  Sorry about that!
0
 
LVL 12

Expert Comment

by:rgn2121
ID: 22747257
I guess what I really meant was....

dim tempString as string
tempString = "Hello World,  Opps I just added 2 spaces after the comma"

tempString = Replace(tempString, "  ", "+")  'Replace the double space
temptString = Replace(tempString, " ", "+")  ' Replace the single Space
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now