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
- SELECT listSubDept.SubDept
- FROM listSubDept
- ORDER BY listSubDept.SubDept;
Expand|Select|Wrap|Line Numbers
- Private Sub cboDept_AfterUpdate()
- ' Filter records in cboSubDept based on selection in cboDept
- Dim subDeptSource As String
- subDeptSource = "SELECT[listSubDept].[SubDept],[listSubDept].[Dept] " & _
- "FROM listSubDept " & _
- "WHERE [Dept] = " & Me.cboDept.Value
- Me.cbosubDept.RowSource = subDeptSource
- Me.cbosubDept.Requery
- End Sub
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?