Solved

Combo Boxes

Posted on 2000-03-21
2
193 Views
Last Modified: 2010-05-02
Hi,

How do I populate combo boxes with items? The list of items may increase later. How should I add the items now so that additional items can be added easily?

When I select an item in the combo box and click ok, I need to pop up another window that displays some details. How do I check which item I had selected and display the correct information?

Thanks!
0
Comment
Question by:wmlim060298
2 Comments
 
LVL 5

Accepted Solution

by:
Voodooman earned 20 total points
ID: 2639483
Use the Add Item method to add to the list.

Combo.addItem "Voodooman" ' a string value

You can do this any time.

To check the value use the textproperty of the combo

Dim mystring as string

mystring=Combo.text

You can also use the Change Event of the combo box to do things.  When the value changes you can pop up a box or whatever.

Beware the change event doesnt always work for some reason - its a Bill Gates bug!!

Voodooman!
0
 
LVL 1

Expert Comment

by:PatrickVD
ID: 2639528
Hi,

Populating comboboxes is a quite simple operation... you just use the AddItem method of the combobox.

Combo1.AddItem "This is the string that appears in the box"

This code just appends an item to the combobox... It doesn't matter if you want to add new items later, you can simply add them in your code ... There isn't a restriction on this.

If you want to know which items was selected in the combobox, you can retrieve the index of that item in the combo's list with the ListIndex property. Beware, if no items are selected in the combobox, it will have the value -1.

As you probably know, the combobox has the List property which contains all the string items being displayed. But, it also contains an ItemData list, which is actually a list in which you can store any 'Long' value. Conceptually, you could say that for each item in the combobox, you can store a 'String' and a 'Long' value.
I very ofter use this Itemdata to store the primary key of the records that contain the details of the displayed items... Then I can retrieve the information later...

I'll now try to make everything clear in view of what you would like to do...

'Add Items to combo
Private Sub AddComboItem(Combobx As Control, ItemString As String, ItemLong As Long)
  'Create the new Item
  Combobx.AddItem ItemString
  'Update this new Item's Long value
  Combobx.ItemData(Combobx.NewIndex) = ItemLong

End Sub

'Now, To display the selected Item details check listindex (assuming this will happen when the user clicks a command button, and that the concerned combobox is named Combo1)

Private Sub Command1_Click()
  Dim lngID as Long

  If Combo1.ListIndex = -1 Then
     MsgBox "Please Select an Item!!"
  Else
     'Retrieve the ID from Itemdata
     lngID = Combo1.ItemData(Combo1.ListIndex)
     'Do data access to retrieve data and display it.
  End If

End Sub


Hope this helps...
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

705 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

18 Experts available now in Live!

Get 1:1 Help Now