|
-
Apr 17th, 2000, 11:11 PM
#1
Thread Starter
Hyperactive Member
When I try to run this code (thanks to MartinLiss for the Replacetext Function), I get an error about Parameter Type Mismatch.
I've tried running the module with actually text like:
Code:
x = Replacetext("Hello There", "ello", "i")
And that works fine.
I've really never used functions before, but from what I've looked up, I'm doing it right. What's wrong?
Thanks,
JazzBass 
Code:
Sub Command1_Click
Dim sString, sFind, sReplace As String
sString = InputBox$("Please enter some text")
sFind = InputBox$("Please enter the text you want to replace from " & sString)
sReplace = InputBox$("Please enter the text you want to replace from " & sString)
x = ReplaceText(sString, sFind, sReplace)
msg = "The starting string was: " & sString
msg = msg & Chr$(13) & Chr$(10) & "The string to find was: " & sFind
msg = msg & Chr$(13) & Chr$(10) & "The string to replace was: " & sReplace
'
msg = msg & Chr$(13) & Chr$(10) & Chr$(13) & Chr$(10) & "The New String is: " & sNewText
End Sub
In Module:
Code:
Function ReplaceText (sString As String, sFind As String, sReplace As String) As String
Dim intPos As Integer
intPos = 999
If sFind <> sReplace Then
Do Until intPos = 0
intPos = InStr(1, sString, sFind)
If intPos > 0 Then
sString = Left$(sString, intPos - 1) & sReplace & Right$(sString, Len(sString) - intPos - Len(sFind) + 1)
End If
Loop
End If
sNewText = sString
End Function
[Edited by JazzBass on 04-18-2000 at 10:23 AM]
JazzBass
In the .NET era
Trying to remember VB6
Progress: 
XP Professional @ Home
and @ the Office
-
Apr 17th, 2000, 11:17 PM
#2
-
Apr 17th, 2000, 11:26 PM
#3
Thread Starter
Hyperactive Member
Thanks, but that was not it
Bonker,
Thanks, but that was not it. Were you talking about when I call the function or where? I still have the same problem at the line
Code:
x = ReplaceText(sString, sFind, sReplace)
Parameter Type Mismatch
Please help.
JazzBass 
JazzBass
In the .NET era
Trying to remember VB6
Progress: 
XP Professional @ Home
and @ the Office
-
Apr 17th, 2000, 11:42 PM
#4
Fanatic Member
or you could make things a lot, lot simpler and use VB's built in replace function. DOH!
Code:
x = Replace (sString, sFind, SReplace)
[Edited by Iain17 on 04-18-2000 at 05:44 PM]
Iain, thats with an i by the way!
-
Apr 17th, 2000, 11:56 PM
#5
Thread Starter
Hyperactive Member
No can Do
Lain17,
If only I could. I'm at work and I have to use vb3.
Any other ideas.
JazzBass
JazzBass
In the .NET era
Trying to remember VB6
Progress: 
XP Professional @ Home
and @ the Office
-
Apr 18th, 2000, 12:34 AM
#6
Hyperactive Member
Hi, JazzBass.
You get type mismatch error, because you declare sString, sFind as Variant type.
You should do:
Code:
Dim sString As String, sFind As String, sReplace As String
Second:
x = ReplaceText(sString, sFind, sReplace)
x is empty. Put Debug.Print x and you will see it.
Add as the last row in Command1_Click and you will see all string values.
sString holds your new value
Try this:
Code:
Public Function ReplaceText(sString As String, sFind As String, sReplace As String) As String
Dim intPos As Integer
Dim sNewText As String
intPos = 999
If sFind <> sReplace Then
Do Until intPos = 0
intPos = InStr(1, sString, sFind)
If intPos > 0 Then
sString = Left$(sString, intPos - 1) & sReplace & Right$(sString, Len(sString) - intPos - Len(sFind) + 1)
End If
Loop
End If
sNewText = sString
End Function
Private Sub Command1_Click()
Dim sString As String, sFind As String, sReplace As String
sString = InputBox$("Please enter some text")
sFind = InputBox$("Please enter the text you want to replace from ")
sReplace = InputBox$("Please enter the text you want to replace from ")
Call ReplaceText(sString, sFind, sReplace)
MsgBox "The New String is: " & sString
End Sub
OR
Code:
Public Function ReplaceText(sString As String, sFind As String, sReplace As String) As String
Dim intPos As Integer
Dim sNewText As String
intPos = 999
If sFind <> sReplace Then
Do Until intPos = 0
intPos = InStr(1, sString, sFind)
If intPos > 0 Then
sString = Left$(sString, intPos - 1) & sReplace & Right$(sString, Len(sString) - intPos - Len(sFind) + 1)
End If
Loop
End If
ReplaceText = sString
End Function
Private Sub Command1_Click()
Dim sString As String, sFind As String, sReplace As String, x As String
sString = InputBox$("Please enter some text")
sFind = InputBox$("Please enter the text you want to replace from " & sString)
sReplace = InputBox$("Please enter the text you want to replace from " & sString)
x = ReplaceText(sString, sFind, sReplace)
msg = "The New String is: " & x
MsgBox msg
End Sub
Larisa
[Edited by LG on 04-18-2000 at 02:00 PM]
-
Apr 18th, 2000, 12:43 AM
#7
Fanatic Member
Mr. JazzBass
Here is how you write a QUICK REPLACE function.
Sub Command1_Click()
Dim sString As String
Dim sFind As String
Dim sReplace As String
sString = InputBox$("Please enter some text")
sFind = InputBox$("Please enter the text you want to replace from " & sString)
sReplace = InputBox$("Please enter the text you want to replace from " & sString)
Dim msg As String
msg = "The starting string was: " & sString
msg = msg & vbCrLf & "The string to find was: " & sFind
msg = msg & vbCrLf & "The string to replace was: " & sReplace
MsgBox msg & vbCrLf & vbCrLf & "The New String is: " & ReplaceText(sString, sFind, sReplace)
End Sub
Public Function ReplaceText(sString As String, sFind As String, sReplace As String) As String
Dim intPos As Integer
intPos = 1
Do
intPos = InStr(intPos, sString, sFind)
If intPos = 0 Then Exit Do
sString = Left$(sString, intPos - 1) & sReplace & Mid$(sString, intPos + 1)
Loop
ReplaceText = sString
End Function
REASON WHY IT DID NOT WORK
You declare certain variables wrong.
*****Dim sString, sFind, sReplace As String
sString and sFind are considered as a variant
Your function has some missing logic.
Another thing, when you write function, make sure you put either "Private" or "Public" for good coding pratice.
[Edited by Nitro on 04-18-2000 at 10:47 AM]
Chemically Formulated As:
Dr. Nitro
-
Apr 18th, 2000, 01:07 AM
#8
Fanatic Member
There is really no need to use a function here because you don't return a value and you also need to pass sFind and sReplace By Value instead of the default By Reference because you don't want to change the values of sFind and sReplace.
change the declaration to this:
Code:
Public Sub ReplaceText(sString as String, ByVal sFind as String, ByVal sReplace as String)
And then instead of doing this when calling the procedure:
Code:
x = ReplaceText(sString, sFind, sReplace)
Do this and you don't have to worry about the variable x, which is useless in it's current state
Code:
Call ReplaceText(sString, sFind, sReplace)
Good Luck

{Insert random techno-babble here}
{Insert quote from some long gone mofo here}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|