mike said:
How do I take the selected values of a multiselect list
box and use them as filter criteria in a query
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I use the following code to extract the selected values in a multi
select ListBox. The return string can be used in the WHERE clause of a
query or in a form/report Filter property. E.g. (assumes there is
something selected in ListBox):
Me.Filter = "ID In (" & getIds(lstMyListBox, dbNumeric) & ")"
To put in a query (using VBA - strSQL holds 1st part of query):
strSQL = strSQL & "WHERE ID In (" & getIds(lstMyListBox, _
dbNumeric) & ")"
Public Function getIDs(lst As Control, ByVal intType As Integer) As
String
' Purpose:
' Get a list of the item IDs into a comma separated string
' This code assumes the List Box's BoundColumn holds the IDs.
' In:
' lst A ref to a list box control
' intType One of the dbText, dbNumeric, etc. constants.
' Out:
' A string of comma-delimited IDs. Format: "1,2,3,4"
' If the intType is undefined an empty string is returned.
' Created:
' mgf 8mar2000
' Modified:
' mgf 10mar2000 Added intType selection
Dim row As Variant
Dim strIDs As String
For Each row In lst.ItemsSelected
Select Case intType
Case dbText
strIDs = strIDs & "'" & Replace(lst.ItemData(row), _
"'", "''") & "',"
Case dbDate, dbTime
strIDs = strIDs & "#" & lst.ItemData(row) & "#,"
Case dbNumeric
strIDs = strIDs & lst.ItemData(row) & ","
Case Else
' Don't know how to handle this type
Exit Function
End Select
Next row
' Return string w/o trailing comma
getIDs = Left$(strIDs, Len(strIDs) - 1)
End Function
- --
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQF99/oechKqOuFEgEQL8uQCeJINf9kWz6ZZyrnG4R/vYl+PIzKkAnRiO
39PfoYF0Q5+dYLzFlJFoJPN7
=hl1d
-----END PGP SIGNATURE-----