Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace in an unbound text box

Status
Not open for further replies.

mswilson16

Programmer
Nov 20, 2001
243
US
I have a form that has many unbound text box's on it. One will be filled with text already (via an open statement on the form).

What I need is, when Text1 is filled in Text2 is then search to see if the result from Text1 is in it. If it is then it will need replacing with done.

eg.
Text 2 = Martin, Matthew, John, Chris

Text1 = "Matthew".

Now what I need is to search text2 and replace Matthew with Done.

I have had to make this example up as to give you a really example would be to long winded.

I need this asap so if anyone can help please do.

Cheers
 
On the AfterUpdate event (or BeforeUpdate event depending on how you want to handle it), do the following

Private Sub Text1_AfterUpdate()

If (InStr(Text2, Text1) > 0) Then
Text2 = "Done"
Else
MsgBox "No Match"
End If

End Sub
 
cheers fancy,

The only problem is that when text1 is not a complete match to text2 the given code does not work. I need it so that when part of Text2 is found in text1 it then replaced with "Done".

eg.
text1 = Find this
text2 = Find this, find that, find what

Now the vb will replace "find this", with "done".

Any help will be great.
 
Sorry, misunderstood what you were trying to do. The following code should handle it:

Private Sub Text1_AfterUpdate()

Dim i As Integer

i = InStr(Text2, Text1)
If (i > 0) Then

If (i = 1) Then
Text2 = "Done" & Mid(Text2, Len(Text1) + 1)
Else
Text2 = Mid(Text2, 1, i - 1) & "Done" & Mid(Text2, i + Len(Text1))
End If
Else
MsgBox "No Match"
End If

End Sub
 
What was I thinking???? Here is an easier way.

Private Sub Text1_AfterUpdate()

Text2 = Replace(Text2, Text1, "Done")

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top