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