?
Solved

Access VBA: replace spaces in string

Posted on 2008-10-17
4
Medium Priority
?
2,019 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 2000 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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.
AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

770 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