Microsoft Access forms and table datasheets support Combo Boxes to let your users easily enter a selection by providing them with a list of valid values. This makes sure your data is correct and avoids typos:
Sample combo box listing countries from Total Zip Code Database
Most commonly, the list of values is from a table or query, and can be bound to one field (like an ID field) while displaying a field the user sees and understands. Combo boxes in Access also support the display of multiple columns so you can show more data to make it easier to select the right item.
Here are some tips for using Combo Boxes effectively. FYI, Total Access Analyzer flags any combo boxes in your tables and forms that don't have these properties set correctly:
Set the LimitToList property to Yes, otherwise users can enter values that are not in your list.
Set the AutoExpand property to Yes to automatically select a matching value in the list as you type.
It's annoying to see a short list of options and be forced to scroll to see the remaining few items, or to have a long list and be forced to scroll more than necessary because the number of rows displayed is small -- even though there's plenty of room on the screen!
Increase the ListRows property to display more choices and minimize the need to scroll. The default is number of rows is 8 in Access 2003 or earlier. It is increased to 16 in Access 2007, but that's still low. We would recommend 25 or 30+ if space allows. If the list doesn't have that many rows, the higher number has no impact.
With an unbound Combo Box, you may want to display the first item by default rather than showing a blank value. To do this, use the combo box's ItemData object. ItemData is an array of the combo box list.
For instance, a combo box named cboCountry can be set to the first value in the list by adding this code to the form's OnLoad event:
Me.cboCountry = Me.cboCountry.ItemData(0)
In forms, the BeforeUpdate event is used to validate the entries in the current record. For Combo Boxes, people often check if the value exists by looking for Null like this (where cboName is the name of the combo box):
If IsNull(Me.cboName) Then MsgBox "A value is required for ..." Me.cboName.SetFocus Cancel = True End If
This flags a missing value, prompts the user, and sets focus to the control, works in most cases, but not all.
If the combo box is on a bound field, there may be an existing value which is not valid. With the combo box set to LimitToList, the field has a value, but you can't see it. Maybe the value was in the list when it was originally selected, but the current list of allowed values has changed.
If you simply check for IsNull, Access says it is not null (which is correct) and you don't handle the invalid entry. What you really want to know is if a valid value was selected. A better check is with the ListIndex property:
If Me.cboName.ListIndex = -1 Then MsgBox "A value is required for ...." Me.cboName.SetFocus Cancel = True End If
There is no need to check for Null. The ListIndex property returns the number of the selected item (ranges from 0 to the number of items in the list minus 1). If no value or an invalid item is selected, it returns -1 and you can trap it.