how to fill a combobox with item and index 
Author Message
 how to fill a combobox with item and index

i've got a class for a table
with a for each i fill a combobox with items
how do i make a unique key in the listitem so i can retriev that value
easlily when a user clicks?

the id field in the table  startst at 1 but is not seqeuntial
thanks

wiemer



Tue, 17 Sep 2002 03:00:00 GMT  
 how to fill a combobox with item and index
Wiemer,

You can place your unique ID field into the combobox's Itemdata property.
When the user clicks on an item in the combobox you can retrieve the ID from
the Itemdata property and use it to retrieve the appropriate record.

For example:

Private Sub FillList()

    'Clear the combobox
    cboNames.Clear

    'Move to the first record in the recordset
    rsPhonebook.MoveFirst
    'Do while the recordset has not reached end-of-file
    Do While Not rsPhonebook.EOF
        'Add the Name field to the combobox
        cboNames.AddItem rsPhonebook("Name")
        'Place the ID field into the hidden Itemdata property
        cboNames.ItemData(cboNames.NewIndex) = rsPhonebook("ID")
        'Move to the next record in the recordset
        rsPhonebook.MoveNext
    Loop

End Sub

Then, when an item in the combobox is clicked:

Private Sub cboNames_Click()

    Dim myID As Long

    'if an item is highlighted in the combobox
    If cboNames.ListIndex >= 0 Then
        'retrieve the ID from the Itemdata for the highlighted item
        myID = cboNames.ItemData(cboNames.ListIndex)
        'Find the first record with a matching ID
        rsPhonebook.FindFirst "ID = " & myID
        'If the record with the matching ID was found
        If rsPhonebook.NoMatch = False Then
            'Display the record
            ShowFields
        Else
            'Otherwise, indicate not found
            MsgBox "ID not found", vbCritical, "Find Error"
        End If
    End If

End Sub

Hope this helps,

Kerry Moorman



Quote:
> i've got a class for a table
> with a for each i fill a combobox with items
> how do i make a unique key in the listitem so i can retriev that value
> easlily when a user clicks?

> the id field in the table  startst at 1 but is not seqeuntial
> thanks

> wiemer



Tue, 17 Sep 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Reference ComboBox item to fill ListBox

2. Filling a Secondary Combobox based on information from a primary combobox

3. How do you return the index of a combobox that corresponds to a string value that exists as an item in the combobox?

4. DAO - Fill in foreign key with correct PK index from a string-data table

5. Programatically fill a combobox A2K ??

6. Using the pipe symbol to fill a listbox or combobox

7. ComboBox Custom Fill Functions

8. fill combobox with contents directory

9. Fill a combobox

10. How to clear a filled combobox

11. Filling a comboBox with ODBC-data

12. Filling a ComboBox with data from DB

 

 
Powered by phpBB® Forum Software