Solved

DCount in Access

Posted on 2011-02-18
6
573 Views
Last Modified: 2012-05-11
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")
0
Comment
Question by:MRG_AL
6 Comments
 
LVL 10

Expert Comment

by:VTKegan
ID: 34926047
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
0
 
LVL 57
ID: 34926067
<<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.
0
 

Author Comment

by:MRG_AL
ID: 34926172
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;
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 34926557
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.
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 34926837
A DCount() or Dlookup can use a table or query as the input "table"

So you need a saved query that simply returns the correct count for all members.  Then your form can do a DLookup() like this:

 =Dlookup("[<name of field with count>]", "<name of saved query that returns the count>","[MemberID] = " & Me.MemberID)

  I can be more specific on the save SQL that's required if you outline the design of the members table.

  Dpending on what your doing in the form, you might also want to take that saved query and add it to the forms underlying recordsource rather then doing a DLookup (all the domain functions are just another way of executing some SQL and you only need to use them where an expression is allowed, but a SQL statement is not).

JimD.
0
 

Author Closing Comment

by:MRG_AL
ID: 34927490
That makes sense.  Thank you!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question