itchyII
MIS
- Apr 10, 2001
- 167
Hello all,
I have an interesting problem. I am running Access 2003. I have a form that has a listbox. The listbox contains a set list of values (department numbers) and the Multi Select option is set to “Extended”. The purpose of the list box is so that the user can select one or many of the departments and then print a report based on this information. I have a function that I wrote to go through the list box selections and create a list and I have placed a reference to the function in the criteria row of a query.
Here is the function code:
The problem is, that if the user selects only one department, this works fine. If the user selects multiple departments, the query returns no records. I think that it might have something to do with the fact that the field I am trying to filter on in the query is set as a text field, but really contains numeric like values (30,31,32, etc). I am passing a string to the criteria row in the query but I think Access is adding quotes somewhere and it is screwing everything up. I tried passing the string with the single quotes already included, but it still doesn’t work. I even tried outputting the function to text box on the form and then tried referencing the text box in the criteria of the query, instead of the function. This gave me the same problem. But if I copy the text from the text box and paste it into the criteria row of the query, the query works fine. Has anyone experienced this problem?
Thanks,
ItchyII
I have an interesting problem. I am running Access 2003. I have a form that has a listbox. The listbox contains a set list of values (department numbers) and the Multi Select option is set to “Extended”. The purpose of the list box is so that the user can select one or many of the departments and then print a report based on this information. I have a function that I wrote to go through the list box selections and create a list and I have placed a reference to the function in the criteria row of a query.
Here is the function code:
Code:
Function returnSelectedDepartments() As String
Dim list As String
For Each itm In Forms!frmMain!DeptList.ItemsSelected
If Forms!frmMain!DeptList.Column(0, itm) = "All" Then
list = "30 OR 31 OR 32 OR 33 OR 35 OR 36 OR 37 OR 38 OR 39 OR 40 OR 41 OR 42"
'also tried this -- list = "'30' OR '31' OR '32' OR '33' OR '35' OR '36' OR '37' OR '38' OR '39' OR '40' OR '41' OR '42'"
Else
If list = "" Then
list = list & Forms!frmMain!DeptList.Column(0, itm)
'also tried this -- list = list & "'" & Forms!frmMain!DeptList.Column(0, itm) & "'"
Else
list = list & " OR " & Forms!frmMain!DeptList.Column(0, itm)
'also tried this -- list = list & " OR " & "'" & Forms!frmMain!DeptList.Column(0, itm) & "'"
End If
End If
Next itm
returnSelectedDepartments = list
End Function
The problem is, that if the user selects only one department, this works fine. If the user selects multiple departments, the query returns no records. I think that it might have something to do with the fact that the field I am trying to filter on in the query is set as a text field, but really contains numeric like values (30,31,32, etc). I am passing a string to the criteria row in the query but I think Access is adding quotes somewhere and it is screwing everything up. I tried passing the string with the single quotes already included, but it still doesn’t work. I even tried outputting the function to text box on the form and then tried referencing the text box in the criteria of the query, instead of the function. This gave me the same problem. But if I copy the text from the text box and paste it into the criteria row of the query, the query works fine. Has anyone experienced this problem?
Thanks,
ItchyII