Results 1 to 6 of 6

Thread: replace function wont' work

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    replace function wont' work

    Hi guys,

    I created a basic program which replaced hello with Hey and bye Cya, it took the word found if it needed to be replaced if it did it would send the replacement word into a second text box. This program was kinda working, but it has stopped dont know why. COuld you please have a look any help would be appreciated.

    CAz

    VB Code:
    1. Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
    2. Dim Word As String
    3. Dim Prts() As String
    4. Dim NewWord As String
    5.  
    6.     If KeyCode = vbKeySpace Then
    7.         Prts = Split(Text2.Text, " ")
    8.        
    9.         Word = Prts(UBound(Prts) - 1)
    10.         Select Case LCase(Word)
    11.             Case "hello": NewWord = "Hey"
    12.             Case "bye": NewWord = "Cya"
    13.             Case Else: NewWord = Word
    14.         End Select
    15.        
    16.         Text1.Text = Replace(Text2.Text, Word, NewWord)
    17.         Text1.SelStart = Len(Text2.Text)
    18.     End If
    19. End Sub

  2. #2
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: replace function wont' work

    Why not simply use InStr to see if one of the two words exists in the string (without splitting it), and if it does, do a Replace, and add the word to the textbox?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: replace function wont' work

    would i still be able to do this at real time?

    caz

  4. #4
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: replace function wont' work

    Of course.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2005
    Posts
    88

    Re: replace function wont' work

    okay thanks, I got the other program but found a fundamental error as it only replaces one word and once you type in another word it just changes all the other word back to their origrinal format.

    I will try and use the InStr command that you suggested, but as i have never used it might take some time.

    Thanks alot.

    Caz

  6. #6
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: replace function wont' work

    InStr simply tells you the position of one string within another string. But it can be used to detect if one string is located in another. If InStr returns a value different than zero, it means that string1 contains string2 (and when that is true you use replace to replace the word).

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