Access Tips Cascading Lists For Access Forms
Access Tips Cascading Lists For Access Forms
htm
This tutorial demonstrates several different ways to create cascading lists on your
Access forms. I will use combo boxes, rather than list boxes, as the latter have a
more specific purpose (to allow the user to make multiple selections).
If you are new to form building, or have not ever put a combo box on to a form,
have a look at a step-by-step tutorial first.
In this example there are two combo boxes: one displays a list of countries, the
other displays a list of cities. If they worked independently the user could choose a
country and a city but might get the combination wrong. Also, the list of cities in
would have to show all the cities available. The combo boxes can be linked in a
number of ways. The illustration below shows how linked combo boxes might work.
The user chooses a country first then opens the city list. They see a list of cities
relevant to the country they selected.
To see the demo, point at the numbered items below and watch the image change
(if necessary scroll your browser window so that you can see the whole image).
If your browser does not have JavaScript enabled my rollover effects for the image
above won't work. You can see the individual images by clicking the thumbnails
below:
The following examples show several different ways of achieving this effect and are
1 de 6 27/7/2012 17:19
Access Tips: Cascading Lists for Access Forms http://www.fontstuff.com/access/acctut10.htm
The following examples show several different ways of achieving this effect and are
presented in an increasing order of complexity.
The plan is to have the contents of the cboCity list change to reflect the user's
choice from cboCountry. This will be achieved by programmatically defining the
Row Source property of cboCity using the After Update event of cboCountry.
Here's the code that does the job:
How it works...
The code for this method is very simple. The AfterUpdate event fires when the user
makes a choice from the cboCountry combo box. The code uses a Case Statement
to assign one of the city tables to to the Row Source property of the cboCity combo
box according to the user's choice.
2 de 6 27/7/2012 17:19
Access Tips: Cascading Lists for Access Forms http://www.fontstuff.com/access/acctut10.htm
The Row Source property of the cboCountry combo box takes the form of an SQL
statement which represents a query of the tblAll table returning the unique values
found in the Country field, sorted into ascending order:
As in the previous example, no row source is specified for the dependent combo
box. The code will deal with that. The following procedure runs on the After Update
event of the cboCountry combo box:
How it works...
When the user makes a choice from the cboCountry combo box the AfterUpdate
event fires. The attached code defines a Row Source to the cboCity combo box in
the form of an SQL statement.
The SQL statement changes according to the user's choice as determined by the
inclusion of cboCountry.Value in the WHERE clause. If, for example, the user had
chosen France then the line containing the WHERE clause would read:
When a Row Source is assigned to the cboCity combo box, it remains assigned
until it is reassigned by a change in the cboCountry combo box. So, when
navigating through existing set of records, the list of cities may not be appropriate
for the city shown in the City field.
For example, you enter a record for an address in the United Kingdom so cboCity
displays a list of UK cities. You then move back through the records and stop on an
address in France. You might wish to edit that address so you open the cboCity
combo and instead of seeing a list of French cities you see a list of UK cities! This
happened because that was the last Row Source assigned to the combo box and
nothing has happened since to change it.
This example corrects that using the following code on the form's On Current event:
3 de 6 27/7/2012 17:19
Access Tips: Cascading Lists for Access Forms http://www.fontstuff.com/access/acctut10.htm
This example corrects that using the following code on the form's On Current event:
How it works...
The OnCurrent event fires when a form moves from one record to another (or when
the form is refreshed or requeried).
The first line uses the DLookup function (this works like Excel's VLOOKUP
function). This gets the value (i.e. the existing city name) from the City field, looks
for it in tblAll table, and returns the corresponding Country name which it places
in cboCountry combo box. (NOTE: If, as in my example there is a Country field in
the form's underlying table then this data should be in place anyway - but I'm just
making sure! In fact, you might want to omit or modify this line if you were
allowing users to enter city names that were not on the list.)
The next line is the same as the one that runs on the AfterUpdate event of the
cboCountry combo box but we need it here because the AfterUpdate event has not
been fired.
The illustration below shows how it works. To see the demo, point at the numbered
items below and watch the image change (if necessary scroll your browser window
so that you can see the whole image).
If your browser does not have JavaScript enabled my rollover effects for the image
above won't work. You can see the individual images by clicking the thumbnails
below:
4 de 6 27/7/2012 17:19
Access Tips: Cascading Lists for Access Forms http://www.fontstuff.com/access/acctut10.htm
NOTE: When you draw a group of Option Buttons you should draw the
Option Group frame first, then draw the Option Buttons inside. When
you do this all the buttons inside the frame become part of the same
group and function properly. Only one can be selected at a time, and
the selected button passes its value to the group.
The code used to assign a Row Source to the cboCity combo box runs on the After
Update event of the grpCountry Option Group (NOT the event of the individual
button). The event fires when the user selects one of the buttons:
How it works...
First of all, a Case Statement checks the value of the group and puts the
corresponding country name a string variable called strCountry. Then an SQL
statement, incorporating the variable and querying the tblAll table, is assigned as
the row source of the cboCity combo box.
It works in much the same way as previously but the code gets the name of the
country from an Option Group instead of a Combo Box.
5 de 6 27/7/2012 17:19
Access Tips: Cascading Lists for Access Forms http://www.fontstuff.com/access/acctut10.htm
grpCountry.Value = Null
End If
' Synchronise country combo with existing city
strCountry = DLookup("[Country]", "tblAll",
"[City]='" & cboCity.Value & "'")
Select Case strCountry
Case "France"
grpCountry.Value = 1
Case "United Kingdom"
grpCountry.Value = 2
Case "United States"
grpCountry.Value = 3
End Select
' Synchronise city combo with existing city
cboCity.RowSource = "Select tblAll.City " & _
"FROM tblAll " & _
"WHERE tblAll.Country = '" & strCountry & "' " & _
"ORDER BY tblAll.City;"
End Sub
How it works...
You might have noticed that in the first illustration none of the option buttons are
selected, all are greyed out. Form designers often like to set one of the buttons of
an option group as the "default" button so that it is already selected when the form
opens or when the user moves to a new record. It's easy to do, just find the option
group's Default Value property and set it to whatever value represents the button
you want selected. But I didn't want to do that. Since I was going to synchronise
the option group country with any existing city, I decided to give the group a Null
value if no city name was present. This is achieved by the If Statement at the start
of the code procedure.
Next comes the code to determine the correct country for the existing city (if
present). Like the previous example it uses the DLookup function, but unlike the
previous example it can't feed the country name straight into the option group. It
has to convert the name to a number (the value of the appropriate option button)
so it uses a Case Statement to do this. Finally, the same code as before
synchronises the combo box list with the existing city.
6 de 6 27/7/2012 17:19