NearImpossible
Registered User.
- Local time
- Yesterday, 20:59
- Joined
- Jul 12, 2019
- Messages
- 225
I'm stepping outside of my comfort zone on this one, and would like for my database to kick off some alerts based on the day of the Week/Month when the main form is opened. The alerts would be set based on the 1st Monday, 1st Tuesday, 2nd Wednesday, 2nd, Thursday, etc.
I found the following Function
That I can call to get the 1st, 2nd, 3rd Monday, etc,
but I would like to automate it based on the current date, as well as calculate for those months where there are more than 4 weeks to determine if its the 4th Monday or the Last Monday of the Month, etc
I'm sure the answer is in the function above as its already dealing with the weeks/days, but as I said before, its beyond my knowledge if anyone would like to break it down for me
Do I just manually code each day similar to the above? If so, how I do determine if its the 4th Friday or the Last Friday if the month has more than 4 weeks?
Am I way off base on this ?
Any help is greatly appreciated
I found the following Function
Code:
' Calculates the date of the occurrence of Weekday in the month of DateInMonth.
'
' If Occurrence is 0 or negative, the first occurrence of Weekday in the month is assumed.
' If Occurrence is 5 or larger, the last occurrence of Weekday in the month is assumed.
'
' If Weekday is invalid or not specified, the weekday of DateInMonth is used.
'
' 2016-06-09. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function DateWeekdayInMonth( _
ByVal DateInMonth As Date, _
Optional ByVal Occurrence As Integer, _
Optional ByVal Weekday As VbDayOfWeek = -1) _
As Date
Const DaysInWeek As Integer = 7
Dim Offset As Integer
Dim Month As Integer
Dim Year As Integer
Dim ResultDate As Date
' Validate Weekday.
Select Case Weekday
Case _
vbMonday, _
vbTuesday, _
vbWednesday, _
vbThursday, _
vbFriday, _
vbSaturday, _
vbSunday
Case Else
' Zero, none or invalid value for VbDayOfWeek.
Weekday = VBA.Weekday(DateInMonth)
End Select
' Validate Occurence.
If Occurrence <= 0 Then
Occurrence = 1
ElseIf Occurrence > 5 Then
Occurrence = 5
End If
' Start date.
Month = VBA.Month(DateInMonth)
Year = VBA.Year(DateInMonth)
ResultDate = DateSerial(Year, Month, 1)
' Find offset of Weekday from first day of month.
Offset = DaysInWeek * (Occurrence - 1) + (Weekday - VBA.Weekday(ResultDate) + DaysInWeek) Mod DaysInWeek
' Calculate result date.
ResultDate = DateAdd("d", Offset, ResultDate)
If Occurrence = 5 Then
' The latest occurrency of Weekday is requested.
' Check if there really is a fifth occurrence of Weekday in this month.
If VBA.Month(ResultDate) <> Month Then
' There are only four occurrencies of Weekday in this month.
' Return the fourth as the latest.
ResultDate = DateAdd("d", -DaysInWeek, ResultDate)
End If
End If
DateWeekdayInMonth = ResultDate
End Function
That I can call to get the 1st, 2nd, 3rd Monday, etc,
Code:
Private Sub Command0_Click()
FirstMonday = DateWeekdayInMonth(Date, 1, vbMonday)
SecondMonday = DateWeekdayInMonth(Date, 2, vbMonday)
ThirdMonday = DateWeekdayInMonth(Date, 3, vbMonday)
FourthMonday = DateWeekdayInMonth(Date, 4, vbMonday)
If Date = DateWeekdayInMonth(Date, 3, vbMonday) Then
MsgBox "Third Monday: " & ThirdMonday
End If
End Sub
but I would like to automate it based on the current date, as well as calculate for those months where there are more than 4 weeks to determine if its the 4th Monday or the Last Monday of the Month, etc
I'm sure the answer is in the function above as its already dealing with the weeks/days, but as I said before, its beyond my knowledge if anyone would like to break it down for me

Do I just manually code each day similar to the above? If so, how I do determine if its the 4th Friday or the Last Friday if the month has more than 4 weeks?
Am I way off base on this ?
Any help is greatly appreciated