473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to filter second combo box by selection in first combo box

In my asset and employee database I have Departments and Subdepartments.

In the forms I want to filter the Subdepartment combo boxes based on the selection in the Department combo box.

Combo boxes:
cboDept
cbosubDept

Source tables and fields:
listDept
- ID (primary key, not really used currently)
- Dept (Department acronym)
- Name (Full department name, not really used)
listSubDept
- ID (primary key, not really used currently)
- Dept (Department acronym from listDept)
- SubDept (Full name of Subdepartment)

Source for cbosubDept:
Expand|Select|Wrap|Line Numbers
  1. SELECT listSubDept.SubDept
  2. FROM listSubDept
  3. ORDER BY listSubDept.SubDept;
Source for updating cbosubDept:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboDept_AfterUpdate()
  2.     ' Filter records in cboSubDept based on selection in cboDept
  3.     Dim subDeptSource As String
  4.  
  5.     subDeptSource = "SELECT[listSubDept].[SubDept],[listSubDept].[Dept] " & _
  6.         "FROM listSubDept " & _
  7.         "WHERE [Dept] = " & Me.cboDept.Value
  8.     Me.cbosubDept.RowSource = subDeptSource
  9.     Me.cbosubDept.Requery
  10.  
  11. End Sub
I've adapted this code from here:
http://www.databasedev.co.uk/filter_combo_boxes.html

Explanation:
I'm using the full Dept for reference because currently I haven't become comfortable or found a consistant method of viewing one (user-friendly) value in a form while storing a different (numeric) value in the table.

Problems:
1) When I select a Dept for which there are no SubDepts, cboSubDept shows all SubDepts. I know I don't actually have any code regarding how to deal with a Dept for which there are no SubDepts, and I would appreciate pointers along those lines.

2) When I select a Dept for which there are SubDepts, I get a pop up asking for Parameter Values. Do I need to switch to the listDept.ID in listSubDept and in the Form in order to make this work? Or is something else causing this issue?
May 27 '10 #1
3 1813
I would also like to add some If/Then logic to the cboDept to change the behavior of cbosubDept based on whether or not there are subDepts for the selection in cboDept.

Something along the lines of:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cboDept_AfterUpdate() 
  2.     ' Filter records in cboSubDept based on selection in cboDept 
  3.     Dim subDeptSource As String 
  4.  
  5.     If Me.cboDept.Value (does not equal null)
  6.     subDeptSource = "SELECT[listSubDept].[SubDept],[listSubDept].[Dept] " & _ 
  7.         "FROM listSubDept " & _ 
  8.         "WHERE [Dept] = " & Me.cboDept.Value 
  9.     Me.cbosubDept.RowSource = subDeptSource 
  10.     Me.cbosubDept.Requery 
  11.  
  12.      Else
  13.        (grey out, disable, hide, or display string "N/A" in cbosubDept)
  14.     EndIf
  15.  
  16. End Sub 
  17.  
  18.  
I suppose I could set the initial state of cboSubDept.Visible to No.
But if I were opening the form, would it default to the visible state based on the intially selected record or the visible state defined in the control properties?

Or do I need to add code to the form determining the initial visible state based on the initial entry selected?
May 27 '10 #2
colintis
255 100+
To your where clause, try putting double quotes between the value

Expand|Select|Wrap|Line Numbers
  1. "WHERE [Dept] = "  & Chr(34) & Me.cboDept.Value & Chr(34)
Note: Chr(34) is the "

For the if statement, there's a function called ListCount, try put the if statement as this:

Expand|Select|Wrap|Line Numbers
  1. If Me.cboDept.ListCount = 0 Then
  2.       (code for the stuffs)
  3. Else
  4.       Me.cboDept.Enable = False  'This should disable the combo box
  5.       Me.cboDept.Visible = False 'This will hide the combo box
  6. End If
  7.  
Also if you want to check the amount of items in the combo box, you should put the rowsource codes outside the if statement, so that it can do the counting after the 2nd combo box is updated.
May 28 '10 #3
NeoPa
32,556 Expert Mod 16PB
Check out Example Filtering on a Form and tell us if there are any issues outstanding. This should cover the fundamentals, and progress from there should be simpler and clearer.

Welcome to Bytes!
May 28 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: rikesh | last post by:
Hi I'm sure this is a very trivial problem! I have a combo bound to a recordset. I was wondering how I can show the value on the page, what the user has selected? The code that I'm using is...
3
by: deko | last post by:
SELECT TxType_ID, TxTypeName FROM tblTxType UNION SELECT Top 1 TxType_ID, "<Payment Account>" FROM tblTxType ORDER BY ; The problem I'm having is that the Top 1 "TxType_ID" brings up a...
1
by: Alex | last post by:
Acc97.. I have a database which records down-time on 8 machines, the selection of reasons comes from a combo box. I have tried to be clever! and limit the options relative to each machine within...
1
by: MLH | last post by:
I have a form (xxxxxxxxxxxxxx) with a combo-box control (yyyyyyyyyyyyyy). The rowsource property for that combo box is as follows: SELECT DISTINCTROW ., . FROM ; The SQL for qryVehicleList...
5
by: ddhung | last post by:
how to insert multible values by making one selection from combo box in forms. any way to code insert SQL in froms??
6
by: Dave | last post by:
I want to put the information that the user selects in my combo boxes into a subform that lies on the same form as the combo boxes. Thanks for your help already, Dave
4
by: whamo | last post by:
I have the need to populate a field based on the selection in a combo box. Starting out simple. (2) tables tbl_OSE_Info and tbl_Input; tbl_OSE_Info has three fields: Key, OSE_Name and OSE_Wt...
2
by: ARC | last post by:
I'm testing a user's db that contains a very large number of records. I have an invoice screen, with an invoice select dropdown box that shows all invoices, and the customer's name, etc. With...
4
by: novoselent | last post by:
This seems like it should be an easy thing, but I think I'm missing something simple here...or I'm just going about it all wrong... Using Access 2003 I have a form that lists vehicle service...
7
by: Orv | last post by:
I have a "Yes/No" combo box (set to required) and I want the focus to shift to differnet controls (on the same form) based on the selection of "Yes" or "No". Would this have to be placed on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.