Results 1 to 10 of 10
  1. #1
    Chris_Cline is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    7

    Question Strange behavior of Dateadd function, drops the 31st day of a month

    I have a query that looks for any invoices billed within a specific month in the previous year and also looks for any invoices billed during that same month in the current year.

    The field [EffectiveDate] is the due date of an invoice.
    Using a criteria as follows:
    Between DateAdd("yyyy",-1,[Beginning Date]) And DateAdd("m",-11,[Beginning Date]-1) Or Between [Beginning Date] And DateAdd("m",1,[Beginning Date]-1)

    Here is where it gets strange.
    For example if you enter the parameter of [Beginning Date] as "6/1/21" it will return any invoices with an effective date between 6/1/20 and 6/30/20 or 6/1/21 and 6/30/21. Which is what I intended.


    However, any months that have 31days do not give the same results. For some reason the 31st day is being dropped off and only invoices with an effective date between the first and the thirtieth days are returned.

    Can anybody explain why this is happening?

    Thank you in advance.

  2. #2
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Have you tried dateserial() ?
    Use the 0 day of the next month to get the last day of any month.

    Not sure it will work but it's the first thing that came to mind.

    something like (air code)

    Code:
    Between DateSerial(Year([BeginningDate])-1,month([Beginning Date]),1) And DateSerial(Year([BeginningDate]),month([Beginning Date]+1),0)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  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,521
    I would use this to get the last day of a month:

    DateSerial(Year(dte), Month(dte) + 1, 0)

    Given July:

    ?DateSerial(Year(#7/1/21#), Month(#7/1/21#) + 1, 0)
    7/31/2021

    Your method subtracts a day from 7/1 which is 6/30 and adds a month to get 7/30:

    ?DateAdd("m",1,#7/1/21#-1)
    7/30/2021
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Oops, my slow typing and copy/pasting rears its ugly head.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,654
    Quote Originally Posted by pbaldy View Post
    Oops, my slow typing and copy/pasting rears its ugly head.
    so did my edit
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    Chris_Cline is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    7
    Not what I was looking for. This returns everything from a starting point one year before the effective date till the day before the effective date.

    What has me puzzled is that it only drops off the last day of the month for months that have 31 days months with 30 days work as expected. It does get me closer as the 31st days are coming up.

    Thank you

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Did you see "Your method subtracts a day from 7/1 which is 6/30 and adds a month to get 7/30"? Using DateSerial() will solve your problem.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,940
    See if this enlightens you?

    Code:
    Sub TestBS()
    Dim BeginningDate As Date
    
    BeginningDate = #6/1/2020#
    Debug.Print "DateAdd minus 1 year " & CStr(DateAdd("yyyy", -1, [BeginningDate]))
    Debug.Print "DateAdd minus 11 months " & CStr(DateAdd("m", -11, [BeginningDate] - 1))
    Debug.Print "Date - 1 " & BeginningDate - 1
    Debug.Print "DateAdd 1 month less day " & CStr(DateAdd("m", 1, [BeginningDate] - 1))
    
    BeginningDate = #7/1/2020#
    Debug.Print "DateAdd minus 1 year " & CStr(DateAdd("yyyy", -1, [BeginningDate]))
    Debug.Print "DateAdd minus 11 months " & CStr(DateAdd("m", -11, [BeginningDate] - 1))
    Debug.Print "Date -1 " & BeginningDate - 1
    Debug.Print "DateAdd 1 month less day " & CStr(DateAdd("m", 1, [BeginningDate] - 1))
    
    End Sub
    Result
    Code:
    DateAdd minus 1 year 01/06/2019
    DateAdd minus 11 months 30/06/2019
    Date - 1 31/05/2020
    DateAdd 1 month less day 30/06/2020 You cannot have 31/06/20 ?
    DateAdd minus 1 year 01/07/2019
    DateAdd minus 11 months 30/07/2019
    Date -1 30/06/2020
    DateAdd 1 month less day 30/07/2020
    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
    Chris_Cline is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    7
    Got it solved

    Between
    DateSerial(Year([Beginning Date])-1,Month([Beginning Date]),1)
    And
    DateSerial(Year([Beginning Date])-1,Month([Beginning Date])+1,0)
    Or
    Between
    [Beginning Date]
    And
    DateSerial(Year([Beginning Date]),Month([Beginning Date])+1,0)

  10. #10
    Chris_Cline is offline Novice
    Windows 10 Access 2016
    Join Date
    Jan 2021
    Posts
    7
    Thank you for showing me what the math was really doing. Resolved using the Dateadd function

    Between
    DateAdd("yyyy",-1,[Beginning Date]) And DateAdd("d",-1,(DateAdd("m",-11,[Beginning Date])))
    Or Between
    [Beginning Date] And DateAdd("d",-1,(DateAdd("m",1,[Beginning Date])))

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

Similar Threads

  1. Strange Behavior
    By Dave14867 in forum Forms
    Replies: 3
    Last Post: 06-16-2020, 03:52 PM
  2. Combo box strange behavior
    By Goyooper in forum Database Design
    Replies: 5
    Last Post: 06-02-2020, 12:40 PM
  3. Query not listing the 31st of the month
    By schulzy175 in forum Queries
    Replies: 12
    Last Post: 11-21-2017, 01:33 PM
  4. Strange TransferSpreadsheet Behavior
    By JoeM in forum Programming
    Replies: 6
    Last Post: 08-05-2015, 07:47 AM
  5. Strange Behavior when Sorting
    By geniass in forum Queries
    Replies: 5
    Last Post: 09-02-2010, 03:53 PM

Tags for this Thread

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