One way is to rebuild the query-SQL to prompt for desired criteria.
Suppose you had query27, based on Northwind's Orders table, as shown below:
PARAMETERS [enter order date] DateTime, [enter ship country] Text;
SELECT Orders.OrderID, Orders.CustomerID, Orders.OrderDate, Orders.ShipCountry
FROM Orders
WHERE (((Orders.OrderDate)<[enter order date]) AND ((Orders.ShipCountry)=[enter ship country]));
This will prompt both for an Order Date and the Ship Country.
The following function, called from a command button on each form:
For the date prompt: Call splitquery("query27", "date")
For the country prompt: Call splitquery("query27", "country")
Will create and open query "TempFilter", based upon your calling choice (date or country).
Function SplitQuery(myQuery As String, mytype As String)
Dim db As Database
Dim qd As QueryDef
Dim tName As String, test As String
Dim xleft, xmid, xright, strSQL, intType
Set db = CurrentDb
Set qd = db.QueryDefs(myQuery)
strSQL = qd.SQL
On Error Resume Next
tName = "TempFilter"
'Does query "TempFilter" exist? If true, delete it;
test = db.QueryDefs(tName).Name
If Err <> 3265 Then
DoCmd.DeleteObject acQuery, "TempFilter"
End If
intType = IIf(mytype = "Date", 1, 2)
'break query into sections
xleft = Left(strSQL, InStr(strSQL, ";"))
xmid = Mid(strSQL, InStr(strSQL, ";") + 1)
xmid = Left(xmid, InStr(xmid, "WHERE") - 1)
xright = Mid(strSQL, InStr(strSQL, "WHERE"))
xleft = "PARAMETERS " & IIf(intType = 1, "[enter order date] DateTime;", "[enter ship country] Text;")
xright = "WHERE " & IIf(intType = 1, "(Orders.OrderDate)<[enter order date];", "(Orders.ShipCountry)=[enter ship country];")
strSQL = xleft & xmid & xright
Set qd = db.CreateQueryDef("TempFilter", strSQL)
db.QueryDefs.Refresh
DoCmd.OpenQuery "TempFilter", acViewNormal
qd.Close
db.Close
Set db = Nothing
End Function