Hi everyone,
i've been trying to create a multi select list box that passes the items selected as parameters to a query. i search the forum and found some posts related to this subject, but i could not understand them. i did some research and found a procedure that uses DAO to perform this action from the OnClick event for a control button on a form. i compiled the code and it seems to work, but i think i have my references mixed up.
here's a little reference background on my dbase:
i created the form "multi select form" that has a list box named "MultiSelectListBox". this list box uses a table named "Units" as the control source. i created a query based on a table named "Master Table". this is the table i want the query to search from. within the "Master Table" the query is suppose to query on the "Unit Description" field.
i used the following code on the OnClick event for a command button on the "multi select form".
Private Sub MultiSelectQuery_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("MultiSelectQuery")
For Each varItem In Me!MultiSelectListBox.ItemsSelected
strCriteria = strCriteria & ",'" & Me!MultiSelectListBox.ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM [Master Table] " & _
"WHERE [Master Table].[Unit Description] IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "MultiSelectQuery"
Set db = Nothing
Set qdf = Nothing
End Sub
whenever i run this query, a little parameter dialog box appears asking for input for the [Master Table.Unit Description]. then even when i manually type (single selection) the information i selected from the list box into the dialog box i still get an empty dynaset. can someone tell me what i'm doing wrong? any help will be GREATLY appreciated.
thanks
i've been trying to create a multi select list box that passes the items selected as parameters to a query. i search the forum and found some posts related to this subject, but i could not understand them. i did some research and found a procedure that uses DAO to perform this action from the OnClick event for a control button on a form. i compiled the code and it seems to work, but i think i have my references mixed up.
here's a little reference background on my dbase:
i created the form "multi select form" that has a list box named "MultiSelectListBox". this list box uses a table named "Units" as the control source. i created a query based on a table named "Master Table". this is the table i want the query to search from. within the "Master Table" the query is suppose to query on the "Unit Description" field.
i used the following code on the OnClick event for a command button on the "multi select form".
Private Sub MultiSelectQuery_Click()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim varItem As Variant
Dim strCriteria As String
Dim strSQL As String
Set db = CurrentDb()
Set qdf = db.QueryDefs("MultiSelectQuery")
For Each varItem In Me!MultiSelectListBox.ItemsSelected
strCriteria = strCriteria & ",'" & Me!MultiSelectListBox.ItemData(varItem) & "'"
Next varItem
If Len(strCriteria) = 0 Then
MsgBox "You did not select anything from the list" _
, vbExclamation, "Nothing to find!"
Exit Sub
End If
strCriteria = Right(strCriteria, Len(strCriteria) - 1)
strSQL = "SELECT * FROM [Master Table] " & _
"WHERE [Master Table].[Unit Description] IN(" & strCriteria & ");"
qdf.SQL = strSQL
DoCmd.OpenQuery "MultiSelectQuery"
Set db = Nothing
Set qdf = Nothing
End Sub
whenever i run this query, a little parameter dialog box appears asking for input for the [Master Table.Unit Description]. then even when i manually type (single selection) the information i selected from the list box into the dialog box i still get an empty dynaset. can someone tell me what i'm doing wrong? any help will be GREATLY appreciated.
thanks