R
Robert_DubYa
I have a multi select list box that I am trying to use in conjunction with a
query. I have borrowed some code and modified (courtesy of Allen Brown) that
grabs all selected data in the list box (I will paste the code and query
below). I want to pass this data to a query. I am doing so via a module.
My problem is the query (query and module below as well) will run fine if I
select just one item from my listbox, but NO results if I select more than
one item in the list (I know the data is there). I'm not sure where I have
gone wrong, but when I test with a message box all appears fine. I have two
diffrent variables I can pass to the module (I did this for testing) one with
and one without quotes for my "IN" criteria. The query works fine if I use
an "In" statement with the values typed in (not passed from the module) Any
help will be greatly apperciated.
Code from form:
Private Sub Command3_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strDepartmentsNoQuotes As String 'String to use as WhereCondition
Dim strDepartmentsWithQuotes As String 'String to use as WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
'Loop through the ItemsSelected in the list box.
With Me.lstDept
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strDepartmentsNoQuotes = strDepartmentsNoQuotes & strDelim &
..ItemData(varItem) & strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDepartmentsWithQuotes = strDepartmentsWithQuotes & """" &
..Column(0, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strDepartmentsNoQuotes) - 1
If lngLen > 0 Then
strDepartmentsNoQuotes = Left$(strDepartmentsNoQuotes, lngLen)
lngLen = Len(strDepartmentsWithQuotes) - 2
If lngLen > 0 Then
strDepartmentsWithQuotes = Left$(strDepartmentsWithQuotes, lngLen)
End If
End If
strDepartments = strDepartmentsNoQuotes
MsgBox strDepartments
DoCmd.OpenQuery "qryStep4-ByDept"
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"cmdPreview_Click"
End If
Resume Exit_Handler
End Sub
MODULE:
Option Compare Database
Public strDepartments As String
Function Departments() As String
Departments = strDepartments
MsgBox Departments
End Function
SQL from Query:
SELECT [tblStep1-HoursQtys].[Employee Name], Trim([tblStep1-HoursQtys]!Dept)
AS Dept, Trim([Business Unit]) AS [Work Cent], [tblStep1-HoursQtys].[Order
Numb], [tblStep1-HoursQtys].[Op Seq], [tblStep1-HoursQtys].[Gl Date],
[tblStep1-HoursQtys].[Shift Code], [tblStep1-HoursQtys].[Qty Completed],
[Hours Worked]/([Total Alot Time]/[Wo Qty]) AS [Standard Completions],
[tblStep1-HoursQtys].[Hours Worked], [Qty Completed]*([Total Alot Time]/[Wo
Qty]) AS [Earned Hours], [Earned Hours]/[Hours Worked] AS Realization
FROM ([tblStep1-HoursQtys] INNER JOIN [tblStep2-WoQty] ON
([tblStep1-HoursQtys].[Order Numb] = [tblStep2-WoQty].[Order Numb]) AND
([tblStep1-HoursQtys].[Order Type] = [tblStep2-WoQty].[Order Type])) INNER
JOIN tblStep3GetOpSeqTime ON ([tblStep1-HoursQtys].[Batch Numb] =
tblStep3GetOpSeqTime.[Batch Numb]) AND ([tblStep1-HoursQtys].[Order Numb] =
tblStep3GetOpSeqTime.[Order Numb]) AND ([tblStep1-HoursQtys].[Order Type] =
tblStep3GetOpSeqTime.[Order Type]) AND ([tblStep1-HoursQtys].[Op Seq] =
tblStep3GetOpSeqTime.[Op Seq])
WHERE (((Trim([tblStep1-HoursQtys]![Dept]))=Departments()));
query. I have borrowed some code and modified (courtesy of Allen Brown) that
grabs all selected data in the list box (I will paste the code and query
below). I want to pass this data to a query. I am doing so via a module.
My problem is the query (query and module below as well) will run fine if I
select just one item from my listbox, but NO results if I select more than
one item in the list (I know the data is there). I'm not sure where I have
gone wrong, but when I test with a message box all appears fine. I have two
diffrent variables I can pass to the module (I did this for testing) one with
and one without quotes for my "IN" criteria. The query works fine if I use
an "In" statement with the values typed in (not passed from the module) Any
help will be greatly apperciated.
Code from form:
Private Sub Command3_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
'Author: Allen J Browne, 2004. http://allenbrowne.com
Dim varItem As Variant 'Selected items
Dim strDepartmentsNoQuotes As String 'String to use as WhereCondition
Dim strDepartmentsWithQuotes As String 'String to use as WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
'Loop through the ItemsSelected in the list box.
With Me.lstDept
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strDepartmentsNoQuotes = strDepartmentsNoQuotes & strDelim &
..ItemData(varItem) & strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDepartmentsWithQuotes = strDepartmentsWithQuotes & """" &
..Column(0, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strDepartmentsNoQuotes) - 1
If lngLen > 0 Then
strDepartmentsNoQuotes = Left$(strDepartmentsNoQuotes, lngLen)
lngLen = Len(strDepartmentsWithQuotes) - 2
If lngLen > 0 Then
strDepartmentsWithQuotes = Left$(strDepartmentsWithQuotes, lngLen)
End If
End If
strDepartments = strDepartmentsNoQuotes
MsgBox strDepartments
DoCmd.OpenQuery "qryStep4-ByDept"
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Report cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, ,
"cmdPreview_Click"
End If
Resume Exit_Handler
End Sub
MODULE:
Option Compare Database
Public strDepartments As String
Function Departments() As String
Departments = strDepartments
MsgBox Departments
End Function
SQL from Query:
SELECT [tblStep1-HoursQtys].[Employee Name], Trim([tblStep1-HoursQtys]!Dept)
AS Dept, Trim([Business Unit]) AS [Work Cent], [tblStep1-HoursQtys].[Order
Numb], [tblStep1-HoursQtys].[Op Seq], [tblStep1-HoursQtys].[Gl Date],
[tblStep1-HoursQtys].[Shift Code], [tblStep1-HoursQtys].[Qty Completed],
[Hours Worked]/([Total Alot Time]/[Wo Qty]) AS [Standard Completions],
[tblStep1-HoursQtys].[Hours Worked], [Qty Completed]*([Total Alot Time]/[Wo
Qty]) AS [Earned Hours], [Earned Hours]/[Hours Worked] AS Realization
FROM ([tblStep1-HoursQtys] INNER JOIN [tblStep2-WoQty] ON
([tblStep1-HoursQtys].[Order Numb] = [tblStep2-WoQty].[Order Numb]) AND
([tblStep1-HoursQtys].[Order Type] = [tblStep2-WoQty].[Order Type])) INNER
JOIN tblStep3GetOpSeqTime ON ([tblStep1-HoursQtys].[Batch Numb] =
tblStep3GetOpSeqTime.[Batch Numb]) AND ([tblStep1-HoursQtys].[Order Numb] =
tblStep3GetOpSeqTime.[Order Numb]) AND ([tblStep1-HoursQtys].[Order Type] =
tblStep3GetOpSeqTime.[Order Type]) AND ([tblStep1-HoursQtys].[Op Seq] =
tblStep3GetOpSeqTime.[Op Seq])
WHERE (((Trim([tblStep1-HoursQtys]![Dept]))=Departments()));