Link to home
Start Free TrialLog in
Avatar of macaila
macaila

asked on

Searching and replacing words from a text box

If i have a textbox with some text in it, how cand i search and replace a word let's say "word" with some other words "word number 2".
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kidgenius2002
kidgenius2002

if you know what the word is, and it's length, you could do something like the following

x=1
'z is length of "word", in this case 4'
z= 4
do until mid(textbox1.text,x,z) = "word"
     x=1+x
     loop
textbox1.text = left(textbox1.text,x) & "word number 2" & right(textbox1.ext,x+z)
Or the old method:

a = "1234 word 5678"
 strToSearch = "word"
 strReplaceBy = "word number 2"
 start = 1
While InStr(start, a, strToSearch)
 a = Mid(a, 1, InStr(start, a, strToSearch) - 1) & strReplaceBy & Mid(a, InStr(start, a, strToSearch) + Len(strToSearch))
 start = start + Len(strReplaceBy)
Wend
Here you go.

Private Sub Command1_Click()
Text1.Text = Replace(Text1.Text, "word", _
                "word number 2")

Text1.Text = Replace(Text1.Text, "word", _
                "word")

Text1.SelStart = Len(Text1.Text)

End Sub
Avatar of aelatik
OK, i think im missing something here ! What is wrong with bobbit31's post. It's the best answer that fits this question, why make it so complex ?
lol, my thoughts exactly ;)
Some like it complex.
And it is only with an educational purpose.
Avatar of macaila

ASKER

I'll accept one answer as soon as I get VB6 I use VB4 so I get error messages.
Avatar of macaila

ASKER

Well bobbit31 was the first person who answered me and i try not to make my programs complex because that would mean they wouldn't run on older computers...
so why the 'B' grade?