Question:
running a query or report in access using value from combo box?
dcjames
2008-05-25 12:34:07 UTC
I have a combo box on a form with values in a dropdown style that come from a field on a table. I want to run a query using a selected value from the combo box. How do i do this? I am a beginniner please use exact syntax assuming : table1, query1, form1, combobox0
Thanks in advance!
Five answers:
Lyreceus
2008-05-29 20:53:50 UTC
1. SETUP THE QUERY YOU WISH TO EXECUTE BASED ON THE VALUE OF THE COMBO-BOX



The query must have, in the 'criteria' line of the combo-box field, a reference to the value of the combo-box on your form.



In the query design, put something like this in the 'criteria' line of the combo-box field:



[Forms]![frmFormName]![cboComboName]



([Forms] is the forms collection, and the other two are your form name and your combo-box name.)



2. SETUP THE AFTERUPDATE EVENT TRIGGER FOR THE QUERY



You could execute the query when the user changes the value in the combo-box by using the AfterUpdate event of the combo-box (it kicks-off when the value is changed).



It would look like this:



*********************

Private Sub cboComboName_AfterUpdate()



Dim strQueryName as String



strQueryName = "qryYourQueryName"



'I think you must have this line to save the record, first

DoCmd.RunCommand acCmdSaveRecord



'Then execute the query

DoCmd.OpenQuery strQueryName



End Sub

********************



When you make a new selection in the combo-box, the query will execute, using the new value of the combo-box as criteria.



=============================



EDIT: You must enter the reference to the combo-box on the query exactly as shown or you will get a syntax error. The bracketed elements are separated by exclamation points in:



[Forms]![frmFormName]![cboComboName]



Also, this line was left out of my original AfterUpdate code above, and I have added it above:



strQueryName = "qryYourQueryName"



(I don't think that would cause a syntax error, though.)



I have gotten this to work, using the method and syntax as shown. Try again and I bet you can get it to work.



.
brinser
2017-01-01 12:16:34 UTC
Access Combo Box Value
sayles
2016-09-28 07:38:01 UTC
Combo Box Access
Rosa
2016-03-13 06:00:36 UTC
The linking fields must contain the same kind of data and have the same or a compatible data type or field size. For example, an AutoNumber field with its FieldSize property set to Long Integer is compatible with a Number field with its FieldSize property set to Long Integer. Make sure that the LinkChildFields property is set to the name of a field (as specified in the ControlSource property), not the name of a control (as specified in the Name property). The LinkMasterFields property can be set to the name of either a field or a control.
anonymous
2008-05-25 16:23:37 UTC
Assuming that it's the search criterion you're getting from the combobox:



If the field you're searching on is numeric:



"Select * (or the fields you want) from table1 where field1 = " & combobox0.Text



If it's text:



"Select * (or the fields you want) from table1 where field1 = '" & combobox0.Text & "'"



That last one is double-quote - single-quote - double-quote.



How you use it depends on whether you're using ADO, DAO or something else. I use ADO, and open the recordset using that string.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...