Link to home
Start Free TrialLog in
Avatar of MRG_AL
MRG_AL

asked on

DCount in Access

I have a query that calculates a distinct count of a date field in a continuous subform on a form but I'm wondering if I create an unbound field on the parent form if I can create the distinct count within that field. Is there a way to do this? This is what I currently have although it doesn't work. Thank you.


Me.Text2= DCount((DLookup("Field1", "table2","[criteria] = form![criteria])), "table2", Me.Year= "2011")
Avatar of VTKegan
VTKegan
Flag of United States of America image

Your syntax is a little wrong, but the idea is ok.

what is Me.Year?  you are storing the year by itself on the form?  You shouldn't hardcode dates in your code like this.  Because next year it will be no good if you need it to say 2012.

If you can provide actual field and tables names we could help you construct the actual syntax
<<I have a query that calculates a distinct count of a date field in a continuous subform on a form but I'm wondering if I create an unbound field on the parent form if I can create the distinct count within that field.>>

  Yes you can, but a simpler method is to refer to the control in the subform that contains the count:

=Me.<mySubFormControlName>.Form.<mySubFormControlNameThatHasTheCount>

  as far as the Dcount(), you need to pass the value of what is in a control:

Me.Text2= DCount( (DLookup("Field1", "table2","[criteria] = '" & Me.[criteria]) & "'"), "table2", "[Year] = 2011")

  This assumes [criteria] is a text field.

JimD.
Avatar of MRG_AL
MRG_AL

ASKER

This is the actual.
I want the count to be all distinct dates entered into the "Flu" field (which is a continuous date field because the user has to be able to enter as many as possible). In addition to that the count must only count dates where the measure = CIS and is within 2 years of the DOB. I haven't started working on the DOB part.

Me.Text256 = DCount((DLookup("Flu", "Members","[MemberID] = form![MemberID])), "Members2", Me.Measure = "CIS")

This is the query I have that runs and calculates the correct count. Although I'm trying to see if I can calculate the samething without the query.

SELECT x.MemberID, Count(x.Flu) AS Flu
FROM (SELECT DISTINCT MemberID, Flu FROM Members WHERE Measure="CIS" And Flu>DOB And Flu<DateAdd("yyyy",2,DOB))  AS x
GROUP BY x.MemberID;
See Patrick's Article, Calculating Distinct Counts in Access, here as it explains well getting distinct counts in Access as well as provides a very handy modification to DCount called DCountDistinct that should do exactly what you are after.
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MRG_AL

ASKER

That makes sense.  Thank you!