Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

ACC2000: How to Synchronize Two Combo Boxes on a Form


View products that this article applies to.

Summary

This article shows you how to synchronize two combo boxes so that when you make a selection in the first combo box, the selection limits the choices in the second combo box.

NOTE: This article explains a technique demonstrated in the sample file, FrmSmp00.mdb. For information about how to obtain this sample file, please see the following article in the Microsoft Knowledge Base:
233324 Microsoft Access 2000 Sample Forms Database Available in Download Center

↑ Back to the top


More information

The following example uses the sample database Northwind.mdb. 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:

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
  1. Open the sample database Northwind.mdb.
  2. Create a new form not based on any table or query with the following combo boxes, and save the form as Categories And Products.
       Combo Box 1
       -------------------------------
       Name:          Categories
       RowSourceType: Table/Query
       RowSource:     Categories
       ColumnCount:   2
       ColumnWidths:  0";1"
       BoundColumn:   1
       AfterUpdate:   [Event Procedure]
    
       Combo Box 2
       --------------------------
       Name:          Products
       RowSourceType: Table/Query
       ColumnWidths:  2"
       Width:         2"
    						
    NOTE: If you are in an Access project, the RowSourceType will be Table/View/StoredProc.
  3. Add the following code to the AfterUpdate event procedure of the Categories combo box:
    Me.Products.RowSource = "SELECT ProductName FROM" & _
       " Products WHERE CategoryID = " & Me.Categories & _
       " ORDER BY ProductName"
    Me.Products = Me.Products.ItemData(0)
    					
  4. 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.

Notes

In the above example, the second combo box is filled with the results of an SQL statement. This SQL statement finds all the products that have a CategoryID that matches the category selected in the first combo box.

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.

↑ Back to the top


Keywords: KB209595, kbhowto

↑ Back to the top

Article Info
Article ID : 209595
Revision : 3
Created on : 6/23/2005
Published on : 6/23/2005
Exists online : False
Views : 400