Use an Access Form Control as a VBA Query Parameter
Question details
Use a combo box value from an open Access form to supply a parameter to a saved query in VBA.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- A VBA procedure needs to open a DAO recordset filtered by the value selected in a form's combo box.
- Observed behavior
- The saved query receives the selected control value before the DAO recordset opens.
Check the form and combo box names in Access, and confirm that the form is open when the procedure runs. Verify that the combo box's bound value has the same data type as the query parameter.
Assign the form value to a DAO QueryDef parameter
Use an explicitly named parameter to pass the combo box value to a saved query before opening its recordset.
This example assumes the combo box returns a numeric customer ID. Replace the example form, control, query, table, and field names with those in your database.
Open the saved query in Design View, switch to SQL View, and define a parameter that matches the field type. For example: PARAMETERS [pCustomerID] Long; SELECT * FROM Orders WHERE CustomerID = [pCustomerID]; Save the query as qryOrdersByCustomer.
In the VBA editor, add this code to a procedure, such as a button's Click event: Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset If IsNull(Forms!frmOrders!cboCustomer.Value) Then MsgBox "Select a customer first." Exit Sub End If Set db = CurrentDb Set qdf = db.QueryDefs("qryOrdersByCustomer") qdf.Parameters("pCustomerID").Value = Forms!frmOrders!cboCustomer.Value Set rs = qdf.OpenRecordset(dbOpenSnapshot) ' Process the records here. rs.Close Set rs = Nothing Set qdf = Nothing Set db = Nothing
Open frmOrders, select a customer in cboCustomer, and run the procedure or click its associated button. Add your record-processing code before rs.Close and check that the returned records match the selected customer.

Work with exported Access data in WPS Spreadsheets
WPS Office is a free, lightweight Office alternative with a familiar interface. Export Access data to Excel format to move it into WPS Spreadsheets; WPS does not run Access queries or fix Access DAO and VBA issues.

Frequently Asked Questions
Does the Access form need to be open when the code runs?
Yes. A reference such as Forms!frmOrders!cboCustomer.Value requires frmOrders to be open. Open the form and select a value before running the procedure.
What value does the combo box pass to the query?
The combo box's Value is its bound value, which may differ from the text displayed in the list. Check the combo box's Bound Column property in Design View and make sure its type matches the query parameter.
Why does Access report that a parameter is missing?
Open the saved query in SQL View and compare each parameter name with the names assigned through qdf.Parameters. Assign every required parameter before calling OpenRecordset.
Can I use a text value instead of a numeric ID?
Yes. In the query's SQL View, declare the parameter with an appropriate text type, such as PARAMETERS [pCustomerName] Text (255); then assign the combo box's text value to qdf.Parameters("pCustomerName").




