Solved

Access VBA: replace spaces in string

Posted on 2008-10-17
4
2,015 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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.

705 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