Results 1 to 5 of 5
  1. #1
    KoolBreeze is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2018
    Posts
    3

    Records from last month

    I'm trying to write a dynamic query to retrieve records entered into a table during the previous month, e.g. between 2/1/2018 and 2/28/2018. I'm having a little trouble getting the datediff and dateadd functions to work as expected.

    Code:
    select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE())-1, 0)
    select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE())-1, -1)
    The above statements return the first day and last day of the previous month respectively, 2/1/2018 and 2/28/2018, in SQL Server.

    Code:
    select DATEADD("M", DATEDIFF("M", 0, DATE())-1, 0)
    select DATEADD("M", DATEDIFF("M", -1, DATE())-1, -1)
    So, why don't the above statements return do the same in Access 2010? Instead both statements return 2/28/2018.



    Thanks for the help.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    make a form,
    on the form, put 4 text boxes, txtStart, txtEnd, txtPrevStart, txtPrevEnd
    when user sets the txtStart, the formula for txtPrevStart is =DateAdd("m",-1,txtStart)
    txtPrevEnd is =DateAdd("m",-1,txtEnd)

    then some queries can run current month:
    select * from table where [date] between forms!myForm!txtSTart and forms!myForm!txtEnd

    and others the previous
    select * from table where [date] between forms!myForm!txtPrevStart and forms!myForm!txtPrevEnd

    you can even make quick picks to set the start end date boxes ....
    Click image for larger version. 

Name:	rpts ytd.jpg 
Views:	15 
Size:	45.5 KB 
ID:	32898

  3. #3
    KoolBreeze is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2018
    Posts
    3
    Not really looking for anything that involved. I have a simple report that for the records to be listed on and just want to link the report to a query that I don't have to manually change the dates on to get the previous month's entries. So in the end, all the user has to do is click report and last month's records are displayed.

  4. #4
    KoolBreeze is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Mar 2018
    Posts
    3
    I found another way shortly after posting:

    Code:
    SELECT EntryDate
    FROM tRecords
    WHERE EntryDate >= DateSerial(Year(Date()), Month(Date()) - 1, 1) AND EntryDate <= DateSerial(Year(Date()), Month(Date()), 0)
    Still not sure why the other way works on SQL Server but not Access though.

  5. #5
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

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

Similar Threads

  1. Auto-Generate new records per month
    By gottnoskill in forum Programming
    Replies: 10
    Last Post: 03-12-2014, 08:16 AM
  2. search records by month and year
    By nurul in forum Forms
    Replies: 8
    Last Post: 12-09-2012, 09:34 PM
  3. show records in this month last year?
    By geoffcox in forum Queries
    Replies: 4
    Last Post: 06-11-2011, 07:12 AM
  4. Replies: 3
    Last Post: 12-23-2009, 08:50 AM
  5. Replies: 0
    Last Post: 08-04-2009, 08:51 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