Solved

Combo Boxes

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now