The ComboBox control is an VB/VBA control that can also be used in Access.
The ComboBox displays a list of values to choose from.
The ListBox has the same properties as the ComboBox except the values are always displayed. It doesn't have a drop down pane.
By having a user select from the ComboBox list, you can control the values selected.
Adding the ComboBox control to the form will differ based on the version of Access being used.
Create a form and select the control tools. You might have to place the form in Design view.
Select the ComboBox control and drag it to the form.
Right-click on the ComboBox control and select Properties.
In the Properties box, give the ComboBox a name. Standard practice is to prefix the name with " cbo"
However, using VBA to set the properties of your ComboBox allows you to get the database keys, and the location of the selected item in the database. There are many reasons why you would want to do this.
City ID | City Name |
20 | Chicago |
21 | Houston |
22 | Dallas |
23 | New York |
List | ListIndex | ItemData |
Chicago | 0 | 20 |
Houston | 1 | 21 |
Dallas | 2 | 22 |
New York | 3 | 23 |
ItemData | The set of numbers that correspond to the items in the ComboBox. |
List | Text strings for the items in the ComboBox. |
ListCount | The number if items in the ComboBox. |
ListIndex | The index of the item currently selected. |
NewIndex | The index of the last item that was added to the ComboBox. |
Sorted | States if the ComboBox is sorted. |
Style | If the ComboBox has a drop-down list or a
permantly visible list. Also determines if the user can add a new value, or can only select from the preset values. |
Text | The Text value of the item currently selected |
List is the set of all values in the ComboBox. AddItem adds text items to the ComboBox. |
cboBox1.AddItem "Chicago" cboBox1.AddItem "Houston" cboBox1.AddItem "Dallas" cboBox1.AddItem "New York" |
ListCount is the number of items in the ComboBox. In this example, the ListCount is 4 |
x = cboBox1.Count x = 4 |
ListIndex is the index of the selected item. If no item is selected, the ListIndex is -1 cboBox1.ListIndex(2) is Dallas. |
"Chicago (0)" "Houston (1)" "Dallas (2)" "New York (3)" |
Text is the value of the selected item. If the user selects the third item (ListIndex = 2), the text will be "Dallas" |
str = cboBox1[2].text str = "Dallas" |
ItemData is a hidden value for each value in the ComboBox. ItemData can be used to store a key value while a more descriptive text value is displayed in the ComboBox. You cannot rely on the ListIndex to determine which text value was selected, especially if the list has been sorted. In the example on the right, each city has a code in the database. These are used as the ItemData values. NewIndex is the index of the item currently being added to the ComboBox. In this example, the NewIndex value is replaced with the city code and set as the ItemData value. |
cboBox.AddItem "Chicago" cboBox.ItemData(cboBox.NewIndex) = 20 cboBox.AddItem "Houston" cboBox.ItemData(cboBox.NewIndex) = 21 cboBox.AddItem "Dallas" cboBox.ItemData(cboBox.NewIndex) = 22 cboBox.AddItem "New York" cboBox.ItemData(cboBox.NewIndex) = 23 |
There are three types of ComboBox styles to choose from:
0-DropDown Combo | The user can type a new value into the control |
1-Simple Combo | Same as a DropDown Combo except the list is always visible |
2-DropDown List | Only items in the list can be selected. |
The first statement returns the ItemData value for the item at ListIndex (in this case, 1), which is Houston.
The ItemData for Houston was entered as 21, which is the key to Houston in the database.
ListIndex is the ComboBox index of the item selected.
If Houston is selected, and Houston's ListIndex is "1", then the ItemData for ListIndex "1" is "21".