Discussion Locked

MSAccess 97 Replace Function

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I need to strip spaces (between words) out of a field in a query. VBA does not have the Replace function I need. Any ideas?

3 replies

by bob sellman

If you put the following code (substituting your text for the sample text and dropping the msgbox that shows the results), it will do exactly what you ask. In fact, I've written it in a way that allows you to easily strip any selected character from the text.

Dim strSource As String
strSource = "This is a test." 'sample text
Dim strResult As String 'result ends up here
Dim lngLength As Long 'length of original string
Dim strEliminate As String ' what to eliminate
strEliminate = " " 'eliminate a space
Dim intWhere As Integer ' track where we are
Dim intX As Integer ' counter
strResult = "" ' set starting string to null
lngLength = Len(strSource)
MsgBox lngLength
For intX = 1 To lngLength
intWhere = InStr(intX, strSource, strEliminate) ' see if space is in remainder of text
If intWhere = 0 Then
strResult = strResult & MID$(strSource, intX, lngLength - intX + 1)
intX = lngLength + 1
Else
strResult = strResult & MID$(strSource, intX, intWhere - intX)
intX = intWhere
End If
Next intX
' display initial string and stripped result
MsgBox "starting string: " & strSource & vbNewLine & "result: " & strResult

This should do what you want.

This discussion is closed to new replies.