I have a multi-select list box on one of my forms. I want to pass the selected items to stored query as a parameter. I know how to concatenate all the selected items, however, I can not work out how to get the query to accept the concatenated string as a parameter. For example, we have various grant applicants in our database, ie. A- Area, C- CERP, U – University and so on. I need to open a stored query based on the applicants selected in the multi-select list box.
I tried concatenating selected applicants and storing the concatenated string in the text box called txtApplicants and then referring to txtApplicants in query criteria. For instance, if the user selects A and U list items, I would store “A”, “U” in the textbox and refer to it in the criteria using “IN” statement, ie. IN ([forms]![frmGrants]![txtApplicants]).
Normally if I type IN (“A”, “U”) in the criteria box I would get a certain number of records, however, referring to a form field generates an empty recordset. I also tried using LIKE statement without success.
I would appreciate any help I can get.
PS: I took a look at an example posted by Dev Ashish on The access Web (httt://www.mvps.org/access/forms/frm0007.htm), which discussed this topic.
He actually did have a note stating that his method can be used in a parameterized query provided I pass the entire Where clause to it via code as a parameter. The question remains, how do I pass the entire Where clause to parameterized query via code as a parameter?
Following is the article from httt://www.mvps.org/access/forms/frm0007.htm. Could any one let me know how to achive my objective? Better still, if you work it out using a database, could you send me the solution database to have a look it at john.fejsa@hunter.health.nsw.gov.au. Much appreciated.
===========================================================
(Q) I have a MultiSelect listbox control on my form. I want to pass the selected items to a query as a parameter. How do I do this?
(A) Unlike simple listbox controls which can be referenced as a parameter by a query, MultiSelect listboxes cannot be used directly as a parameter. This is because calling the listbox (Forms!frmMyForm!lbMultiSelListBox) from anywhere will not automatically concatenate all the selected items. You need to build the criteria yourself.
Note: You can still use a parameterized query provided you pass the entire Where clause to it via code as a parameter. (eg. Have the query reference a hidden control to which you manually assign the complete WHERE clause using the following logic.)
For example,
‘******************** Code Start ************************
Dim frm As Form, ctl As Control
Dim varItem As Variant
Dim strSQL As String
Set frm = Form!frmMyForm
Set ctl = frm!lbMultiSelectListbox
strSQL = “Select * from Employees where [EmpID]=”
‘Assuming long [EmpID] is the bound field in lb
‘enumerate selected items and
‘concatenate to strSQL
For Each varItem In ctl.ItemsSelected
strSQL = strSQL & ctl.ItemData(varItem) & ” OR [EmpID]=”
Next varItem
‘Trim the end of strSQL
strSQL=left$(strSQL,len(strSQL)-12))
‘******************** Code end ************************
===========================================================
Thanks in advancejohn.fejsa@hunter.health.nsw.gov.au