Results 1 to 7 of 7
  1. #1
    manics31 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Location
    Philadelphia
    Posts
    23

    Date Criteria Question

    Hello All,
    Is there a way to put a criteria on a query to pulls all data for current year plus 3 months of last year without using a form to pass the parameter to the query?
    for example pull all records of 2018 up until now and also pull the record from 2017 from October to Dec.
    is something like this achievable?

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    yes, something like


    Code:
    SELECT *
    FROM myTable
    WHERE year(datefield)=year(date()) OR (year(datefield)=year(date())-1 AND month(datefield)>=10)

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I would be more likely to use something like:

    WHERE DateField Between Date() - 365 And Date()

    or using formulas to get the first day of the month (or your specific requirement). Using the Year() function on the date field forces it to be applied to every record in the table, and the engine can't use an index on the date field. In smaller tables you won't notice the difference, but you could on larger tables.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    Minty is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,001
    To add to Paul's suggestion, and hopefully just as efficient? as it uses the DatePart function on the current date rather than your fieldvalue.

    WHERE DateField Between DateAdd("m",-3,(DateSerial(DatePart("yyyy",(Date())),1,1))) And Date()
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    Join Date
    Apr 2017
    Posts
    1,673
    Another one:
    Code:
    ... WHERE datefield >= DateSerial(Year(datefield)-1, 10,1)

  6. #6
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,397
    and another

    datefield>dateadd("m",-month(date())-2,date()-day(date()))

  7. #7
    manics31 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2016
    Location
    Philadelphia
    Posts
    23
    Hey everyone, thank you so much for the help, i learn something new everyday. thanks a million everyone.

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

Similar Threads

  1. Replies: 5
    Last Post: 06-13-2015, 04:29 PM
  2. Replies: 2
    Last Post: 04-02-2015, 12:45 PM
  3. Date criteria using between and form date picker
    By killermonkey in forum Queries
    Replies: 3
    Last Post: 03-21-2013, 12:44 PM
  4. Replies: 3
    Last Post: 08-21-2012, 03:05 PM
  5. Question About Criteria
    By Kipster1203 in forum Queries
    Replies: 2
    Last Post: 05-20-2010, 12:09 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