Results 1 to 9 of 9
  1. #1
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64

    How to calculate a due date to fall on the same day of the year

    Hello folks, here's my problem: I need to calculate the due date of something when a form opens. This due date is based on a previous date and a frequency interval, the latter two are stored in a table. The form has the following fields/controls: a textbox called MyLastDate, a combobox called MyFrequencyCombo and another textbox called MyDueDate.



    The DateAdd function is currently performing this calculation and it works as expected:

    Code:
       MyDueDate = DateAdd("yyyy", MyFrequencyCombo.Column(1), MyLastDate)
    Now the client wants all of the due dates to fall on the same day of the year - June 30th - regardless of the value of MyLastDate. (don't ask )

    For example, if MyLastDate is March 17, 2024 and MyFrequencyCombo is "2" then MyDueDate should be June 30, 2026.

    I thought I remember seeing a post somewhere along the line of using constants in the DateAdd Fx, but can't seem to find it now.

    Any ideas on how to accomplish this calculation?

    disclaimer: I do not have the client's permission to post the database

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Use DateSerial() ?
    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

  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,652
    One option would be the DateSerial() function, hard-coding the day and month arguments and incrementing the year.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Thanks for the suggestions. I also used DatePart to get the year from MyLastDate then I add the value from MyFrequencyCombo. Seems to work good in initial testing so here's my revised code:

    Code:
    MyDueDate = DateSerial(DatePart("yyyy", [MyLastDate]) + MyFrequencyCombo.Column(1), "06", "30")

  5. #5
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,574
    Year() would work just as well, and what I would have used?
    Then I could add 1 to it.
    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

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I would not put quotes around the 6 or the 30. They are numeric values, not text.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    easyrider is offline Advanced Beginner
    Windows 11 Office 365
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    good catch, thanks Paul

  8. #8
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 11 Office 365
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,880
    As Gasman pointed out year() works also
    Code:
    MyDueDate = DateSerial(Year([MyLastDate]) + MyFrequencyCombo.Column(1),6, 30)
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  9. #9
    ano is offline Competent Performer
    Windows 11 Office 365
    Join Date
    Nov 2023
    Posts
    204
    i donot understand your question
    a fixed data is a fixed date so no calculation

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

Similar Threads

  1. Replies: 6
    Last Post: 05-10-2018, 10:55 AM
  2. Replies: 5
    Last Post: 01-04-2017, 02:13 AM
  3. Replies: 3
    Last Post: 02-16-2016, 05:02 PM
  4. Replies: 3
    Last Post: 09-14-2013, 10:36 AM
  5. Replies: 11
    Last Post: 08-04-2010, 04:26 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