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!

Getting a string from a multi select list box.

Status
Not open for further replies.

greymonkey

Technical User
Jul 20, 2003
29
AU
I have a multi select list box listing client’s names and I need to get the selected names into the criteria for a query.

If you have any ideas or sample code which would sort this problem it would help me a lot.

Cheers,
 
This should do it for you:

Note that it assumes that you are looking for a string (not a number)
Code:
    Dim strSQL as string
    Dim strSQLWhere as string
    dim varItem as variant
   
   strSQL = "Select * From YourTable"
   strSQLWhere = vbNullString
   
    If (YourListBox.ItemsSelected.Count > 0) then
        strSQLWhere = " WHERE "
        For Each varItem In YourListBox.ItemsSelected
            strSQLWhere = strSQLWhere & "YourFieldName ='" & lst.column(0,varItem) & "' OR "
        Next varItem

        strSQLWhere = Left$(strSQLWhere, Len(strSQLWhere) - 4)  'Get rid of last OR
    End If

   strSQL = strSQL & strSQLWhere

   Set dbs = CurrentDb
   dbs.QueryDefs.Delete "YourQueryName"
   Set qdf = dbs.CreateQueryDef("YourQueryName",strSQL)
   strSQL  = vbNullString
   DoEvents

ExitProcedure:
    Exit Sub

ErrHandler:

    If (Err.Number = 3265) Then         'IFT, tryed to delete a query that did not exist.
        
        Resume Next

    Else

        Msgbox err.number & err.description
        Resume ExitProcedure
    End if

End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top