Home   |   How-Tos   |   Tutorials   |   Web Development   |   Local News   |   About

Monday, April 21, 2014

Visual Basic 6: How to add item in ComboBox with custom ListIndex

This tutorial will teach you to add an item to ComboBox with custom index (integer value) such as from a table primary key and selecting it programmatically. The Visual Basic 6 has not much functionality to implement this with ease but a simple workaround can get your program work as expected.

Add item to ComboBox with custom ListIndex

The following code snippet add an item to combobox with a custom index.  Let's say it has two fields namely id and name. The first line will add the name to the combobox, and the second will assign a new index to the newly added item.

Combo1.AddItem rs("name")
Combo1.ItemData(Combo1.NewIndex) = rs("id")

Calling the assigned custom ListIndex value

Now, to call the index value of a selected item we use the following code. This code will just display the custom index assigned to the ComboBox control. You can also assigned this to any variable on your program, but this time just to show you the value using MsgBox function.

Msgbox Combo1.ItemData(Combo1.ListIndex)

Assigning the Combobox with a specific ListIndex value (preferably integer value)

The most common way to assign a value into Combobox control is by using the .Text property. Sometimes we need to assign a value to a Combobox by a using specific integer value (e.g. from a table primary key field).  Unforunately, there is no built-in property to assign value into ComboBox programmatically in Visual Basic 6 using a specific integer value (not by a text value). But a simple workaround will do the job. Below is a simple loop which will stop selecting an item when the integer value is equal to the ItemData's index. This will mimic loading or selecting the correct item based on the comparison value.

Dim i As Integer
For i = 0 To Combo1.ListCount - 1
    Combo1.ListIndex = i       
    If Combo1.ItemData(Combo1.ListIndex) = rs("id") Then
        Exit Sub
    End If
Next i

This way, you can select an item in ComboBox control programmatically using a defined value or variable.

But this has disadvantage over large records because it loops all the way down to the target index value. Let's say we have a hundred thousand records, and the index is located somewhere near the last record. So the processing is CPU intensive. But this situation is rare for ComboBox usage. Overall, this is a good workaround to programmatically fill-up or select a ComboBox item.
  • Stumble This
  • Fav This With Technorati
  • Add To Del.icio.us
  • Digg This
  • Add To Facebook
  • Add To Yahoo

1 comments:

Anonymous said...

Visual Basic 6: How To Add Item In Combobox With Custom Listindex >>>>> Download Now

>>>>> Download Full

Visual Basic 6: How To Add Item In Combobox With Custom Listindex >>>>> Download LINK

>>>>> Download Now

Visual Basic 6: How To Add Item In Combobox With Custom Listindex >>>>> Download Full

>>>>> Download LINK

Post a Comment