A
AnDe
I want to use the values selected in a list box as parameters for a query.
[email protected] said:Normally in a query to return rows on the basis of an arbitrary set of
values in the same column you'd use the IN operator with a value list
as its argument, but this does not accept a parameter as the
argument. However, you'll find a couple of methods of simulating it
at:
http://support.microsoft.com/kb/100131/en-us
The second method using the InParam and GetToken functions is probably
the more reliable and simpler to use and will cope with different data
types in exactly the same way. First paste the two functions into a
standard module in your database. Then in the form with the list box
add a hidden text box, txtHiddenParameter say. You need to fill the
text box with a value list delimited by commas, so in the list box's
AfterUpdate event procedure put:
Dim varItem As Variant
Dim strValueList As String
Dim ctrl As Control
Set ctrl = Me.ActiveControl
ctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strValueList = strValueList & "," & ctrl.ItemData(varItem)
Next varItem
' remove leading comma
strValueList = Mid(strValueList, 2)
Me.txtHiddenParameter = strValueList
In the query put the following in the 'field' row of a blank column in
the design grid:
InParam([YourFieldName],[Forms]![YourFormName]!
[txtHiddenParameter])
uncheck the 'show' check box, and in the 'criteria' row put:
True
or in SQL view put:
WHERE InParam([YourFieldName],[Forms]![YourFormName]!
[txtHiddenParameter])
Ken Sheridan
Stafford, England
I want to use the values selected in a list box as parameters for a query.
Ken,
I'm experimenting with this technique. I think you intended an "If"
statement:
If ctrl.ItemsSelected.Count > 0 Then
I always get a "0" for ctrl.ItemsSelected.Count in a List Box
What am I doing wrong? Thanks.
David
[email protected] said:Normally in a query to return rows on the basis of an arbitrary set of
values in the same column you'd use the IN operator with a value list
as its argument, but this does not accept a parameter as the
argument. However, you'll find a couple of methods of simulating it
at:
The second method using the InParam and GetToken functions is probably
the more reliable and simpler to use and will cope with different data
types in exactly the same way. First paste the two functions into a
standard module in your database. Then in the form with the list box
add a hidden text box, txtHiddenParameter say. You need to fill the
text box with a value list delimited by commas, so in the list box's
AfterUpdate event procedure put:Dim varItem As Variant
Dim strValueList As String
Dim ctrl As ControlSet ctrl = Me.ActiveControlctrl.ItemsSelected.Count > 0 Then
For Each varItem In ctrl.ItemsSelected
strValueList = strValueList & "," & ctrl.ItemData(varItem)
Next varItem' remove leading comma
strValueList = Mid(strValueList, 2)Me.txtHiddenParameter = strValueListIn the query put the following in the 'field' row of a blank column in
the design grid:InParam([YourFieldName],[Forms]![YourFormName]!
[txtHiddenParameter])
uncheck the 'show' check box, and in the 'criteria' row put:True
or in SQL view put:WHERE InParam([YourFieldName],[Forms]![YourFormName]!
[txtHiddenParameter])Ken Sheridan
Stafford, England
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.