Question : How do I get correct calculations using Dcount in a Report that is based on a filtered table?

I am using a command button on a form to run a report using 3  to 4 parameters to filter the data.  The report has calculated fields that are correct when I use Sum or Count, but erroneous when I use DCount.  It appears that the Sum and Count calcualtions honor the filter by the parameters, but the DCount ignores the filter and treats the table as unfiltered.

Here is the code for the report button in the form

Private Sub cmdWeeklyReport_Click()
Dim stCriteria As String
stCriteria = "[Client]= '" & cboClientFilter & "' And DateValue([VisitDate]) Between " & "#" & DateValue(ActXReportStartDate) & "#" & " And " & "#" & DateValue(ActXReportEndDate) & "#" & ""
    DoCmd.OpenReport "WeeklyReport", acViewPreview, , stCriteria
End Sub

Here is the code used in the calculated (counted) field in the report that includes all records in the table instead of the records that are filtered by the criteria listed above.
=DCount("[SiteID]","VisitTreatmentQuery","[HotLine] = 'Site visited'")

Here is the code that works correctly:
=Sum([VisitTreatmentQuery]![TreatAreaAcre])

Basically, whenever I use Sum or Count, it works.  But when  I use DCount it uses the entire table and ignores the original filter.

Do I need to repeat the original filter in the criteria in DCount?
If so, what is the syntax for that?
Something like
=DCount("[SiteID]","VisitTreatmentQuery","[HotLine] = 'Site visited'") and "[Client]= '" & cboClientFilter & "' And DateValue([VisitDate]) Between " & "#" & DateValue(ActXReportStartDate) & "#" & " And " & "#" & DateValue(ActXReportEndDate) & "#" & ""

thanks for all the help

Pat

Answer : How do I get correct calculations using Dcount in a Report that is based on a filtered table?

Pat,
Did you try my suggestion of using the open report syntax with the filter argument instead of the where clause (strCriteria) argument?

DoCmd.OpenReport "WeeklyReport", acViewPreview, "YourFilterQuery"
                  YourFilterQuery is the name of the query that has the additional criteria (the entire revised query, not just the additional criteria)
Random Solutions  
 
programming4us programming4us