Solved

Combo Boxes

Posted on 2000-03-21
2
195 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

815 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

10 Experts available now in Live!

Get 1:1 Help Now