Solved Weekday Function problem (1 Viewer)

urjudo

Member
Local time
Today, 05:47
Joined
Oct 9, 2020
Messages
67
Hello,
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
 

jdraw

Super Moderator
Staff member
Local time
Today, 06:47
Joined
Jan 23, 2006
Messages
15,379
Please show you entire code. I don't think you are using the function as intended.

' Procedure Kind: Function
' Procedure Access: Public
' Parameter DateInMonth (Date):A Date to identify the Month and Year for calculation
' Parameter Occurrence (Integer): Which occurrence of the weekday in the month
' Parameter Weekday (VbDayOfWeek): which weekday
' Return Type: Date
'
'Sample: What date is/was the First Sunday in March 1943.
'DateWeekdayInMonth(#3/21/1943#, 1, vbSunday)

Returns : 07-Mar-1943
 

urjudo

Member
Local time
Today, 05:47
Joined
Oct 9, 2020
Messages
67
@jdraw,
Thank you for your replied. I think I know my problem. I should not have the Cal that occur in three different day into three different line of code, I should place it into on line of code, like cal 97 should be:
Case Me.HoldCal = "Cal 97"
If Weekday(Me.FIRSTCDATE) = vbMonday or Weekday(Me.FIRSTCDATE) = vbWednesday or weekday(Me.FIRSTCDATE) = vbFriday then
OK = true
end if

This works now.

Thanks you,
 

Users who are viewing this thread

Top Bottom