Sei sulla pagina 1di 4

How to Synchronize Combo Boxes

VBA Code How to Synchronize two combo boxes on a form In Access with Code
This sample shows you how to select an item in one drop down list on a form and 
limit the choices in a second drop down list so the second list shows only items 
related to the item in the first drop down.

For instance, you might have a list of cities and a list of customers. You would like to pick a 
city from your first drop down list. In the second drop down list, you want to see only
customers in that city. You might also have a list of auto makers and want to see only the
models that belong to that maker in your second drop down list. 

This example shows you how to select a product category in the first drop down and in the 
second dropdown see only the products that belong to that category. In this example we
use Visual Basic code that controls what appears in the second combo box after you
make your selection in the first combo box.

Review the left side of the form Synchronized Combo Boxes to see how your form will 
operate.

You can accomplish synchronizing the drop down lists, or combo boxes, following 
the steps below.

1. Create a new form by clicking the Create tab and selecting Form Design.

2. In the Controls group on the Design tab click the combo box icon (If you move 
your cursor over each of the icons in the Controls group, you'll see a tooltip that
names the control.) Move your cursor to the design section of the form and hold
your left mouse button down while you draw the combo box rectangle on the form.

3. Cancel the combo box wizard if it launches when you let go of the mouse button.

4. Repeat steps 2 and 3 to add another combo box to the form.

5. Select the first combo box, right click and select Properties.

6. In the Properties window make the following adjustments on the All tab:

    Name:   cboCategories
    RowSourceType: Table/Query
    RowSource:     Categories
    ColumnCount:   2
    ColumnWidths:  0";2"
    BoundColumn:   1
    AfterUpdate:   [Event Procedure]
   
NOTE: To create the AfterUpdate Event:
      A. Select the Event tab on the Property Sheet
      B. Select After Update
      C. Click the button at the end of the row
      D. Click Code Builder. The Visual Basic editor opens. 

Page 1 of 4
7. Copy the following code under the line, Private Sub cboCategories_AfterUpdate, 
    in the Visual Basic code window:

     Me.cboProducts.RowSource = "SELECT ProductName FROM" & _
                            " Products WHERE CategoryID = " & Me.cboCategories & _
                            " ORDER BY ProductName"
     Me.cboProducts = Me.cboProducts.ItemData(0)

Note: There are two tables involved in this design: the Categories table, where
the first combo box gets its data (row source); and the Products table, where
the second combo box gets its data. We build the second combo box in the
next step.

The code sets the row source of the second combo box, cboProducts,
to show only records in the Products table where the CategoryID matches the 
CategoryID of the Category chosen in the first combo box. 

If you open the Relationships window from the Database Tools tab, you can 
see the two tables are connected, or related by the Category ID. The Products 
table, which can contain many records that belong to one category contains 
a CategoryID field to keep track of what category the product belongs to.

8. Select the second combo box. Right click and select Properties.

9. In the Properties window on the All tab, make the following adjustments
     for the second combo box:

     Name:   cboProducts
     RowSourceType: Table/Query
     ColumnWidths:  3"
     Width:         3"

NOTE : If you are in an Access Project, the RowSourceType will be Table/View/StoredProc. 

10. Save the form as Categories And Products.       
      
11.  View the Categories And Products form in Form view. Note that when you select a 
      category in the first combo box, the second combo box is updated to list 
      only the available products for the selected category.  
  

Whenever a category is selected in the first combo box, the AfterUpdate 
property runs the Event Procedure, which sets the second combo box's 
RowSource property. This refreshes the list of available products in 
the second combo box. Without this procedure, the contents of the second 
combo box would not change.

Page 2 of 4
Macro How to Synchronize two combo boxes on a form In Access with a Macro
This sample illustrates how to synchronize two combo boxes so that when you select an item 
in the first combo box, the selection limits the choices in the second combo box. This sample 
uses a macro instead of VBA code. 

The first combo box lists the available product categories, and the second combo box lists the 
available products for the category selected in the first combo box: 

Open the form Synchronized Combo Boxes to see how your form will operate.
  
To reproduce the Synchronized Combo Boxes With Macro form:

1. Create a new query. In SQL view paste the following:

SELECT Products.ProductName
FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID
WHERE (((Categories.CategoryID)=[Forms]![Categories and Products Macro 
Form].[cboCategories]));

Note that the query references a value in a control on a form that you are about to create.

2. Save the query as qryCategoryProducts2. Look at the query in design view. Close the query.

3. Create a new form that is not based on any table or query.

4. Add the following combo boxes:
    
     Combo Box 1
    ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
    Name:          cboCategories
    RowSourceType: Table/Query
    RowSource:     Categories
    ColumnCount:   2
    ColumnWidths:  0";2"
    BoundColumn:   1
   

   Combo Box 2
   ‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
   Name:          cboProducts
   RowSourceType: Table/Query
   RowSource: qryCategoryProducts2
   ColumnWidths:  3"
   Width:         3"
       
NOTE : If you are in an Access project, the RowSourceType will be Table/View/StoredProc and 
you would need to create a view similar to the qryCategoryProducts2. 

5. Select cboCategories in design view and click the Property Sheet button on the Design tab.

Page 3 of 4
6. On the Event tab in the Property Sheet click in the After Update row. Click the build button 
on the right and select Macro Builder.

7. In the Macro design window click Show All Macros.

8. In the Action column of the first row select Requery. At the bottom of the screen enter 
cboProducts as the Control Name.

9. In the second row of the macro select SetValue as the Action. Enter the following in the 
bottom section:

Item: cboProducts
Expression: nz(DMin("ProductName","qryCategoryProducts2"),"")

NOTE: You can omit the SetValue action, which requires the database to be trusted, but if you 
do, the Products combo box appears blank until you make a selection. The SetValue action 
results in the first ProductName (in alphabetical order) appearing in the combo box. See Office 
OnLine help for more information about DMin() and Nz()

10. Save the form as Categories And Products Macro Form.

11. View the form, Categories and Products Macro Form, in Form view. Note that when you 
select a category in the first combo box, the second combo box is updated to list only the 
available products for the selected category.  

In this example, the second combo box is filled with the values in qryCategoryProducts2. 
The criteria for the query’s CategoryID field is set to the value of the CategoryID for the item 
selected in cboCategories. 

Whenever a category is selected in the first combo box, the AfterUpdate event embedded 
macro requeries the row source of the cboProducts combo box and sets the value you see in 
the combo box to the first record in the query qryCategoryProducts2 that matches the Product 
in the cboProducts combo box.

Page 4 of 4

Potrebbero piacerti anche