M
Mark Kubicki
i'm trying to open a query based on a multi selection from a list box
the query is based on a table: Manufacturers
which includes the fields: Manufacturer and PrimaryCatagory
I would like to show all of the records where the field PrimaryCatagory
matches any of the selections from the list box
the name of the list box is lstCatagorySelect
----> the SQL string "seems" to be formatting correctly, but returns the
error "item not found in this collection"
any suggestions would be greatly appreciated...
thanks in advance,
mark
'-------------- start of code ------------------
Private Sub cmdOpenQuery_Click()
Set MyDB = CurrentDb()
strSQL = "SELECT Manufacturers.Manufacturer,
Manufacturers.PrimaryCatagory FROM Manufacturers"
' Build string by looping through the listbox
For i = 0 To lstCatagorySelect.ListCount - 1
If lstCatagorySelect.Selected(i) Then
If lstCatagorySelect.Column(0, i) = "<All>" Then
flgSelectAll = True
End If
strIN = strIN & "((Manufacturers.PrimaryCatagory)='" &
lstCatagorySelect.Column(0, i) & "')) OR ("
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE " & _
"(" & Left(strIN, Len(strIN) - 4)
'If "<All>" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryCatagories"
Set qdef = MyDB.CreateQueryDef("qryCatagories", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryCataogies", acViewNormal
the query is based on a table: Manufacturers
which includes the fields: Manufacturer and PrimaryCatagory
I would like to show all of the records where the field PrimaryCatagory
matches any of the selections from the list box
the name of the list box is lstCatagorySelect
----> the SQL string "seems" to be formatting correctly, but returns the
error "item not found in this collection"
any suggestions would be greatly appreciated...
thanks in advance,
mark
'-------------- start of code ------------------
Private Sub cmdOpenQuery_Click()
Set MyDB = CurrentDb()
strSQL = "SELECT Manufacturers.Manufacturer,
Manufacturers.PrimaryCatagory FROM Manufacturers"
' Build string by looping through the listbox
For i = 0 To lstCatagorySelect.ListCount - 1
If lstCatagorySelect.Selected(i) Then
If lstCatagorySelect.Column(0, i) = "<All>" Then
flgSelectAll = True
End If
strIN = strIN & "((Manufacturers.PrimaryCatagory)='" &
lstCatagorySelect.Column(0, i) & "')) OR ("
End If
Next i
'Create the WHERE string, and strip off the last comma of the IN string
strWhere = " WHERE " & _
"(" & Left(strIN, Len(strIN) - 4)
'If "<All>" was selected in the listbox, don't add the WHERE condition
If Not flgSelectAll Then
strSQL = strSQL & strWhere
End If
MyDB.QueryDefs.Delete "qryCatagories"
Set qdef = MyDB.CreateQueryDef("qryCatagories", strSQL)
'Open the query, built using the IN clause to set the criteria
DoCmd.OpenQuery "qryCataogies", acViewNormal