Solved

DCount in Access

Posted on 2011-02-18
6
569 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
Comment Utility
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

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
<<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
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 59

Expert Comment

by:Kevin Cross
Comment Utility
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
Comment Utility
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
Comment Utility
That makes sense.  Thank you!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

762 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

7 Experts available now in Live!

Get 1:1 Help Now