Results 1 to 6 of 6
  1. #1
    Monterey_Manzer is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2012
    Posts
    164

    Calculated Future Dates

    Howdy All,



    I have a situation where I want to display a series of dates on a form. However, by setting the first date in this series, the remaining dates are predetermined and I would like to automate this feature. Specifically, these dates will always be the third Friday of every month. Ideally my form users will select the first date in the series and the next two dates (the rest of the series) would be determined via a VBA formula that would determine the date of the third Friday of the following month. I've seen people using the iif command to handle skipping weekends when calculating differences in dates but I can't see if that would apply here. I'm trying to figure out how I would correct for the different weekdays at the start of each month and the different total days in each month. I would appreciate any help anyone could give.

    Thanks!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A brief Google turned this up:

    http://www.pcreview.co.uk/forums/cal...-t3876459.html

    You should be able to use that and DateAdd() to calculate the 3rd Friday for the months following the input date.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Monterey_Manzer is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2012
    Posts
    164
    Thanks! Rather than use DateAdd() I pulled my Month and Year integers called for in the DayInMonth function from my first date field using the Month() and Year() functions. I then set the calculation to include a +1 to the Month integer. Using this method I was able to set my second date to the 3rd Friday of the following month. This works great! In case it will help anyone else, here's the code I used:

    <code>
    Public Function DayInMonth(WeekNumber As Integer, Wkday As Integer, dMonth As Integer, dYear As Integer) As Date

    Dim FirstOfMonth As Date
    Dim NextWeekDay As Integer
    Dim RootDate As Date

    FirstOfMonth = DateSerial(dYear, dMonth, 1)
    NextWeekDay = IIf(Wkday = vbSaturday, vbSunday, Wkday + 1)
    RootDate = FirstOfMonth - Weekday(FirstOfMonth, NextWeekDay)
    DayInMonth = RootDate + WeekNumber * 7

    End Function

    Private Sub First_ISP_AfterUpdate()
    'The date for the second ISP will be set once the date of the first is updated.
    Me.Second_ISP.Value = DayInMonth(3, vbFriday, Month(Me.First_ISP.Value) + 1, Year(Me.First_ISP.Value))
    End Sub
    </code>

  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
    Hopefully that works in December.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Monterey_Manzer is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2012
    Posts
    164
    Okay, I've got a better one for you that DOES work when I give it dates either in December or November (since we're talking about computing out to two months in the future):

    Code:
    Private Sub First_ISP_AfterUpdate()
    'Calculating the third Friday of the month following the month of the first ISP.  If,Then statements used to cover potential change in years.
         If Month(Me.First_ISP.Value) = 12 Then
         Me.Second_ISP.Value = DayInMonth(3, vbFriday, 1, Year(Me.First_ISP.Value) + 1)
         ElseIf Month(Me.First_ISP.Value) < 12 Then
         Me.Second_ISP.Value = DayInMonth(3, vbFriday, Month(Me.First_ISP.Value) + 1, Year(Me.First_ISP.Value))
         End If
    'Calculating the date of the third ISP from the date of the second
         If Month(Me.Second_ISP.Value) = 12 Then
         Me.Third_ISP.Value = DayInMonth(3, vbFriday, 1, Year(Me.Second_ISP.Value) + 1)
         ElseIf Month(Me.Second_ISP.Value) < 12 Then
         Me.Third_ISP.Value = DayInMonth(3, vbFriday, Month(Me.Second_ISP.Value) + 1, Year(Me.Second_ISP.Value))
         End If
    End Sub
    I don't know if anyone else is interested in stuff like this but I was pretty happy when I got this working so hopefully it will help someone else too.

    Ps. To avoid confusion all ISP terms refer to dates of payment for Internship Support Payments (hence ISP).

  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,521
    I would have avoided the testing by adding a month to the first date with DateAdd, then passing the month and year of the resulting date to the function. In any case, glad you have a working solution.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Query criterion for today and the future
    By scoughlan in forum Queries
    Replies: 3
    Last Post: 01-16-2012, 07:28 PM
  2. Setting up a calculated field for dates
    By JoshD in forum Queries
    Replies: 3
    Last Post: 03-04-2011, 11:59 PM
  3. How To Print Calculated Dates on Report
    By ChrisCione in forum Reports
    Replies: 2
    Last Post: 01-15-2011, 03:26 PM
  4. Future date parameter
    By normie in forum Queries
    Replies: 10
    Last Post: 08-12-2010, 09:38 PM
  5. Question about the future of Access
    By kantell in forum Access
    Replies: 0
    Last Post: 11-04-2008, 11:43 AM

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