glenart10,
Also, you probably want to add an Exit For statement after finding your entry...
Regards,
Patrick
Main Topics
Browse All TopicsI am trying to use a collection object to store the values that are in 10 text boxes on my form.
Then I try to see if any of those values match up with a specific value called HitBox (it could be anything).
If there is no match, I want the routine to give a message box telling the user to try entering the correct value in one
of the text boxes in the form.
My text box names are Tab_Name_i where i =1 to 10
STATUS_TEXT is a textbox object which gives the user periodic update information.
Here is what I have so far which does NOT blow up but does not work either.
Dim Col As New Collection
Dim i As Integer
' Get data from textboxes on Country_Tab_Name Form
Col.Add Me.Tab_Name_1
Col.Add Me.Tab_Name_2
Col.Add Me.Tab_Name_3
Col.Add Me.Tab_Name_4
Col.Add Me.Tab_Name_5
Col.Add Me.Tab_Name_6
Col.Add Me.Tab_Name_7
Col.Add Me.Tab_Name_8
Col.Add Me.Tab_Name_9
Col.Add Me.Tab_Name_10
' Search collection object COL
For i = 1 To 10
If Col.Item(i) = "Hitbox" Then
Me.STATUS_TEXT.SetFocus
STATUS_TEXT.Text = "Found HitBox Tab"
Else
MsgBox ("The HitBox name/tab was not found on this form. ABORTING")
End If
Next i
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
BTW, unless you really need that collection, you could simplify your code using something like this:
Dim blnFound As Boolean
blnFound = False
For i = 1 To 10
If UCase$(CallByName(CallByNa
Me.STATUS_TEXT.SetFocus
STATUS_TEXT.Text = "Found HitBox Tab"
blnFound = True
Exit For
End If
Next
If blnFound = False Then
MsgBox ("The HitBox name/tab was not found on this form. ABORTING")
End If
Optionally, you can create a control array to iterate over your textboxes. Just create your first textbox named Tab_Name, for example, and set its Index property to 0. Then name the next textbox Tab_Name also and it should take on the Index value of 1, etc, until you have 10 textboxes. Then you can do:
For i = 0 To 9
If Me.Tab_Name(i).Text = "HitBox" Then
etc....
Glenart10, here's the full code you need...
Dim Col As New Collection
Dim i As Integer, Found As Boolean, CaseSensitive As Boolean, SearchText As String
Found = False
CaseSensitive = False 'if you want the search to be case sensitive replace it by true
SearchText = "Hitbox" 'here's the item you are searching for
Col.Add Me.Tab_Name_1.Text
Col.Add Me.Tab_Name_2.Text
Col.Add Me.Tab_Name_3.Text
Col.Add Me.Tab_Name_4.Text
Col.Add Me.Tab_Name_5.Text
Col.Add Me.Tab_Name_6.Text
Col.Add Me.Tab_Name_7.Text
Col.Add Me.Tab_Name_8.Text
Col.Add Me.Tab_Name_9.Text
Col.Add Me.Tab_Name_10.Text
For i = 1 To 10
If (CaseSensitive And Col.Item(i) = SearchText) Or (Not CaseSensitive And LCase(Col.Item(i)) = LCase(SearchText)) Then
Found = True
Exit For
End If
Next i
If Found Then
Me.STATUS_TEXT.SetFocus
STATUS_TEXT.Text = "Found HitBox Tab"
Else
MsgBox "Not Found"
End If
Hope this helps.. :)
Dan
optionally, and as azrasound also suggested...
i would have done it using control arrays...
my textboxes would be:
tab_name(0)
tab_name(1)
tab_name(2)
tab_name(3)
...
tab_name(9)
and the loop to check would become
for i=0 to 9
if tab_name(i).text="hitbox" then
found=true
exit for
end if
if found... (use code above)
Business Accounts
Answer for Membership
by: matthewspatrickPosted on 2006-10-05 at 17:50:25ID: 17673373
Hi glenart10,
> If Col.Item(i) = "Hitbox" Then
Remember that by default VB uses the binary comparison method, which is case-sensitive (and so Hitbox <> HitBox).
For text comparisons, it is always safest to either:
1) Make both sides of the comparison the same case (perhaps by using UCase of LCase) or
2) Using the StrComp function, with vbCompareText for the third argument (text comparison is *not* case-sensitive).
Regards,
Patrick