|
-
Dec 30th, 2005, 09:58 AM
#1
Thread Starter
New Member
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
-
Dec 30th, 2005, 11:17 AM
#2
Member
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:
Dim strSearch(30) as String
For i = 0 To UBound(strSearch) - 1
Search(i) = v(i).Text
Where = InStr(txtData.Text, Search(i))
If Where Then
txtData.SelStart = Where - 1 + 8
txtData.SelLength = Len(Search(i))
r(i).Text = txtData.SelText
End If
-
Dec 30th, 2005, 11:50 AM
#3
Re: Searching text for strings and moving it to text boxes
I agree.. use a control array
then try this :
VB Code:
Dim Lines() As String
Dim tmp() As String
Lines = Split(Trim(txtData), vbCrLf)
For x = 0 To 29
tmp = Split(Lines(x), " ")
v(x) = tmp(0)
r(x) = tmp(1)
Next
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Dec 30th, 2005, 12:07 PM
#4
Thread Starter
New Member
Re: Searching text for strings and moving it to text boxes
 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:
Dim strSearch(30) as String
For i = 0 To UBound(strSearch) - 1
Search(i) = v(i).Text
Where = InStr(txtData.Text, Search(i))
If Where Then
txtData.SelStart = Where - 1 + 8
txtData.SelLength = Len(Search(i))
r(i).Text = txtData.SelText
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.
-
Dec 30th, 2005, 04:32 PM
#5
Member
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:
Dim strSearch(30) as String
For i = 0 To UBound(strSearch) - 1
strSearch(i) = v(i).Text
Where = InStr(txtData.Text, strSearch(i))
If Where Then
txtData.SelStart = Where - 1 + 8
txtData.SelLength = Len(strSearch(i))
r(i).Text = txtData.SelText
End If
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:
Dim intPos as Integer
For i = 0 To 29
intPos = intPos + 1
Where = InStr(intPos, txtData.Text, "server")
If Where Then
iPos = Where
txtData.SelStart = Where - 1
txtData.SelLength = 8
v(i).Text = Trim(txtData.SelText)
End If
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|