In a comment, lamcro observes in regard to the question of whether or not a comb box box has a Recordset:
When I break into the forms VB code
and "Add Watch" the CB control, the
recordset property is in there. I can
even enter and see it's own
properties.
I see it when I set a watch list, but the recordset of a combo box is not accessible or alterable via code. In order to filter a combo box, you need to work with its Rowsource.
This can be accomplished one of two ways:
- use an event to assign a new Rowsource to your combo boxes on the fly, OR
- use a reference to the control whose value you want to filter on in the WHERE clause of the Rowsource of your other combo boxes.
Say you have cmbComboBox1 and when you select a value in it, you want the values listed in cmbCombBox2 to be filtered according to the value selected in cmbComboBox1. For method 1, you'd use the first combo box's AfterUpdate to set the rowsource of the second:
Private Sub cmbComboBox1_AfterUpdate()
Dim strRowsource As String
strRowsource = "SELECT * FROM MyTable"
If Not IsNull(Me!cmbComboBox1) Then
strRowsource = strRowsource & " WHERE MyField = " & Me!cmbComboBox1
End If
Me!cmbComboBox2.Rowsource = strRowsource
End Sub
To use the second method, you'd instead define the Rowsource of the second combo box to be based on testing the value of the first:
SELECT * FROM MyTable
WHERE (MyField=[Forms]![MyForm]![cmbComboBox1]
AND IsNull([Forms]![MyForm]![cmbComboBox1])=False)
OR IsNull([Forms]![MyForm]![cmbComboBox1])=True
What this does is filter the rowsource if the first combo box has a value and doesn't filter it if there is a value. That is, you get an unfiltered list until there's a value chosen for the first combo box.
Then, in the Afterupdate event of cmbComboBox1, you'd requery the second combo box:
Private Sub cmbComboBox1_AfterUpdate()
Me!cmbComboBox2.Requery
End Sub
It's also probably a good idea to define a parameter in order to insure that the reference to the form control gets appropriately resolved, so your Rowsource would be this:
PARAMETERS [Forms]![MyForm]![cmbComboBox1] Long;
SELECT * FROM MyTable
WHERE (MyField=[Forms]![MyForm]![cmbComboBox1]
AND IsNull([Forms]![MyForm]![cmbComboBox1])=False)
OR IsNull([Forms]![MyForm]![cmbComboBox1])=True
(assuming you're filtering on an Autonumber PK -- if the data type is different, you'd use a different data type, of course)
I would tend to use the dynamic Rowsource assignment, simply because I've found it to be less problematic across different Access versions (i.e., references to controls on forms are not resolved the same way in all versions of Access).
--
David W. Fenton
David Fenton Associates