• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 563
  • Last Modified:

Criteria not working with [Calculated_Values] in Access query

So I make a query, and in it I have a formula:

Small_Pets:  [Total_Pets] - [Dogs]

Then I use that field later with another formula:

Cats: [Small_Pets] - [Fish]

And all of that works great.  Query runs fast and all numbers show up correctly.  However...  Once I try to assign some criteria to the second formula everything goes to crap.

Cats:  [Small_Pets]-[Fish]
Criteria:  >3

And when I run the query now a popup window asks me to input what [Small_Pets] is?

Undaunted I tried something different.  Maybe if I had a text and used a different column it would work?  So I tried this:

Is_Crazy:  IIF([Cats]>3,"CRAZY CAT LADY!","Totally normal person")
Criteria:  "CRAZY CAT LADY"

It still didn't work, it's still asking me what [Small_pets] is, even though it works normally without a criteria.

Heroically I pushed on.  I thought that since this is going into a report I would use an imbedded query in the report that feeds off the first query I made.  The only reason I'd need the imbedded query would be to add this tricky criteria in such a way that [Small_Pets] would be contained in the first query, and thus it couldn't ask me for it again!  Brilliant, I know.  

[*.Pet_Calculator] goes into imbedded query.

[Cats.Pet_Calculator] goes into a column
Criteria:  >3

And when I run that it crashes my access...

So I give up.  What painfully obvious simple thing am I missing here?  Why won't this work?
1 Solution
Helen FeddemaCommented:
Can you post the database?  Also, if you have a Cats field, the name will conflict with a calculated field called Cats: [Small_Pets] - [Fish].

Also, it might be best to have a Total_Pets calculated field that adds up the values for each type of pet, since you seem to have those values stored separately.
BugHuggerNoobAuthor Commented:
I don't have a Cats field that's separate.

What I'm actually doing is calculating a countdown.  So I'm using a formula to tell me how many days I have left.  So the fields I'm using are [DateCollected] and [DaysForTesting].  So I'm calculating how many days we've had the sample:

Countdown:  DateDiff("d",[DateCollected],now())

And subtracting it from how many days we have to test it:


And it keeps asking me what [Countdown] is - but only when I put a criteria in there.  It works perfectly so long as I don't tell it <5
Jeffrey CoachmanCommented:
Make your "DaysLeft" field something like this:

DaysLeft: [DaysForTesting]-DateDiff("d",[DateCollected],Now())

This worked fine for me...

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

BugHuggerNoobAuthor Commented:
Did that new formula without a criteria, it ran great in less than a second returning a few dozen records.

Tried adding <5 as a criteria, and now it's an endless hourglass...  Not sure why Access hates criteria so much?
Jeffrey CoachmanCommented:
FWIW, ...this used to work in older versions of Access because Access would automatically insert the full calculated field in the new expression...
Now it seems that Access just uses the calc field name...

Other notes:
You could have probably done the calculation in the report controls, ...then my guess is that something like this would have worked for the controlsource for txtDaysLeft:

Jeffrey CoachmanCommented:
<5 works just as well for me (no delay at all):
Make sure you run the compact repair utility on your database first
Try this instead of the DateDiff function:

DaysLeft: [DaysforTesting]- (Now() - [DateCollected])
Criteria:  < 5

Also, roughly how many records are we talking about?

Does your query have other functions/criteria that could be contributing to the slow down?

Are you testing your query directly through the query grid or through a form or report which might have additional criteria or grouping?
BugHuggerNoobAuthor Commented:
DaysLeft: [DaysforTesting]- (Now() - [DateCollected])
 Criteria:  < 5

That worked perfectly!  Thanks, mbizup, you're the best!

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now