Jul 12, 2016
In Microsoft Access, we are going to create two Combo Boxes whose values in one of them depend on the value selected in the other one. For example selecting a country from a Country Combo Box will automatically populate the second Combo Box City with the name of cities in that country only, so that the user can easily select appropriate city.
The important requirement for this to work is that there has to be two columns in one table that hold the values of the two Combo Boxes. Therefore in the case above we need to have a table that has at least two columns, Country and City. You create a Combo Box to get data from one of the columns and the other Combo Box gets its data from the other column. So let’s go and see how it’s done.
Here’s our table:
In a form we add a combo box and follow the wizard to select values from a table.
Select the table, Contacts in our example.
Drop Country field to the right.
Apply sorting if you want, and click Next and then the important step is to un-hide the key column.
In the next screen select Country, click Next and type “Country” for Title and click on Finish.
Follow the same steps for the City combo box and then save the form as say, frmContacts.
Now select the Country combo box and activate its Properties pane. In the Other tab set the Name property to cboCountry and then in the Data tab select the Row Source and click on its “…” button. This takes you to query design view. In this view remove the ID from the grid if you have one, so it only has a single field Country.
Then click on the grey area of the query shown by red arrow to activate the General Query properties and set its Unique Values to Yes.
Save and close the query. Back in the Properties of the cboCountry in the Data tab set the Bound Column to 1.
Follow the same procedure with the City combo box and name it cboCity, however in the cboCity query design you need to add the Country field and set its criteria to Forms!frmContacts!cboCountry and make sure the Show check box is unticked shown by red arrow below.
What you are saying is that the entries in the City combo box should be filtered so that their Country field matches the one selected in the cboCountry combo box.
Set other setting as described for cboCountry above.
Test the form. You’ll notice that it is working only in the first try. Subsequent selection of cboCounty will not fetch appropriate cities. The reason is that Access does not run the cboCity query every time you change your selection in the cboCountry. You need to force it to re-query. To do this you need to write a command to be executed when something is changed in cboCountry. So go to the Properties of cboCountry, in the Event tab, for the On Change event drop down select [Event Procedure] and then click on the “…” button. This takes you to VBA programming screen. Then add this line of code cboCity.requery where shown in the picture below:
Close the VBA screen and test your form. It should now be working. Don’t forget to save your form.
Hope this has helped.
How do your Excel skills stack up?
Test NowNext up:
- PowerShell Basics Series – Measuring objects (Part 2)
- How to omit dates in an Excel chart axis
- 5 posts to help you become a better communicator
- SharePoint 2016 social features
- How-To series: Password protect sections in OneNote 2013
- Managing for excellence: An innovative approach to managing performance
- How-to: Give access to a list only in SharePoint 2013
- PowerShell Basics Series – Manipulating strings
- Formula vs. measure in PowerPivot
- PhotoShop in action - Part 1
Previously
- Working with Styles - Part 2
- What is Continual Service Improvement (CSI)?
- Honesty at work
- Search for a Worksheet Function in Excel
- Providing feedback for user interactions
- Working with Styles - Part 1
- Millennial Musings
- View Types in SharePoint
- How to Establish a Creative Workforce
- Managing your Azure subscription using PowerShell