I am trying to do something very similar to this and I am getting an error
message that I have not been able to resolve. I have referenced the code in
this discussion as well as code that I was directed to in another post. It
was entitled "Use a multi-select list box to filter a report" and was
provided by allen Browne.
I have created a form with a select box that lists open issues using this
code for the list box
SELECT [Issues].[ID], [Issues].[Title], [Issues].[Status] FROM Issues WHERE
((([Issues].[Status])="open")) ORDER BY [Issues].[Title];
This appears to work fine. I then created a button that has the filtering
code in it.
If I do not select anything it will run the report with all records.
However, if I pick any records I get this error message.
Error 3071 – This expression is type incorrectly, or it is too complex to be
evaluated. For example, a numeric expression may contain too many complicated
elements. Try simplifying the expression by assigning parts of the expression
to variables.
Here is the code that I am using.
Private Sub cmdPreview_Click()
On Error GoTo Err_Handler
'Purpose: Open the report filtered to the items selected in the list box.
Dim varItem As Variant 'Selected items
Dim strWhere As String 'String to use as WhereCondition
Dim strDescrip As String 'Description of WhereCondition
Dim lngLen As Long 'Length of string
Dim strDelim As String 'Delimiter for this field type.
Dim strDoc As String 'Name of report to open.
strDelim = """" 'Delimiter appropriate to field type. See
note 1.
strDoc = "Details of Multiple Open Issues"
'Loop through the ItemsSelected in the list box.
With Me.lstCategory
For Each varItem In .ItemsSelected
If Not IsNull(varItem) Then
'Build up the filter from the bound column (hidden).
strWhere = strWhere & strDelim & .ItemData(varItem) &
strDelim & ","
'Build up the description from the text in the visible
column. See note 2.
strDescrip = strDescrip & """" & .Column(1, varItem) & """, "
End If
Next
End With
'Remove trailing comma. Add field name, IN operator, and brackets.
lngLen = Len(strWhere) - 1
If lngLen > 0 Then
strWhere = "[ID] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Issues: " & Left$(strDescrip, lngLen)
End If
End If
'Report will not filter if open, so close it. For Access 97, see note 3.
If CurrentProject.AllReports(strDoc).IsLoaded Then
DoCmd.Close acReport, strDoc
End If
'Omit the last argument for Access 2000 and earlier. See note 4.
DoCmd.OpenReport strDoc, acViewPreview, WhereCondition:=strWhere
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
----------------
The strDelim is a text field.
Can you see where I am going wrong. I'm new to VB so I am way over my head.
thanks for any help you can offer.