Results 1 to 8 of 8
  1. #1
    bryan0 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Connecticut
    Posts
    19

    Query Expression with IIF and DLOOKUP

    I need to be able to set the criteria of one field of a query based on a table value. I have a table called CRITERIA, with a text field called DATE_CRITERIA. There is one record, with a value of 2013Q3. I will use a form to set this value to other strings.



    In my query, I have this in as one of the fields:
    PERIOD_1: "SUM(IIF([QUARTER]='" & DLookUp("[DATE_CRITERIA]","CRITERIA") & "',[AMOUNT],0))"

    When I run the query, I don't get an error message, but my "PERIOD_1" column just has SUM(IIF([QUARTER]='2013Q3',[AMOUNT],0)) filled the whole way down.

    If I enter it like this (without the Dlookup), there's no problem:
    PERIOD_1: Sum(IIf([QUARTER]="2013Q3",[AMOUNT],0))

    Can someone please let me know what I'm doing wrong? Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    The quotes around the whole thing make it literal text. Seeing that as the result was the giveaway.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    That DLookup will return value from first record. Is that what you want?

    PERIOD_1: SUM(IIf([QUARTER]=DLookUp("[DATE_CRITERIA]", "CRITERIA"), [AMOUNT], 0)
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    bryan0 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Connecticut
    Posts
    19
    I see what you're saying... However when I ran it like this, I got $0.00 all the way down the PERIOD_1 column, and there should be positive dollar amounts there. Does anything look wrong now?

    Before:
    PERIOD_1: "SUM(IIF([QUARTER]='" & DLookUp("[DATE_CRITERIA]","CRITERIA") & "',[AMOUNT],0))"

    Now:
    PERIOD_1: Sum(IIf([QUARTER]='" & DLookUp("[DATE_CRITERIA]","CRITERIA") & "',[AMOUNT],0))

  5. #5
    bryan0 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Connecticut
    Posts
    19
    Quote Originally Posted by June7 View Post
    That DLookup will return value from first record. Is that what you want?

    PERIOD_1: SUM(IIf([QUARTER]=DLookUp("[DATE_CRITERIA]", "CRITERIA"), [AMOUNT], 0)
    YES!!! Adding the equals symbol did it. Thank you!

  6. #6
    bryan0 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Connecticut
    Posts
    19
    One more question with the same idea. I tried entering multiple criteria in my table, but it just returns zeros. I tried to build off of the IIF statement, so if the QUARTER is 2013Q3 OR 2013Q4 (which would add 2013Q3 and 2013Q4). Here's what I put in the table:
    2013Q3' Or [QUARTER]='2013Q4

    And here's what's in the query expression:
    PERIOD_1: SUM(IIf([QUARTER]=DLookUp("[DATE_CRITERIA]", "CRITERIA"), [AMOUNT], 0)

    I've tried everything I can think of. Do you know if this is possible to do as well? Thanks again.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    I don't understand what you mean by 'put in the table' - put where in the table?

    The DLookup still doesn't make sense to me. It will just return value from first record in table Criteria.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    bryan0 is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Connecticut
    Posts
    19
    I am trying to build off of a solution that you helped me find last week, here is the thread (I was using countries at the time, but have implemented it for something else):
    https://www.accessforums.net/queries...ery-41006.html

    This is the original expression, which works perfectly:
    PREVIOUS_YTD: Sum(IIf([QUARTER]="2012Q1" Or [QUARTER]="2012Q2" Or [QUARTER]="2012Q3",[AMOUNT],0))

    At the end of Q3, we want to compare all of the current year, versus the first 3 quarters of the previous year (which is what the above expression accomplishes). If I can have the expression reference the contents of a table, I can maintain that table with a simple userform, which will in turn set the query expression. This way we won't need to go into the query expression and update it every quarter.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Query Expression Help
    By derek7467 in forum Queries
    Replies: 1
    Last Post: 08-02-2013, 01:12 PM
  2. Replies: 2
    Last Post: 11-20-2012, 03:21 AM
  3. Replies: 4
    Last Post: 10-26-2012, 12:49 AM
  4. Expression for Query Help
    By fikeplay in forum Access
    Replies: 1
    Last Post: 07-09-2011, 05:12 PM
  5. Dlookup Expression Help
    By chrismja in forum Queries
    Replies: 10
    Last Post: 10-29-2010, 03:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums