Hi June 7,
Thanks for the quick response, however, it didn't get me the result I need, just "#Error". I think it has to do with when the DCount function is executed vs. when the query takes the parameter from the list box, but I'm just speculating here. I'm using Allen Browne's "Open the report filtered to the items selected in the list box" code below. to filter the report, and I want to count the records using the criteria from the DCount only after the report has been filtered. Am I over complicating? Is there a simpler way to do this? Thanks much.
Code:
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 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 FORM (report) to open.
strDelim = """" 'Delimiter appropriate to field type. See note 1.
strDoc = "fProject"
'Loop through the ItemsSelected in the list box.
With Me.lstProject
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 = "[Project] IN (" & Left$(strWhere, lngLen) & ")"
lngLen = Len(strDescrip) - 2
If lngLen > 0 Then
strDescrip = "Categories: " & Left$(strDescrip, lngLen)
End If
End If
DoCmd.OpenForm strDoc, acNormal, WhereCondition:=strWhere
Exit_Handler:
Exit Sub
Err_Handler:
If Err.Number <> 2501 Then 'Ignore "Form cancelled" error.
MsgBox "Error " & Err.Number & " - " & Err.Description, , "cmdOpenfProject_Click"
End If
Resume Exit_Handler
End Sub