Synchronizing Combo Boxes on Forms in Access 2007
Office Visual How To

Applies to: 2007 Microsoft Office System, Microsoft Office Access 2007

Joel Krist, Akona Systems

April 2007

Overview

When you build custom Microsoft Office Access 2007 applications, there are often situations where it is useful to synchronize two combination boxes (combo boxes) on an Access 2007 form so that when you select an item in the first combo box, that selection limits the choices in the second combo box. For example, it might be necessary to limit the products that are displayed in a combo box based on the category that is selected in another combo box. The Access 2007 combo box control generates the AfterUpdate event when an item in the combo box is selected. This article illustrates how to use the combo box AfterUpdate event to synchronize two combo boxes on an Access 2007 form.

Code It

This section walks through two steps to illustrate how to synchronize two combo boxes on an Access 2007 form:

  1. How to add an event procedure for the combo box AfterUpdate event.

  2. How to add code to the AfterUpdate event procedure that updates the contents of a combo box based on the item selected in another combo box.

To illustrate the steps required to synchronize two combo boxes, the code provided in this article works with a sample database that has the following characteristics:

  1. An Access 2007 database exists which contains two tables, tblCategories and tblProducts. The Category field in tblProducts is a lookup column that is linked to the Category field in the tblCategories table.

    Figure 1. Categories table

    Synchronizing Combo Boxes categories table

    Figure 2. Products table

    Synchronizing Combo Boxes products table
  2. The database contains a form which contains two combo boxes: a Categories combo box named cboCategories and a Products combo box named cboProducts. The cboCategories combo box has its Row Source Type property set to Table/Query and its Row Source property set to the following SQL statement:

    SELECT [tblCategories].ID, [tblCategories].Category FROM tblCategories ORDER BY [Category];

Add an Event Procedure for the Combo Box AfterUpdate Event

To add an AfterUpdate event procedure for the Categories combo box

  1. Load the form that contains the two combo boxes in design view. In the Navigation Pane, right-click the form's name and select the Design View menu item.

  2. Display the properties for the Categories combo box. Right-click the Categories combo box on the form and select the Properties menu item.

  3. On the Categories combo box Property Sheet, select the Event tab to display the combo box events.

  4. Locate and click the After Update event and select the small down arrow that is displayed.

  5. Click the [Event Procedure] item and then select the ellipses button next to the small down arrow. This causes Visual Basic to load and display the body of the cboCategories_AfterUpdate event procedure.

Add Code to the AfterUpdate Event Procedure

Modify the cboCategories_AfterUpdate event procedure created previously so that it sets the RowSource property of the Products combo box based on the category that is selected in the Categories combo box. This causes the Products combo box to display only those products whose category matches the category that was selected in the Categories combo box.

Visual Basic
Private Sub cboCategories_AfterUpdate()
    ' Update the row source of the cboProducts combo box
    ' when the user makes a selection in the cboCategories
    ' combo box.
    Me.cboProducts.RowSource = "SELECT ProductName FROM" & _
                               " tblProducts WHERE Category = " & _
                               Me.cboCategories & _
                               " ORDER BY ProductName"
                            
    Me.cboProducts = Me.cboProducts.ItemData(0)
End Sub
Read It

When you build custom Access 2007 solutions, it is often useful to synchronize two combo boxes on an Access 2007 form. This ensures that when an item is selected in the first combo box, that selection limits the choices in the second combo box. This article explores how to synchronize two combo boxes on an Access 2007 form. The key steps include:

  1. How to add an event procedure for the combo box AfterUpdate event. This step causes Visual Basic to load and display the body of the AfterUpdate event procedure.

  2. After the body of the AfterUpdate event procedure is created, the next step is to add the code that executes as part of this event procedure. This code, as indicated above, uses a SELECT statement to scope the second combo box list based on the first combo box selection.

See It

Synchronizing Combo Boxes on Forms video

Watch the Video

Video Length: 00:04:46

File Size: 3.37 MB

File Type: WMV

Explore It
Tags :


Community Content

Toufikannab
Sync. two table cells?

Hello everyone,

Is there a similar way to sync two cells in an Access table.

The above method can't be applied to tables since there is nothing called EVENT in the cell's properties.

I highly appreciate your recommendations.

thank you,

Regards.

Tags :

axiom28
Synchronising Combo Boxes

Hello Everyone

I tried the above example exactly as stated here and it did work, however, I then
tried to apply to my real database and that was when the wheels fell off. After much
examining and experimenting I discovered the main difference between my database and
this one was that in my equivalent to the Products table the Category was a look up column
in the table whereby one looked up and inserted the category from a drop down list which
got it's data from the Category table. For some reason the sub list wouldn't work so I made the
assumption that instead of text I am getting a number which represent it's position in an array.
My two table are Country & Track so I want to first select a country and then the tracks in that
country.

Hope I am making sense so far I am a new Access programmer

So on my form I created an unbound textbox which read the array number of the first
combo box using this in the Control Source property

=DLookUp("[Country]","Country","[CountryID]=" & [Forms]![Form2]![Country])

On the form (Form2) I have a combo box as in the example above but with the following
satement in the Row Source property

SELECT [Country].[CountryID], [Country].[CountryName], [Country].[CountryCode] FROM Country ORDER BY [CountryName];

So when I select a country it's position in the array is reflected in the text box

Then I used the OnChange rather than the AfterUpdate event procedure and placed the follwing
VB code in that:

Private Sub Country_Change()
Me.cboTrack.RowSource = "SELECT TrackName FROM Track WHERE Country = " & Me.txtCountry.Value & " ORDER BY TrackName"
Me.cboTrack = Me.cboTrack.ItemData(0)
End Sub

This works well and reliably, but as I say I am new to Access 2007 and maybe there is an easier way,
If anyone knows of one I'd be happy to see it.

Also make sure that when the security warning appears below the ribbon strip that you click on
it and select "Allow Content". If you don't the VBA code won't work, but your SQL will which is very
confusing when you're new to this and your program seems to only half work

Tags :

rmcruz
Sync combo in subforms
Hi there,

I have a problem with syncronizing the combo boxes in a subform. I have it running on the form, bu I would like to use these combos also in a subform. However I cannot make it work. When I choose the value on the first combo and the after_update event runs, I am asked for the same value. So, for any reason, it is no assuming the value of the combo.

Any help?

Regards
Tags :

MarathonCat
first attempt at Synchronizing 2 combo boxes is not working

I am trying to synchronize 2 combo boxes so that once I select the client in the first combo box, it will narrow down my options for the SUBClient in the second combo box


Any help would be greatly appreciated!

The is the row source for ComboClient

SELECT [Q SortClient].[ClientID], [Q SortClient].[Client] FROM [Q SortClient];


This is the row source for the ComboSUBClient

SELECT [Q SortSUBClient].[SUBClientID], [Q SortSUBClient].[SUBClient], [Q SortSUBClient].[Client] FROM [Q SortSUBClient];


This is the code that is not working for me

Private Sub ComboClient_AfterUpdate()

Me.ComboSUBClient.RowSource = "select SUBClientID FROM" & _

" Q SortSUBClient WHERE ClientID = " & _

Me.ComboClient

Me.ComboSUBClient = Me.ComboSUBClient.ItemData(0)

End Sub


Tags :

Page view tracker