Multi-Select Listbox as Criteria for Parameter Query

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I know this can be done - because I've done it before but for the life of me
I can't remember how.

I need to run a query - returning records that match the selection the user
makes in a multi-select listbox.

IE: List the Project Managers for the 3 or 4 Projects the user selects in a
list box.

Does anyone have an example of how this might be done?

Thanks Very Much!
 
Use the ItemsSelected property of the List Box to build the Where condition
for your query.
 
There's a sample at http://www.mvps.org/access/forms/frm0007.htm at "The
Access Web". I have to admit, though, that I'm particularly impressed with
that particular sample. The following produces short SQL statements:

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim strWhere As String

Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = "Select * from Employees"
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ", "
Next varItem

If Len(strWhere) > 0 The
strSQL= strSQL & " where [EmpID] In (" & _
left$(strSQL,len(strSQL)-2)) & ")"
End If
 
I used the code snippet below and modified it for my use but ended up getting
an Error 2465 Microsoft Office Access Can't find the field 'frmSearch'
referred to in your expression.

Here is my code:

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim strWhere As String

Set frm = Form!frmSearch
Set ctl = frm!txtSegments

strSQL = "Select * from tblSegmentOutput_Test"

For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ", "
Next varItem

If Len(strWhere) > 0 Then
strSQL = strSQL & " [SegmentName] In (" & _
Left$(strSQL, Len(strSQL) - 2) & ")"
End If

--
Cyberwolf
Finder of Paths, Hunter of Prey
Ghost of the Night, Shadow of Day
The Wolf


Douglas J. Steele said:
There's a sample at http://www.mvps.org/access/forms/frm0007.htm at "The
Access Web". I have to admit, though, that I'm particularly impressed with
that particular sample. The following produces short SQL statements:

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim strWhere As String

Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = "Select * from Employees"
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ", "
Next varItem

If Len(strWhere) > 0 The
strSQL= strSQL & " where [EmpID] In (" & _
left$(strSQL,len(strSQL)-2)) & ")"
End If


--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Klatuu said:
Use the ItemsSelected property of the List Box to build the Where
condition
for your query.
 
Try

Set frm = Forms!frmSearch


Forms! (with an "S")

HTH
--
Steve S
--------------------------------
"Veni, Vidi, Velcro"
(I came; I saw; I stuck around.)


Cyberwolf said:
I used the code snippet below and modified it for my use but ended up getting
an Error 2465 Microsoft Office Access Can't find the field 'frmSearch'
referred to in your expression.

Here is my code:

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim strWhere As String

Set frm = Form!frmSearch
Set ctl = frm!txtSegments

strSQL = "Select * from tblSegmentOutput_Test"

For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ", "
Next varItem

If Len(strWhere) > 0 Then
strSQL = strSQL & " [SegmentName] In (" & _
Left$(strSQL, Len(strSQL) - 2) & ")"
End If

--
Cyberwolf
Finder of Paths, Hunter of Prey
Ghost of the Night, Shadow of Day
The Wolf


Douglas J. Steele said:
There's a sample at http://www.mvps.org/access/forms/frm0007.htm at "The
Access Web". I have to admit, though, that I'm particularly impressed with
that particular sample. The following produces short SQL statements:

Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Dim strWhere As String

Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = "Select * from Employees"
For Each varItem In ctl.ItemsSelected
strWhere = strWhere & ctl.ItemData(varItem) & ", "
Next varItem

If Len(strWhere) > 0 The
strSQL= strSQL & " where [EmpID] In (" & _
left$(strSQL,len(strSQL)-2)) & ")"
End If


--
Doug Steele, Microsoft Access MVP

(no private e-mails, please)


Klatuu said:
Use the ItemsSelected property of the List Box to build the Where
condition
for your query.

:

I know this can be done - because I've done it before but for the life of
me
I can't remember how.

I need to run a query - returning records that match the selection the
user
makes in a multi-select listbox.

IE: List the Project Managers for the 3 or 4 Projects the user selects in
a
list box.

Does anyone have an example of how this might be done?

Thanks Very Much!
 
Back
Top