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

Posted on 2006-04-18
Last Modified: 2008-02-01
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.
Question by:uzairp
    LVL 2

    Assisted Solution

    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
    LVL 12

    Accepted Solution

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    This article is filled with multiple code samples and explanations for mathematical calculations. They are as follows: 1. General tips 2. Quadratic formula 3. Object collision 4. Projectile path General Tips       Here are some of my tips f…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now