Solved

How do I use multiple criteria inside Access 2007  dcount expression

Posted on 2011-03-09
10
443 Views
Last Modified: 2012-05-11
Experts,

I have a group by query I need some help with.  basically 4 columns:
1.  Week Beginning (converts date to the current monday)
2.  Overall loans reviewed  (count *)
3. Loans with errors (where dcount returns issue = error)
4.  Loans with no error (where dcount returns issue = none)

Column 1 and 2 work as expected.  How do I write this so that dcount only returns the corresponding error count for that weeks record set.
In the attched screen shot (docx), using the 3/7 week example, there should be 1 error and 1 no error.  Currently, the dcount has no additional clause that limits it, and counts all errors and all non errors.  I am not getting the syntax correct and need some help.

sql code is also attached.
ps - is there a better, non excel pivot table way, to do something like this ?

Many thanks!
 
SELECT DateAdd("d",(CInt(Format([Date],"w"))-2)*-1,[Date]) AS [Week Beginning], Count(*) AS [Overall loans Reviewed], DCount("Issue","tbl_loans_Reviewed","[Issue] =" & "'Error'") AS [Loans with Errors], DCount("Issue","tbl_loans_Reviewed","[Issue] =" & "'None' and 
DatePart('ww',[Date])") AS [Loans with no errors], [Loans with no errors]/Count(*) AS [Percent of loans with no Errors], [Loans with errors]/Count(*) AS [Percent of loans with Errors], tbl_loans_reviewed.[Loan Type]
FROM tbl_loans_reviewed
GROUP BY DateAdd("d",(CInt(Format([Date],"w"))-2)*-1,[Date]), tbl_loans_reviewed.[Loan Type], DatePart('ww',[Date]);

Open in new window

ee-screenshot.docx
0
Comment
Question by:MrAutomate
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 3

Expert Comment

by:JAMcDo
ID: 35083412
I've made some changes but since I cannot test the query, I don't know if I caught all the problems.

SELECT DateAdd("d",(CInt(Format([Date],"w"))-2)*-1,[Date]) AS [Week Beginning], Count(*) AS [Overall loans Reviewed], DCount("[Issue]","tbl_loans_Reviewed","[Issue] ='Error'") AS [Loans with Errors], DCount("[Issue]","tbl_loans_Reviewed","[Issue] = 'None' and DatePart('ww',[Date])") AS [Loans with no errors], [Loans with no errors]/Count(*) AS [Percent of loans with no Errors], [Loans with errors]/Count(*) AS [Percent of loans with Errors], tbl_loans_reviewed.[Loan Type] AS Expr1
FROM tbl_loans_reviewed
GROUP BY DateAdd("d",(CInt(Format([Date],"w"))-2)*-1,[Date]), tbl_loans_reviewed.[Loan Type], DatePart('ww',[Date]);

The main thing I thought would be giving the error was the lack of [ . . .] around some items.

Try it and see if it works for you.

J.
0
 

Author Comment

by:MrAutomate
ID: 35083572
JAMcDo:,

Thasnks for the quick response and taking a look.  I astill get the same results.  I have zipped a copy of the db so you can see the objects and test.  

On the 3/7/2011 line I need it to read Loans with Errors 1, loans with no errors 1 (you will see the table for that week has 2 examples, 1 error, and 1 no error.

Thanks again
ee-test.zip
0
 
LVL 57

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 35083668

 FYI, the Domain Functions (DLookup, DCount, etc) should never be used within a SQL expression as they are un-optimizable by the query parsing engine and will yield very poor performance.

 You need to be using a sub select.

  The Domain functions are intended to be used in places where SQL expressions are not allowed, but a VBA expression is.  All the domain functions are really SQL SELECT statements that are encapsulated.

  DCount() is really:

  Select COUNT(<fieldName>) FROM <table name> WHERE <criteria>

  which is what you should be writting in your SQL as a sub select.

JimD.
0
 

Author Comment

by:MrAutomate
ID: 35083722
JimD.  I am unsure what you are saying.  Are you saying I have :

1.  DCount("Issue","[tbl_loans_reviewed]","issue ='none'
Then a seperate  
2. Dcount(DateAdd("d",(CInt(Format([Date],"w"))-2)*-1,[Date])=[qry_rpt_Weekly_temp]![Week Beginning]")

and how do I write the piece to put them togther?

0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 3

Expert Comment

by:JAMcDo
ID: 35083942
I have to get back to work know, so I will come back to it later as time permits - sorry.

JDettman has a good point and it is worth following.  See if you can comvert some of the DCOUNT statments to SELECT statements.

J.
0
 

Author Comment

by:MrAutomate
ID: 35084147
Thanks all.  After my cafine kicked in , I realzed a crosstab query would work, so I have this solved, but I DO want to use this example to learn more.  

Please show me an example of best practice inside of the DB I zipped up, on how to convert dcount to select quesries, using sub selects, etc.  I will award points based on the easiest to follow, as I am not a professional developer, but learn by studying examples .

Thanks again
0
 
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 500 total points
ID: 35084233
<<JimD.  I am unsure what you are saying.  Are you saying I have :>>

  No, you should not be using DCount at all.  Your SQL should look like this:

SELECT DateAdd("d",(CInt(Format([Date],"w"))-2)*-1,[Date]) AS [Week Beginning], Count(*) AS [Overall loans Reviewed], (Select Count([Issue]) From tbl_loans_Reviewed Where [Issue] ='Error') AS [Loans with Errors],

 as an example.

JimD.

0
 
LVL 57
ID: 35084247

  Another way of doing that would be to write a seperate query, save it, then use that as a "table" in the current query and do a join to it.

JimD.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 36032522
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
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
DSum for Access 6 43
Tags from access to excel 3 26
Cross Tab with two column values 7 32
VBA code won't run Delete Query 5 0
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

932 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

19 Experts available now in Live!

Get 1:1 Help Now