Multiselect listbox

F

Fay

I went to support.microsoft.com/?kbid=135546 to get information. I am trying
the second example after failing with the first version.

I use Access03.

Situation: I have a main form, frmListing, bound to qryListingLearners.
qryListingLearners is comprised of two tables, tblLearners and
tblLearnerDepartments. There are two unbound listboxes on the form in the
header. The first listbox, lstDept, rowsource is from tblDepartments and
displays one column, Department. The second listbox, lstPosition, rowsource
is tblPosition. Currently I am just working with the lstDept. On the main
form there is a datasheet subform called fsubListing with is also bound to
qryListingLearners.

I am trying the following code

Dim Q As QueryDef, DB As Database
Dim Criteria As String
Dim ctl As Control
Dim Itm As Variant

' Build a list of the selections.
Set ctl = Me![lstDept]

For Each Itm In ctl.ItemsSelected
If Len(Criteria) = 0 Then
Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34)
Else
Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _
& Chr(34)
End If
Next Itm

If Len(Criteria) = 0 Then
Itm = MsgBox("You must select one or more items in the" & _
" list box!", 0, "No Selection Made")
Exit Sub
End If

' Modify the Query.
Set DB = CurrentDb()
Set Q = DB.QueryDefs("qryListing")
Q.SQL = "Select * From qryListingLearners Where [PerDiem2Unit] In(" &
Criteria & _
");"
Q.Close

' Run the query.
DoCmd.OpenQuery "qryListingLearners"

The returned data is not limited to the items selected in the lstDept
control. Where have I gone wrong?

Thank you for your help.

Fay
 

Ask a Question

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.

Ask a Question

Top