• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 927
  • Last Modified:

VB6 Combo Box - Need to display Emp name but send Empid to the database

Hi Experts,
I am using Combo box in VB6. I am loading this combo box with employee names from database (in form_load event). I see two properties for combo box, one being datafield and the other being 'list' but I dont know how I can use them.
Could I use one of these columns, which does not display, to hold empid and once the user selects an Employee name, then I send the corresponding empid from that field to the database? Please suggest with code examples.
2 Solutions
Omar McKoyHead of Infrastructure OperationsCommented:
This is possible through using the ItemData property.

' Cycle through the result set and populate combobox
Do While Rs.EOF = False
                ' Add new item to "Employee" Combobox
                cmbEmpployee.AddItem Rs![<Emplyee Name>]
                cmbEmpployee.ItemData(cmbCloseFor.NewIndex) = Rs![<Employee ID>]

' To retrieve the employee id
Mortaza DoulatyCommented:
something like this:

    Dim i As Integer

    While Not rst.EOF
        Combo1.AddItem rst!Name
        Combo1.ItemData(i) = rst!EmployeeID
        i = i + 1

But something may not be a good news! and that is: the ItemData only accepts numeric data.
In order to do what you said, you can use an array which holds the EmloyeeID, the index of the comobe and that array, should be the same. (but not neccesary)
The code might look like:

    Dim i As Integer
    Dim Arr() As String
    While Not rst.EOF
        Combo1.AddItem rst!Name

        ReDim Preserve Arr(i) As String
        Arr(i) = rst!EmployeeID
        i = i + 1

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now