Solved

Combo Boxes

Posted on 2000-03-21
2
196 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

726 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