Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
Solved

# Searching and replacing words from a text box

Posted on 2003-03-17
Medium Priority
192 Views
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".
0
Question by:macaila
• 3
• 2
• 2
• +3

LVL 18

Accepted Solution

bobbit31 earned 150 total points
ID: 8154736
textbox1.text = Replace(textbox1.text, "word", "word number 2")
0

Expert Comment

ID: 8154749
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)
0

LVL 3

Expert Comment

ID: 8154790
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
0

LVL 2

Expert Comment

ID: 8154797
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
0

LVL 14

Expert Comment

ID: 8154999
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 ?
0

LVL 18

Expert Comment

ID: 8155085
lol, my thoughts exactly ;)
0

LVL 3

Expert Comment

ID: 8155355
Some like it complex.
And it is only with an educational purpose.
0

Author Comment

ID: 8158621
I'll accept one answer as soon as I get VB6 I use VB4 so I get error messages.
0

Author Comment

ID: 8173943
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...
0

LVL 18

Expert Comment

ID: 8174308
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
###### Suggested Courses
Course of the Month14 days, left to enroll