How do I assign a pass-through query to Row Source that is dependent on another value in the form?
Essentially I want to do this:
SELECT x.companyid,
x.companyname,
x.productid
FROM x
WHERE (((x.CompanyID) = [Forms]![Reporting]![CompanyID_Control]))
ORDER BY x.productid;
But of course pass-through queries do not support reference to any form controls.
I have read here that there is a method via VBA, however I do not know how to use VBA in conjunction with the Row Source of a control.
As Remou stated in his answer, linked tables will make this easier. However, if you have a pass-through query named MyQuery
, you can do the following to make the RowSource of a MyComboOrListBox
control update dynamically when the value of the CompanyID_Control
changes:
Private Sub CompanyID_Control_AfterUpdate()
Dim SQL As String, qdf AS DAO.QueryDef
Set qdf = CurrentDB.QueryDefs("MyQuery")
qdf.SQL = " SELECT x.companyid, x.companyname, x.productid " & _
" FROM x " & _
" WHERE x.CompanyID =" & Me.CompanyID_Control & _
" ORDER BY x.productid;"
Me.MyComboOrListBox.RowSource = "MyQuery"
End Sub
You'll also need to set the AfterUpdate
property of the CompanyID_Control
to:
[Event Procedure]
.
Note that even if you use linked tables as Remou suggested, you will still need code in the AfterUpdate
event of the CompanyID_Control
to refresh your combobox/listbox RowSource:
Private Sub CompanyID_Control_AfterUpdate()
Me.MyComboOrListBox.Requery
End Sub