Results 1 to 4 of 4

Thread: Combobox holding surnames from Access

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Cork, Ireland
    Posts
    14

    Combobox holding surnames from Access

    A pointer here would be great.

    I have a form in which I want to display data contained in an Employees table in Access.

    On loading, I have populated a Combobox on the form with people's surnames selected from the Employees table. When I select one of these surnames I want to get the whole row from the Employees table associated with that person and fill in the relevant employee details on the form.

    I think I need something like:

    [VB]cmd.CommandText = "SELECT * FROM Employees WHERE EmployeeID=" & EmployeeID[/VB]

    but I only have the surname on the Combobox to go with and if there are multiples of identical surnames in the table I'll only ever retrieve the first entry with that surname. Is there a way to 'hide' the ID in the Combobox maybe???

  2. #2
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    What I usually do in this scenario is I don't bind the combo box with my recordset. When I retreive data,I read the records one at a time, build a string that has the name and the record ID, then add it to my dropdown.

    So if you have the following data for example:

    ID | NAME
    ---+------
    100 APPLE
    101 BANANA
    102 ORANGE
    103 GRAPES


    Build a string where you have the name of the fruit and then on the 18th character, your record ID starts (just make sure that the ID is not visible on the combo box so you may have to play around on the number of characters where your ID starts).

    VB Code:
    1. 123456789012345678
    2. APPLE            100
    3. BANANA           101
    4. ORANGE           102
    5. GRAPES           103
    So basically, the user only see the names of the fruit since the ID is invisible to them.

    Now, when a user clicks on a selection within the combo box, on the "ComboBox1_SelectedIndexChanged", extract the ID from the combobox text and use that to get your data.

    Now in your case, you would still have problems if you are just showing the lastnames in your combobox. Let us say you have 2 persons with last name CLINTON. Even with a hidden ID, you would still never know which one among the two is for BILL or HILLARY. You would only find out after clicking on one of them to display the information. So I would go for adding the first name initial.

    CLINTON B 105
    CLINTON H 106


    Hope this helps.

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try searching here or in the help files for ValueMember.

  4. #4

    Thread Starter
    New Member
    Join Date
    Feb 2004
    Location
    Cork, Ireland
    Posts
    14
    Thanks for replies! I got it working using ValueMember. Here is the code to populate the combobox:
    PHP Code:
    Do Until Not nextResult
        
    While reader.Read()
            
    tempEmployeeID reader.Item(0).ToString()
            
    EmployeeName reader.Item(1).ToString() + ", " reader.Item(2).ToString()

            
    cmbSurname.DisplayMember EmployeeName
            cmbSurname
    .Items.Add(EmployeeName)
            
    cmbSurname.ValueMember tempEmployeeID

            End 
    While
            
    nextResult reader.NextResult()
    Loop 
    I am not sure what the

    cmbSurname.DisplayMember = EmployeeName

    does as it alone didn't put anything in the combobox.

    cmbSurname.Items.Add(EmployeeName) did though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width