Results 1 to 10 of 10
  1. #1
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305

    Criteria to filter down to YTD across multiple years?

    So I've got a query that is full of customer complaints from 2018 up to today. I'm interested to see if there's a way to code criteria that filters down to nothing beyond today's date across all previous years. So that essentially it only shows records for 1.1.2018 - 6.24.2018, 1.1.2019 - 6.24.2019, 1.1.2020 - 6.24.2020, and 1.1.2021 - present....if that makes sense.



    My query is fairly simple but I can attach an example if needed. The fields are 'submissiondate' 'customername' and 'category'

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I presume that today's date is not fixed and that tomorrow it will be 06/25?
    You could use a combination of Date() as the current date and DateSerial(Year(Date()),1,1) or it might be DateSerial(Year(Date),1,1). Your query could use the Between operator as well: Between DATE() And DateSerial(Year(Date()),1,1)
    I've never really learned why it's sometimes Date() and other time, just Date.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    I would build the criteria for each year using Microns solution and DateAdd() subtracting years from date() until 2018 or whenever?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  4. #4
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I'm not sure if you want complaints between 1/1 and the current date or you want complaints for just 6 months. (1 - 6 or 7 - 12)

    Given the data set below
    submissiondate
    1/4/2018
    5/22/2020
    1/4/2020
    11/5/2020
    3/20/2019
    4/26/2018
    7/18/2021
    6/22/2021
    6/29/2021
    6/29/2019
    (10 records)

    the query
    Code:
    SELECT tblComplaints.Category, tblComplaints.customername, tblComplaints.submissiondate
    FROM tblComplaints
    WHERE (((tblComplaints.submissiondate)<=Date()) AND ((Month([submissiondate]))>=1 And (Month([submissiondate]))<=Month(Date())));
    returns these dates
    submissiondate
    1/4/2018
    5/22/2020
    1/4/2020
    3/20/2019
    4/26/2018
    6/22/2021
    6/29/2019
    (7 records - should be 6 records)

    where the current date is 6/24/2021



    I haven't figured out how to not include the date #6/29/2019#.

  5. #5
    templeowls is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Feb 2019
    Posts
    305
    Hi Steve, I used your code and worked, but similar to you, I'm having entries from 2019 and 2020 show up to the 30th rather than the 25th (today).

    It's a small enough glitch where its usable still, but its weird nonetheless lol

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Include the day check with the month check?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @Welshgasman,
    I tried that (if I did it right), but I looked to me that dates that should have been included weren't.

    Quote Originally Posted by templeowls View Post
    So that essentially it only shows records for 1.1.2018 - 6.24.2018, 1.1.2019 - 6.24.2019, 1.1.2020 - 6.24.2020, and 1.1.2021 - present....if that makes sense.
    There wasn't an expected result set posted, so not exactly sure what fits.


    If today is 6/24/2021, shouldn't 4/26/2018 be included in result set?

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Not easy I admit. All the brackets throw me as well.
    I think I'd have to build the criteria in a function and perhaps use DateValue()
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    create a table to store your year starts

    tblYears
    Yr
    1/1/2018
    1/1/2019
    etc


    then your query

    Code:
    SELECT Yr, Count(submissionDate) as NumComplaints
    FROM tblComplaints, tblYears
    WHERE submissionDate between Yr and dateadd("yyyy",year(Yr)-year(Date()),Date())
    GROUP BY Yr
    add additional grouping as required

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,932
    I haven't figured out how to not include the date #6/29/2019#
    you need to include a criteria for day as well as year/month

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

Similar Threads

  1. Filter form by multiple combo box criteria
    By whamilton in forum Access
    Replies: 3
    Last Post: 06-18-2021, 12:46 PM
  2. Replies: 9
    Last Post: 05-13-2014, 08:22 AM
  3. Multiple Form Filter Criteria vba code
    By Moonman in forum Programming
    Replies: 6
    Last Post: 11-16-2013, 12:42 AM
  4. Replies: 1
    Last Post: 12-04-2011, 06:33 PM
  5. Replies: 1
    Last Post: 02-03-2010, 08:17 AM

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