if statements to test for third Wed of the month & third Thurs. of the month
hello,
what would be the VB6 code for an if statement that would check for if it's the third wednesday of the month and execute some code based on that. Also would would be the similar code for if it's the third thursday of the month?
thanks in advance,
david
Re: if statements to test for third Wed of the month & third Thurs. of the month
The third wednesday will be the wednesday between the 15th and 21st (inclusive). the code is similar for the third thursday with the numbers being the 16th to the 22nd
so the vb code would look something like
vbcode Code:
If ((Day = wednesday) + (daynumber >= 15) + (daynumber <= 21)) Then
'code here
end if
I'm not sure how you are storing the day of the week and the day number but there is the basic logic for it.
Re: if statements to test for third Wed of the month & third Thurs. of the month
Here is code to get the 3rd Wednesday. It is based on some code by member CVMichael.
vb Code:
Private Sub Form_Load()
MsgBox ThirdWednesday(2007, 3) ' Year and month
End Sub
Private Function ThirdWednesday(ByVal Y As Integer, ByVal M As Integer) As Date
Dim TheDate As Date
TheDate = DateSerial(Y, M, 1)
TheDate = DateAdd("d", -Weekday(TheDate) + 25, TheDate)
ThirdWednesday = TheDate
End Function
Re: if statements to test for third Wed of the month & third Thurs. of the month
Quote:
Originally Posted by TBeck
The third wednesday will be the wednesday between the 15th and 21st (inclusive). the code is similar for the third thursday with the numbers being the 16th to the 22nd.
Regardless of the day of the week, the third occurance of any given day of the week will be between the 15th and 21st.
vb Code:
If Weekday(Date) = vbWednesday And Day(Date) >= 15 And Day(Date) <= 21 Then
MsgBox "Today is the third wednesday of the month"
End If
Re: if statements to test for third Wed of the month & third Thurs. of the month
Something else to note, the third occurance of any given day of week could also happen BEFORE the third occurrance of the day before -- IE, the third Thursday could happen before the third Wednesday.
-tg
Re: if statements to test for third Wed of the month & third Thurs. of the month
thanks for everyone's input. With your help, I came up with this test code that worked well as I changed the pc's calendar to the third wed, third thurs and some other days.
Code:
If Weekday(Date) = vbWednesday And Day(Date) >= 15 And Day(Date) <= 21 Then
MsgBox "Today is the third Wednesday of the month"
Else
If Weekday(Date) = vbThursday And Day(Date) >= 15 And Day(Date) <= 21 Then
MsgBox "Today is the third Thursday of the month"
Else
MsgBox "today is the " & Weekday(Date) & " / " & Day(Date) & " day of " & Month(Date) & " " & Year(Date)
End If
End If