Ken-
I tried the code that you provided. I seem to have run into a problem
though. When I try and run the query it gives me an error message that says
"Undefined function'InParam' in Expression."
Here is a copy of my SQL code for my Query.
SELECT CPM.[Ship Week], CPM.[Destination State], CPM.[Avg CPM], CPM.[Avg
RPM], [Weekly Fuel Rates].[Fuel Price]
FROM [Weekly Fuel Rates] INNER JOIN CPM ON [Weekly Fuel
Rates].Week=CPM.[Ship Week]
WHERE (((CPM.[Ship Week]) Between (Forms!frmStartupscreen!StartDate) And
(Forms!frmStartupscreen!EndDate)) And
((InParam([DestinationState],Forms!frmStartupscreen!txtStatesHidden))=True));
Perhaps my code is wrong? I'm still pretty new at this and not exactly sure
what I am looking at.
Any ideas what I could be doing wrong?
Thanks
:
To reference the selections form the list box as a parameter you'd
firstly have to build a value list in a hidden text box control so
that the query has something to reference. However, the IN operator
does not accept a parameter as its argument, so you have to simulate
it. You'll find a couple of ways of doing this at:
http://support.microsoft.com/kb/100131/en-us
I'd recommend the second solution, using the InParam function as this
makes building the value list more straightforward as it caters for
any data type in exactly the same way, and you don't have to cater for
the possibility of a value being a substring of another value. So
using that method, if the hidden text box on the form is named
txtStatesHidden say, and the list box is named States the code to fill
the text box, which you'd put in the button's Click event procedure
before code to open the report etc, would be like this to open a
report in print preview for example:
Dim varItem As Variant
Dim strStatesList As String
Dim strCriteria As String
Dim ctrl As Control
Set ctrl = Me.States
' loop through list box's ItemsSelected collection
' and build comma separated list of selected items
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strStatesList = strStatesList & "," & ctrl.ItemData
(varItem)
Next varItem
' remove leading comma
strStatesList = Mid(strStatesList, 2)
' assign value list to hidden text box
Me.txtStatesHidden = strStatesList
DoCmd.OpenReport "YourReport", _
View:=acViewPreview
Else
MsgBox "No States Selected", vbInformation, "Warning"
End If
The query would be restricted as follows:
WHERE InParam(State, Forms!YourForm!txtStatesHidden)
where State is the name of the column in the table. In query design
view you'd enter InParam([State], Forms!YourForm!txtStatesHidden) into
the 'field' row of a blank column in the design grid, uncheck the
'show' check box, and in the column's 'criteria' row you'd enter True.
An alternative approach would be not to restrict the query at all, so
you don't need the hidden text box or the InParam function, but to
build a string expression for the WhereCondtion of the OpenReport
method:
Dim varItem As Variant
Dim strStatesList As String
Dim strCriteria As String
Dim ctrl As Control
Set ctrl = Me.States
' loop through list box's ItemsSelected collection
' and build comma separated list of selected items
If ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strStatesList = strStatesList & ",""" & ctrl.ItemData
(varItem) & """"
Next varItem
' remove leading comma
strStatesList = Mid(strStatesList, 2)
strCriteria = "State In(" & strStatesList & ")"
DoCmd.OpenReport "YourReport", _
View:=acViewPreview, _
WhereCondition:=strCriteria
Else
MsgBox "No States Selected", vbInformation, "Warning"
End If
This assumes that the State column in the table is a text data type
rather than a 'surrogate' numeric ID value.
Ken Sheridan
Stafford, England
I have created a form that displays when I startup the database called
frmStartupscreen. On that form are 3 command buttons to run a form, report
and chart. There is also a place to select multiple states out of a list box.
the list box is named States. What I would like to do is take the selections
from the States list box and apply it to a query named CPM Query that runs
the report and chart.
Is there a way that I can do that using the criteria box in the design view
of the Query? Or would I need to know VB or SQL? I'll be honest, I don't have
any experience in those two, so any help would be greatly appreciated!
Thanks