Query with multiple parameters

knaabis

Active Member
Joined
Apr 25, 2006
Messages
254
Office Version
  1. 2013
Platform
  1. Windows
How make a query with multiple parameters?
For example i need to enter many parameters - [Order].
What i need to enter in query Criteria fiels?
If i put there only [Order], then i get in dialog box only one possibilities - i can enter only one order number.
But i need to make query by many criterias, for exaple - order 10, 15, 27, 30.
These order numbers not static - these numbers whenever is different.
I need to enter each time manualy in dialog box.
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Do you need to use a dialog box. How about creating a form that holds a multiselect list box to pass the selections into your query's criteria argument?

You need to create the list box on a form. Then create a button on the form that runs your query. In the relevant field in your query you need to enter the name of the control that you have created on the form to hold your list box.

Using only singleselect listbox:

So if your form is called frmMyForm and the list box is list0, then as a criterion in your query under [Employee ID] you enter
[forms].[frmMyForm].[list0]
This will then take the value selected in the list box and use it as the criterion in the query.

Using multiselect listbox:
Using a singleselect listbox you do not need vba code to pass the criteria to the field criteria argument in a query, however, using a multiselect listbox you need to pass the multiple criteria to the field criteria argument in your query in the form of an "IN" argument using vba as follows.

Adjust the below code to suit your object references and place this code in the "on click" event of a commandbutton on your form.
Code:
Dim varItem as variant
Dim mycriteria as String

 With Me.list0 'this is your listbox on your form
        For Each varItem In .ItemsSelected
            If Not IsNull(varItem) Then
                mycriteria = mycriteria & "'" & .ItemData(varItem) & "'" & ","
             
            End If
        Next
    End With

lngLen = Len(mycriteria) - 1
' The SQL statement will be passed to the query using vba as follows
mystring1 = "(([Table1].[Field1]) IN (" & Left$(mycriteria, lngLen) & "))" ' This represents your IN argument in the Queries SQL statement holds the multiple criteria selected in the listbox

myQuery = "[Query1]" 'name of query to run and pass the SQL statement to

Dim strSQL As String
Dim qdf As DAO.QueryDef


Set qdf = CurrentDb.QueryDefs(myQuery)
st5 = "SELECT DISTINCT [Table1].field1, [Table1].field2 " ' adjust the object references and SELECT statement to match your objects and desired Query type
st6 = "FROM [Table1]" ' adjust the object reference here
st7 = " WHERE " & mystring1 & " ;"


strSQL = st5 & st6 & st7


qdf.SQL = strSQL

Set qdf = Nothing

Just a quick sample of code needs some reference adjustments if you think this is helpful reply with your object names (Table name, Query name, Form name, Listbox name) and someone can help you adjust the code for proper references.
 
Upvote 0
Thanks, but then for me better is to get these [Order] numbers from Table (Form).
These [Order] numbers is entered in Table called "Cutting_List" >SubTable "Cutting_List_Details"
There is Field called "Order_No".
Same names has also Forms
Will be nice, if i can press button from "Cutting_List" Form and get this Query filtered by this Form>Subform data.
 
Upvote 0
If i enter in Queri criteria field like this:

[Forms]![Cutting_List]![Cutting_List_Details subform]![Order_No]

then i get query result only by first entry in this subform...
But there are more entries!
 
Upvote 0
Thanks, but then for me better is to get these [Order] numbers from Table (Form).
These [Order] numbers is entered in Table called "Cutting_List" >SubTable "Cutting_List_Details"
There is Field called "Order_No".
Same names has also Forms
Will be nice, if i can press button from "Cutting_List" Form and get this Query filtered by this Form>Subform data.

Instead of selecting multiple criteria (order #'s) from a form list box, you would like the criteria to come from a table? My first suggestion is to query the table to generate a list of unique order #'s then adjust the code (previously included in this post) to loop through each record in the query and create the criteria string to pass to your SQL statement...Or create a list box on your form that references the query holding all of your unique order #'s from your table and create the criteria string by looping through each item in your list box...Might be easier using this method as opposed to trying to open the query as recordset and looping through the records. Does this make sense? I'll post back with sample code on how to cycle through each item in a list box to create a criteria string for your SQL statement.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,735
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top