control array

I have a control array(comboboxes), and I need to compare values of two comboboxes at run time after user inputs some values in comboboxes. How do I refer to these two controls?
sshayevichAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Erick37Connect With a Mentor Commented:
Private Sub Command1_Click()

Dim i As Long, j As Long
For i = Combo1.LBound To Combo1.UBound
    For j = i + 1 To Combo1.UBound
        If Combo1(i).Text = Combo1(j).Text Then
            MsgBox "Matched Values: " & Combo1(i).Text & " = " & Combo1(j).Text
            Exit Sub
        End If
    Next
Next

End Sub
0
 
DalinCommented:
Use the Index to refer to the element
0
 
baitzCommented:
Yes! You have to use the index to refer the array element. If you send me a e-mail with the code, I can reply...

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
baitzCommented:
For example,

if combo(1).text = combo(2).text then
   beep
End If

Ok?!?
0
 
sshayevichAuthor Commented:
My user selects values from comboboxes.According to scenario two comboboxes can't have same values selected. So after they're done selecting i have to look at the values, and if there's 2 same values found, a prompts should be put up. So, I don't know which index to refer to.
0
 
Erick37Commented:
Compare two members of the array:

If Combo1(0).Text = Combo1(1).Text Then
    'They match
End If


Or look at each control in the array:

Dim i As Long
For i = Combo1.LBound To Combo1.UBound
    Debug.Print Combo1(i).Text
Next
0
 
DalinCommented:
Example:

If Combo1(2).Text = Combo1(3).Text then
'some code
End If

In the combo's event, the index will pass to you to indicate which element you are in
Private Sub Combo1_Click(Index as Integer)
If Index= 0 then
      If Combo1(Index).Text = Combo1(1).Text then
         End If
    Else
      If Combo1(index).Text = Combo1(0).Text then
         End If
    End If

  End sub
0
 
sshayevichAuthor Commented:
I've tried this code before, and I get the msgbox"same values" no matter wether there is or there's no duplicate value found.
0
 
Erick37Commented:
Make sure you are not indexing the same control. Use

Debug.Print i; j

just before

MsgBox ...

to confirm they are not equal.
0
 
abaldwinCommented:
On another note.

Where are the list for the combo boxes getting their data.  Is it from a DB table?

One thing you could do is to put code in the lost focus event of the combo boxes and rebuild the list excluding any value selected.

Just a rough idea.

Andy
0
 
sshayevichAuthor Commented:
Thank you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.