pi********@hotmail.com (Pieter Linden) wrote in message news:<bf**************************@posting.google. com>...
Paolo,
a few questions first... can the user select multiple options in the
listbox? If not, save yourself considerable headache and just use a
combobox. Otherwise, if you *really* want to use the listbox, there's
code here for what you want:
http://www.mvps.org/access/forms/frm0007.htm
Dev's idea of creating SQL from a multiselect listbox is a good one:
'******************** Code Start ************************
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = "Select * from Employees where [EmpID]="
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
For Each varItem In ctl.ItemsSelected
strSQL = strSQL & ctl.ItemData(varItem) & " OR [EmpID]="
Next varItem
'Trim the end of strSQL
strSQL=left$(strSQL,len(strSQL)-12))
'******************** Code end ************************
Instead of using the code above, I would use something like:
'******************** Code Start ************************
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim intI As Integer
Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = "Select * from Employees"
'Assuming long [EmpID] is the bound field in lb
'enumerate selected items and
'concatenate to strSQL
intI = 0
For Each varItem In ctl.ItemsSelected
intI = intI + 1
If intI = 1 Then
strSQL = strSQL & " WHERE [EmpID] = " & ctl.ItemData(varItem)
Else
strSQL = strSQL & " OR [EmpID]= " & ctl.ItemData(varItem)
End If
Next varItem
'******************** Code end ************************
so that the case where no items are selected creates a valid SQL
string. In a more generalized version that includes other criteria
just tack on the " OR ... " parts for each selected item in the
listbox onto the WHERE part.
James A. Fortune