Results 1 to 14 of 14

Thread: find a string...sounds simple!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326

    find a string...sounds simple!

    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.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Dim p As Integer
    2.  
    3. p = InStr(1,Text1.Text, "Hello")
    4.  
    5. If p <> 0 Then
    6.  
    7.   Text1.SelStart = p
    8.   Text1.SelLength = Len("Hello")
    9.  
    10. Else
    11.  
    12.   MsgBox "Not Found!"
    13.  
    14. End If

    see if that works

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326
    nope, it is just adding the text at the front of the textbox. Thanks anyway.

  4. #4
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Dim p As Integer
    2.  
    3. p = InStr(1,Text1.Text, "Hello")
    4.  
    5. If p <> 0 Then
    6.  
    7.   Text1.SetFocus
    8.   Text1.SelStart = p-1
    9.   Text1.SelLength = Len("Hello")
    10.  
    11. Else
    12.  
    13.   MsgBox "Not Found!"
    14.  
    15. End If

    this modified version works for me

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    ???

    works like a charm for me as well

    peet

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    maby make it a bit more general...

    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
    then :

    Code:
    Private Sub Command1_Click()
        FindString Text1, "test"
    End Sub
    hope you don't mind me stealing you code crptcblade

    peet

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326
    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.

  8. #8
    Frenzied Member markman's Avatar
    Join Date
    Nov 2000
    Location
    Florida.
    Posts
    1,197
    check you dont have any code under text1_change() that would mess it up.
    retired member. Thanks for everything

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326
    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326
    I also need a nind next. It seems to just find the same one over and over

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326
    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?

  12. #12
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    eh... so you fixed it... then this isn't needed

  13. #13
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    but this is maybe of interrest :

    Code:
    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
    peet

    hmmm... what don't one do in order to increase the post count

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    who am I....and why do you want to know where I am?
    Posts
    326
    Worked like a charm!! thnx a lot

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