Solved

DCount in Access

Posted on 2011-02-18
6
570 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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sub Reports 8 22
Getting Run-Time Error 13 - Type Mismatch 3 27
Error: Operation must use an updateable query 2 10
format date field on certain entries 8 29
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now