Sample database: CascadingComboBoxes.zip (62 KB)
Combo boxes and list boxes are a great way to control user experience by limiting the values that a user can choose to a known list.
In Microsoft Access, a common need is to have multiple combo boxes or list boxes on a form, and to have the selection in one combo box limit the choices in a second combo box. For example, an Address form containing Country and State lookups. When you select a country, its list of states appear.
Our this sample database, we have a list of species, both plants and animals, categorized by their taxonomic rank (kingdom, phylum, class, order, family, genus, then species).
If you select the value "Animal" from the Kingdom combo box, the Phylum combo box is updated to show Animal phylums only. The Species list box is also filtered by your selection.
This is known as cascading combo boxes or synchronized combo boxes.
To create the cascading combo box, we add code in the "AfterUpdate" event for the Kingdom combo box, to update the RowSource property of the Phylum combo box based on the selected Kingdom.
' Set the Phylum combo box to be limited by the selected Kingdom Me.cboPhylumID.RowSource = "SELECT PhylumID, PhylumName " & _ "FROM tblPhylum " & _ "WHERE KingdomID = " & Nz(Me.cboKingdomID) & _ "ORDER BY PhylumName"
Additionally, we add code to update the contents of the Species list box:
' Limit the Species list box Me.lstSpeciesID.RowSource = "SELECT SpeciesName, Description " & _ "FROM qryTaxonomy " & _ "WHERE KingdomID = " & Me.cboKingdomID ' Refresh the list box Me.lstSpeciesID.Requery
After updating the RowSource property, use the list box's Requery method to re-load the data in the ListBox.
There is sometimes confusion between the terms RecordSource and RowSource. Both are properties that can contain a table, query, or SQL string.
Microsoft Resource: How to: Synchronize Two Combo Boxes on a Form