Results 1 to 8 of 8

Thread: Cannot get this function to work

  1. #1

    Thread Starter
    Hyperactive Member JazzBass's Avatar
    Join Date
    Jun 1999
    Posts
    393

    Unhappy

    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

  2. #2
    Fanatic Member Bonker Gudd's Avatar
    Join Date
    Mar 2000
    Location
    Saturn
    Posts
    748

    Angry

    Use " &" instead of "," to join the text together

  3. #3

    Thread Starter
    Hyperactive Member JazzBass's Avatar
    Join Date
    Jun 1999
    Posts
    393

    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

  4. #4
    Fanatic Member
    Join Date
    Mar 2000
    Location
    That posh bit of England known as Buckinghamshire
    Posts
    658

    Wink

    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!

  5. #5

    Thread Starter
    Hyperactive Member JazzBass's Avatar
    Join Date
    Jun 1999
    Posts
    393

    Wink 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

  6. #6
    Hyperactive Member
    Join Date
    Jun 1999
    Posts
    308
    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
    Code:
    MsgBox msg
    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]

  7. #7
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    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

  8. #8
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987

    Exclamation

    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
  •  



Click Here to Expand Forum to Full Width