help using data in combo boxes


Whats the best way of storing a list of 2 columns in a combobox ?
Ie I have a list of forenames  and a list of surnames. I only want to show the forenames but when I pick the forename I wish to use the surname in code.

any ideas?

Who is Participating?
TimCotteeConnect With a Mentor Commented:
There are a whole host of possibilies here:

1) Use another combo box or listbox to store the surnames and get the appropriate surname by referencing the .List(n) property of the other control.

2) Store the information in an array and get one or both parts from the array referenced by the .ListIndex property of the combo box to get the appropriate element(s).

3) Store the info in a collection, similar idea to 2)

And possibly many more!

The easiest is perhaps 1) though the others may be more useful in some circumstances.
Take my OCX, it can handle mutiple columns and much more ...
BY the way, meant to say that in method 1) you would set the visible property of the partner control to false so that it is not displayed to the user.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Put the surenames in  collection like this:

Dim Mysurenames As Collection

Set Mysurenames = new Collection
Mysurenames.Add "Smith" , "John"

Then you can access the related surename with:
str_surename = Mysurenames("John")

Best regards
WoK ;o)

Éric MoreauSenior .Net ConsultantCommented:
VK, where can we try your OCX?
ComboBox has a ItemData property that is used for this purpose (Display one thing and pass another thing)
For eg. If you have the forname and surname in a array
with cmb1
        For Idx = 0 To UBound(vDataArray, 2)
            .AddItem vDataArray(1, Idx) 'forename
            .ItemData(Idx) = vDataArray(0, Idx) 'surname
end with

Hope it help
GoodJun, using the itemdata property when you already have an array is irrelevant because you can infer the element from the .ListIndex property. It would be handy if the itemdata property stored something other than an integer but it doesn't so in reality you are using an array method as I suggested earlier.
BBaronAuthor Commented:
Cheers Tim,
  Im going with the collection suggestion
 ps watch out for my next one!

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.