Board index » Visual Studio » replace function

replace function

Visual Studio281
I have a large VB6 program that I originally started in VB 5 and in

several places I used the Replace function. I needed to put some addins

and some references into the project to get the Replace function

accepted by the compiler. Recently while making some changes, I seem to

have done something to make the compiler reject the function and I seem

to be missing something in the references. Can someone give me a clue as

to what I missed or is there a function equivalent to Replace.


-
 

Re:replace function

Replace is a built-in function of VB6. If you are now getting errors when

referencing Replace, ensure that



a) the first four selected (in order) items under project>references are:



visual basic for applications

visual basic runtime objects and procedures

visual basic objects and procedures

OLE automation



(The last one seems to be 'optional' as others have reported not having this

particular reference selected. However, selected is the out-of-box default

setting for OLE Automation.)



b) you have not named a variable with that name, and that you have not added

another control that exposes a Replace method. If you have, you can access

the VB method by referencing with VBA, e.g.



x = VBA.Replace(a, b, c)





--



Randy Birch

MS MVP Visual Basic

vbnet.mvps.org/">vbnet.mvps.org/



Please reply to the newsgroups so all can participate.









"Leon M" <lmintz1@verizon.net>wrote in message

I have a large VB6 program that I originally started in VB 5 and in

several places I used the Replace function. I needed to put some addins

and some references into the project to get the Replace function

accepted by the compiler. Recently while making some changes, I seem to

have done something to make the compiler reject the function and I seem

to be missing something in the references. Can someone give me a clue as

to what I missed or is there a function equivalent to Replace.



-

Re:replace function

Also the error pointing to the "Replace" function may be bogus.



The OP needs to check References and make sure nothing is marked "Missing".



-ralph





-

Re:replace function

"Leon M" <lmintz1@verizon.net>wrote in message



Quote
I have a large VB6 program that I originally started in VB 5 and

in several places I used the Replace function. I needed to put some

addins and some references into the project to get the Replace

function accepted by the compiler. Recently while making some

changes, I seem to have done something to make the compiler

reject the function . . .



If you have both VB5 and VB6 on your system are you sure that you are

currently using VB6? As you almost certainly know, VB5 does not have a

Replace function. Sounds like a daft question I know, but it is a mistake

I've made myself in the past. If you're interested, here is a coded Replace

function that works in both VB5 and VB6. I wrote it many years ago when VB5

was the latest version and so the function does not work in exactly the same

way as the VB6 function (which of course I did not know about at the time).

My version does not allow you to specify a start position and instead of

allowing you to specify the desired number of replaces it simply replaces

them all and returns the number of replaces made. It would be easy to modify

though if you want exactly the same functionality as VB6. There are faster

ways of doing the job of course, but the method I have used is quite fast.

In a native code compiled exe it averages out at about the same speed as the

VB6 function, and on very long strings with lots of replaces it is quite a

bit faster than VB6 Replace.



Mike Williams

MVP Visual Basic



Option Explicit



Private Function ReplaceAll(ByRef txtWork As String, ByVal _

txtFind As String, ByVal txtReplace As String, ByVal compare _

As Long, ByRef replacecount As Long) As String

Dim txtNew As String, Ltxtnew As Long

Dim Start As Long, Counter As Long, n As Long

Dim Lfind As Long, Lreplace As Long

Dim OldPointer As Long, NewPointer As Long

Dim txtTemp As String

replacecount = 0

Lfind = Len(txtFind)

If Lfind < 1 Then

ReplaceAll = txtWork

Exit Function

End If

Lreplace = Len(txtReplace)

' the following code avoids the need to use

' vbTextCompare (which is very slow)

If compare = vbTextCompare Then

txtTemp = UCase(txtWork)

txtFind = UCase(txtFind)

Else

txtTemp = txtWork

End If

Start = 1 - Lfind

Do

Start = InStr(Start + Lfind, txtTemp, txtFind, 0)

If Start <>0 Then Counter = Counter + 1 Else Exit Do

Loop

If Counter < 1 Then

ReplaceAll = txtWork

Exit Function

Else

Ltxtnew = Len(txtWork) + Counter * (Lreplace - Lfind)

txtNew = Space$(Ltxtnew)

Start = 1 - Lfind

OldPointer = 1

NewPointer = 1

On Error Resume Next

' the Resume Next is the easiest way to prevent

' errors being generated when the search word is

' exactly at the end of the text and the replace

' string is empty as it avoids other forms of

' check inside the loop slowing the code down

For n = 1 To Counter

Start = InStr(Start + Lfind, txtTemp, txtFind, 0)

Mid$(txtNew, NewPointer, Start - OldPointer) = _

Mid$(txtWork, OldPointer, Start - OldPointer)

NewPointer = NewPointer + Start - OldPointer + Lreplace

Mid$(txtNew, NewPointer - Lreplace, Lreplace) = txtReplace

OldPointer = Start + Lfind

Next n

On Error GoTo 0

If OldPointer <= Len(txtWork) Then

Mid$(txtNew, NewPointer) = Mid$(txtWork, OldPointer)

End If

End If

ReplaceAll = txtNew

replacecount = Counter

End Function



Private Sub Command1_Click()

Dim s1 As String, rcount As Long

s1 = "Whisky and Coke is one of my favourite drinks."

s1 = ReplaceAll(s1, "Coke", "spring water", vbTextCompare, rcount)

Print s1, rcount

End Sub

















-