Solved

DCount in Access

Posted on 2011-02-18
6
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 60

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

732 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