Vous êtes sur la page 1sur 6

How can I filter one Microsoft Access combobox based on another combobox

selection?

The following article describes how to synchronize two Microsoft Access combo boxes so that
when you select an item in the first combo box, the selection limits the choices in the second
combo box.

Assume you have the following two Microsoft Access tables:

tblStore

• lngStoreID (Primary Key)


• strStoreName

tblStore
lngStoreID strStoreName
1 M&S
2 Binns
3 Safeway
4 B&Q

tblManager

• lngManagerID (Primary Key)


• lngStoreID (Foreign Key - tblStore)
• strManagerName

tblManager
lngManagerID lngStoreID strManagerName
1 1 John Smith
2 1 Lee Thomas
3 1 Alison Jones
4 2 Tim O'Brian
5 2 Simon Marsh
6 3 Harry Hill
7 3 Sally Lees
8 4 Jenny Parker
9 4 Ian Jennings
10 4 Fred Lee
11 4 Bill Hardy
12 4 Alan Parker

You also have a Microsoft Access form with two comboboxes:


Our combo boxes have the following Record Sources that provide their data:

cboStore
RecordSource: SELECT [tblStore].[lngStoreID], [tblStore].[strStoreName] FROM tblStore;

cboManager
RecordSource: SELECT [tblManager].[lngManagerID], [tblManager].[lngStoreID],
[tblManager].[strManagerName] FROM tblManager;

You only want those managers visible that are in the store that has been selected from cboStore.

To do this you will need to modify the cboManager RecordSource in the AfterUpdate event of
cboStore:

Private Sub cboStore_AfterUpdate()


Dim sManagerSource As String

sManagerSource = "SELECT [tblManager].[lngManagerID]," & _


" [tblManager].[lngStoreID]," & _
" [tblManager].[strManagerName] " & _
"FROM tblManager " & _
"WHERE [lngStoreID] = " & Me.cboStore.Value
Me.cboManager.RowSource = sManagerSource
Me.cboManager.Requery
End Sub

You can apply this concept of having the value of one control affect the value of another by
keeping in mind the AfterUpdate event of the first control is where you want to take the action
on the second control.

Below, we see the form, with the second combo box un-filtered. You can see that we have not
yet selected a value in the first (Store Name) combo box:
After selecting a value in the first (Store Name) combo box, we will see that the second (Store
Managers) combo box is now filtered, and contains only values related to the selection in the
first combo box. The two combo boxes are now synchronized.

To see an example of this in action please download the sample database from the Microsoft
Access Forms page or Microsoft Access Downloads page.

Filtering the results of a Microsoft Access query using a Drop-Down combo box:

You should be aware that you can limit the results of a database select query by using particular
criteria. The article relating to Microsoft Access Database Query Basics gives a breakdown on
applying simple query criteria.

What if you are not too sure about what criteria applies to the particular field - wouldn't it be
better to be able to choose this from a list rather than having the standard Microsoft Access input
box appear...

You can create a simple Access form, containing a combo box, and base the query criteria on the
value chosen in the form as detailed below:

To begin with we will create a table in Microsoft Access containing some values to search on -
the table is shown below:
The table that will be used to create the query on.

The next stage is to create the Access query that we will later apply criteria to. The query design,
shown below, prior to any criteria being applied:

The query design, prior to any criteria being applied.

Once we have created the query, we then need to create the Microsoft Access database form that
we are going to use to apply the criteria to the query. This form will contain the drop-down
combo box with a list of values to choose from that will run the query. The form design is shown
below:

The form, showing the drop down list of values to choose from.
The row source that this combo box uses is derived from the table and uses the following SQL:

SELECT tblProducts.PartNumber, tblProducts.Description FROM tblProducts;

Once a value is chosen from the drop down list the On_Change event procedure runs using the
following code:

Private Sub cboSearch_Change()


DoCmd.OpenQuery "qryProducts_And_Locations"
Me.cboSearch = ""
End Sub

The above runs the query and re-sets the search drop down list to a blank selection. What we
now need to do is apply the criteria to the query design that will ensure that the filter is applied
based upon the value chosen in the form and this criteria is shown below:

The query design with the form criteria applied.

So now, when we choose a value from the drop down list on the form, the query will run and
return only the item chosen from the list:
The results of the query once the form criteria has been applied.

You can download an example Microsoft Access 2000 Database that shows this example from
the Microsoft Access Queries menu or from the Microsoft Access Downloads menu.

Vous aimerez peut-être aussi