Hello,
I found this function
I was able to get if it's 1st or 3rd Monday, however, if it's Monday or Wednesday or Friday then I was only able to enter the Monday, it won't let me to enter Wednesday or Friday. For example, if Cal 97, it took the 3/1/21 but not the 3/3/21, once I entered 3/3/21 then I will get the message. so does the other cal that that are in two different day. (like the Cal 35 which is Tuesday & Thursday, it took the Tuesday date but not the Thursday date). Below is my Code(I place the code on the afterupdate), can someone help what did wrong? Thank you for your time.
I found this 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
I was able to get if it's 1st or 3rd Monday, however, if it's Monday or Wednesday or Friday then I was only able to enter the Monday, it won't let me to enter Wednesday or Friday. For example, if Cal 97, it took the 3/1/21 but not the 3/3/21, once I entered 3/3/21 then I will get the message. so does the other cal that that are in two different day. (like the Cal 35 which is Tuesday & Thursday, it took the Tuesday date but not the Thursday date). Below is my Code(I place the code on the afterupdate), can someone help what did wrong? Thank you for your time.
Code:
dim OK As Boolean
Select Case True
Case Me.HoldCal = "Cal 97" or Me.HoldCal = "Cal 62" or Me.HoldCal = "Cal 94"
If Weekday(Me.FirstCDate) = vbMonday Then
OK = true
End if
Case Me.HoldCal = "Cal G" or Me.HoldCal = "Cal 35" or Me.HoldCal = "Cal 84"
If Weekday(Me.FirstCDate) = vbTuesday Then
OK = true
End if
Case Me.HoldCal = "Cal 97" or Me.HoldCal = "Cal A" or Me.HoldCal = "Cal 74"
If Weekday(Me.FirstCDate) = vbWednesday Then
OK = true
End if
Case Me.HoldCal = "Cal 98" or Me.HoldCal = "Cal 61" or Me.HoldCal = "Cal 54" or Me.HoldCal = "Cal 84" or Me.HoldCal = "Cal 35"
If Weekday(Me.FirstCDate) = vbThursday Then
OK = true
End if
Case Me.HoldCal = "Cal 97" or Me.HoldCal = "Cal 62" or Me.HoldCal = "Cal 94" or Me.HoldCal = "Cal 21"
If Weekday(Me.FirstCDate) = vbFriday Then
OK = true
End if
End Select
If OK = False then
Msgbox "You have entered an incorrect court date for " & Me.HoldCal & "."
End if