Dose anyone know any code that will search a text box for a a string of text? I have been experementing with a loop but i havent yet gotten it.
Printable View
Dose anyone know any code that will search a text box for a a string of text? I have been experementing with a loop but i havent yet gotten it.
VB Code:
Dim p As Integer p = InStr(1,Text1.Text, "Hello") If p <> 0 Then Text1.SelStart = p Text1.SelLength = Len("Hello") Else MsgBox "Not Found!" End If
see if that works
:)
nope, it is just adding the text at the front of the textbox. Thanks anyway.
VB Code:
Dim p As Integer p = InStr(1,Text1.Text, "Hello") If p <> 0 Then Text1.SetFocus Text1.SelStart = p-1 Text1.SelLength = Len("Hello") Else MsgBox "Not Found!" End If
this modified version works for me
:)
???
works like a charm for me as well :)
peet
maby make it a bit more general...
then :Code:Private Sub FindString(T As TextBox, sSearchString As String)
Dim p As Integer
p = InStr(1, T.Text, sSearchString)
If p <> 0 Then
T.SetFocus
T.SelStart = p - 1
T.SelLength = Len(sSearchString)
'T.SelText
Else
MsgBox "Not Found!"
End If
End Sub
hope you don't mind me stealing you code crptcblade :DCode:Private Sub Command1_Click()
FindString Text1, "test"
End Sub
peet
i dont get it then. It places the text at the front of the string, then highlights it. For some reason, it dosent locate it but creates it.
check you dont have any code under text1_change() that would mess it up.
I think I figured it out. I am using a combo box for my text input. And the textbox I am searching is on another form. Somehow, this is messing it up.
I also need a nind next. It seems to just find the same one over and over
fixed it. Thanks, i did have code there, now I need one more thing, how can I make a find next procedure without it jujst finding the same text?
eh... so you fixed it... then this isn't needed :)
but this is maybe of interrest :
peetCode:
Option Explicit
Private iNextStartPos As Integer
Private Function FindString(T As TextBox, sSearchString As String, iStartPos As Integer) As Integer
Dim p As Integer
p = InStr(iStartPos, T.Text, sSearchString)
If p <> 0 Then
T.SetFocus
T.SelStart = p - 1
T.SelLength = Len(sSearchString)
FindString = p + Len(sSearchString)
'T.SelText
Else
FindString = 0
MsgBox "String " & """" & sSearchString & """" & " not found!"
End If
End Function
Private Sub cmdFind_Click()
iNextStartPos = FindString(Text1, "test", iNextStartPos)
If iNextStartPos Then
cmdFind.Caption = "Find next"
Else
cmdFind.Caption = "Find"
iNextStartPos = 1
End If
End Sub
Private Sub Form_Load()
iNextStartPos = 1
End Sub
hmmm... what don't one do in order to increase the post count ;)
Worked like a charm!!:) thnx a lot:) :)