Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Selected listbox items to query

Status
Not open for further replies.

Dherrera

Programmer
Mar 22, 2004
86
US
can someone help me with this.
i have values in a multi-select listbox and i want to send the values selected by the user to a query. whats the best way to do this?
 
dherrara
Here's some code you can modify to suit your own situation, changing the names of the form, report, list box etc. It assumes that you are opening a report based on a query, but doesn't need to specify parameters in the query.

The code is behind a cmdPreview command button.

Code:
Private Sub cmdPreview_Click()
On Error GoTo Err_Preview_Click
Dim stDocName As String
stDocName = "rptSelectFromListBox"
Dim frm As Form, ctl As Control
    Dim varItm As Variant
    Dim strList As String

    strList = ""
    Set frm = Forms!frmSelectAddressesToPrint
    Set ctl = frm!List0
    For Each varItm In ctl.ItemsSelected
       strList = strList & ctl.ItemData(varItm) & ", "
    
    Next varItm
    strList = Left(strList, Len(strList) - 2)
DoCmd.OpenReport stDocName, acPreview, , "[UniqueID] IN (" & strList & ")"

'Then reset the list box to nothing
Dim ndx As Integer
For ndx = 0 To Me.List0.ListCount - 1
Me.List0.Selected(ndx) = False
Next
Me.txtSelected = Null
Exit_cmdSelectListBox_Click:
    Exit Sub

Err_cmdPreview_Click:
    MsgBox Err.Description
    Resume Exit_cmdPreview_Click
End Sub

Tom

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top