Results 1 to 5 of 5

Thread: Searching text for strings and moving it to text boxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    2

    Searching text for strings and moving it to text boxes

    Hey all,

    Got a quick question about the best way to search text.


    I currently search a textbox (txtdata.text) for a set of about 30 words (Ex:server1) as shown below:

    Server1 88,324
    Server2 65,343
    Server3 54,789
    Server4 99,324
    ~
    Server30 12,234

    The 1st value "server" is placed in a text box called V#.text and the associated amount is copied to a 2nd text box called R#.text.

    In order to separate the 2 values I use this code:

    Search21 = v21.Text
    Where = InStr(txtData.Text, Search21)
    If Where Then
    'txtData.SetFocus
    txtData.SelStart = Where - 1 + 8
    txtData.SelLength = Len(Search21)
    r21.Text = txtData.SelText
    End If

    I need this to be repeated 30 times over to get all 30 values in their boxes.

    This is a huge pain & probably the worst way to do it but it works.


    Anyone have a better idea on how to do this?

    Thanks

  2. #2
    Member
    Join Date
    Dec 2005
    Posts
    63

    Re: Searching text for strings and moving it to text boxes

    Will the number of words always be the same? If (for example) it's always going to be 30, then, you could just use an array, control arrays, and a loop.

    Instead of naming your textboxes v1, v2, v3. . .v30, use a control array. Name them all v, and in the index property, set the indexes to whichever order you need (it'll be 0-based, though. The indexes will start at 0 and go to 29).

    VB Code:
    1. Dim strSearch(30) as String
    2.  
    3. For i = 0 To UBound(strSearch) - 1
    4. Search(i) = v(i).Text
    5. Where = InStr(txtData.Text, Search(i))
    6. If Where Then
    7. txtData.SelStart = Where - 1 + 8
    8. txtData.SelLength = Len(Search(i))
    9. r(i).Text = txtData.SelText
    10. End If



  3. #3
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Searching text for strings and moving it to text boxes

    I agree.. use a control array
    then try this :

    VB Code:
    1. Dim Lines() As String
    2. Dim tmp() As String
    3.  
    4. Lines = Split(Trim(txtData), vbCrLf)
    5. For x = 0 To 29
    6.     tmp = Split(Lines(x), " ")
    7.     v(x) = tmp(0)
    8.     r(x) = tmp(1)
    9. Next
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2005
    Posts
    2

    Re: Searching text for strings and moving it to text boxes

    Quote Originally Posted by MarkDorf
    Will the number of words always be the same? If (for example) it's always going to be 30, then, you could just use an array, control arrays, and a loop.

    Instead of naming your textboxes v1, v2, v3. . .v30, use a control array. Name them all v, and in the index property, set the indexes to whichever order you need (it'll be 0-based, though. The indexes will start at 0 and go to 29).

    VB Code:
    1. Dim strSearch(30) as String
    2.  
    3. For i = 0 To UBound(strSearch) - 1
    4. Search(i) = v(i).Text
    5. Where = InStr(txtData.Text, Search(i))
    6. If Where Then
    7. txtData.SelStart = Where - 1 + 8
    8. txtData.SelLength = Len(Search(i))
    9. r(i).Text = txtData.SelText
    10. End If

    Hey Thanks for responding,

    This is gonna be my 1st array.

    I have included a pic of what I am trying to build below:




    I am not sure where in your code the server names I am looking for are set.

    Thanks again for your help.

  5. #5
    Member
    Join Date
    Dec 2005
    Posts
    63

    Re: Searching text for strings and moving it to text boxes

    The array (I called it strSearch) can be declared anywhere in the function/sub that'll be doing this work.

    The textboxes that will hold the server names should all be named "v". When you name the 2nd textbox "v", Visual Basic will pop up a box that says "You already have a control named "v". Do you want to create a control array?" Click "Yes". Then, if you select a textbox and look at the Properties window, you'll see a property called "Index". Start at the top textbox, and set the Index for that textbox to 0 (zero). Go to the second textbox, and set the Index to 1, and so forth.

    The textboxes that will hold the values should all be named "r". Set the indexes for these textboxes just like you did for the other control array.

    I made a mistake in the code I gave you before, too.

    VB Code:
    1. Dim strSearch(30) as String
    2.  
    3. For i = 0 To UBound(strSearch) - 1
    4.     strSearch(i) = v(i).Text
    5.     Where = InStr(txtData.Text, strSearch(i))
    6.     If Where Then
    7.         txtData.SelStart = Where - 1 + 8
    8.         txtData.SelLength = Len(strSearch(i))
    9.         r(i).Text = txtData.SelText
    10.     End If
    11. Next i

    Do you kind of see what I'm doing now? I just took your code and adapted it to use arrays.

    EDIT: OK, I see what you were asking about where the server names are set. You didn't include that code, but it would be similar:

    VB Code:
    1. Dim intPos as Integer
    2.  
    3. For i = 0 To 29
    4.     intPos = intPos + 1
    5.     Where = InStr(intPos, txtData.Text, "server")
    6.     If Where Then
    7.         iPos = Where
    8.         txtData.SelStart = Where - 1
    9.         txtData.SelLength = 8
    10.         v(i).Text = Trim(txtData.SelText)
    11.     End If
    12. Next i
    Last edited by MarkDorf; Dec 30th, 2005 at 04:53 PM.



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