VB6 - Combo Control/Drop Down Question

Posted on 2005-03-31
Medium Priority
Last Modified: 2012-06-22
In HTML, if I want to display one thing, but get a different value I would do something like this:

<select name="combo">
<option value=1>John Doe</option>
<option value=2>Jane Smith</option>
<option value=3>Joe Blow</option>

How can I do the same thing (combo which assigns a value of 1 if John Doe is selected, etc) in VB6, figure using a combo control or maybe a list box?
Question by:Lee W, MVP
LVL 32

Accepted Solution

Erick37 earned 2000 total points
ID: 13677560
You can use the ItemData property to store this info:

ItemData Property Example
LVL 11

Expert Comment

ID: 13677562
Sure can, but it can only be integer data.

You can use the Combo1.ItemData(index as integer) property, when index is the index of the item

Add a combo box and paste the following (just select and item) to see how to do it:

Private Sub Combo1_Click()
 MsgBox Combo1.ItemData(Combo1.ListIndex)
End Sub

Private Sub Form_Load()
 Combo1.AddItem "first"
 Combo1.ItemData(0) = 1
 Combo1.AddItem "second"
 Combo1.ItemData(1) = 2
 Combo1.AddItem "third"
 Combo1.ItemData(2) = 3
End Sub
LVL 97

Author Comment

by:Lee W, MVP
ID: 13677581
I thought so... but the documentation I looked at didn't seem clear on this.  Thanks, I'll test things and let you both know.
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 13678252
u can also use NewIndex Property to get newly added entry
Private Sub Form_Load()
Dim i
    For i = 1 To 10
        Combo1.AddItem "Item " & i
        Combo1.ItemData(Combo1.NewIndex) = i
    Next i
End Sub

LVL 97

Author Comment

by:Lee W, MVP
ID: 13722409
Ok, working on it again now (if I don't finish today, won't get back to it until Monday).  That said, how do I retrieve BOTH the index and the text data?
LVL 97

Author Comment

by:Lee W, MVP
ID: 13722924
Thanks folks - have to select Erick37 as the answer - The link he provided was REALLY helpful and he did post first.  

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question