Criteria not working with [Calculated_Values] in Access query

Posted on 2014-08-05
Last Modified: 2014-08-06
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?
Question by:BugHugger
    LVL 31

    Expert Comment

    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.

    Author Comment

    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
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    Make your "DaysLeft" field something like this:

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

    This worked fine for me...

    Author Comment

    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?
    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    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:

    LVL 74

    Expert Comment

    by:Jeffrey Coachman
    <5 works just as well for me (no delay at all):
    Make sure you run the compact repair utility on your database first
    LVL 61

    Accepted Solution

    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?

    Author Closing Comment

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

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

    Featured Post

    Gigs: Get Your Project Delivered by an Expert

    Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

    Join & Write a Comment

    Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
    In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
    Familiarize people with the process of utilizing SQL Server views 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 Access…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    754 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

    22 Experts available now in Live!

    Get 1:1 Help Now