How to setup cascading comboboxes in datasheet subform

In this post we will discuss how to setup cascading comboboxes in your subform.

This post was in response to this issue that someone was having:

So the real key to solving this issue is the ways the tables are setup.

Here are the tables:

-tblCashVouchers
-tblCashVouchersDetails
-tblAccountNames
-tblAccountTypes

tblCashVouchers

tblCashVouchersDetails

tblAccountNames

tblAccountTypes

The 1st 2 tables, tblCashVouchers and tblCashVouchersDetails are setup in the typical parent/child relationship.

tblCashVouchersDetails is the detail table, and will hold the id numbers for all the main tables.

This is the table that is used as the recordsource for the subform.

The comboboxes in the subform are based off of 2 tables: tblAccountNames (the main one) and tblAccountTypes (the sub one)

tblAccountNames is related to tblAccountTypes by the “ATAccountTypeRef” field.

After I select an “Account Name”, I requery the form and can select an “Account Type” corresponding to the “Account Name”.

In my opinion, setting the recordsource is better set in VBA when the “AfterUpdate” event is run.

Here is the code, and I am using the AfterUpdate and Current events to set the “CVDAccountTypeID” combobox recordsource .

Private Sub CVDAccountNameID_AfterUpdate()
    SetRecordsource

End Sub

Private Sub Form_Current()
    SetRecordsource
End Sub
Sub SetRecordsource()
    Dim strSQL As String

    strSQL = "SELECT ATID, ATAccoutName, ATAccountTypeRef FROM tblAccountTypes WHERE ATAccountTypeRef= " & Me.CVDAccountNameID

    Me.CVDAccountTypeID.RowSource = strSQL
End Sub

Watch a video of how it’s all put together:

Here is the “How to setup cascading comboboxes in subform” database

Let me know if you have any questions.

*******************
PS. Do your cell values keep deleting? Well make sure the field you are querying doesn’t have the column headers set to 0,1. In other words, don’t hide to first column and show the second, show both, or else your values will look like they are disappearing, but will be in the table.
*******************

Click here for more information on comboboxes and filter related posts.






By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Escape Apostrophe In SQL Update Query

If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]

How To Make An Access Form Time Picker

Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How to pick a file to load In VBA

Picking a file to load in your Microsoft App is a very important skill to know. In this blog post you will see how to do it. First you need to set a reference to the MS office object library From VBE editor –> select Tools > MS office object library (click check mark) Sub […]

Previous Post

How To Do A VLookup In Access

Next Post

How do I create a query between two dates in Access?