Try modifying this Code to suit your needs:
- Dim DayOfWeek As Integer
-
Dim DayOfMonth As Integer
-
-
NumLoops = Forms![frmScheduleActivities]![FrmScheduleActivitiesSelectSub]![Total Topics]
-
-
DateCounter = Me.StartDate
-
Me.txtDateCounter = DateCounter
-
-
DayOfWeek = Weekday(DateCounter)
-
'2nd Monday = Day Between 8 and 14 and WeekDay = 2
-
'3rd Friday = Day Between 15 and 21 and WeekDay = 6
-
Select Case DayOfWeek
-
Case 2
-
DayOfMonth = 8
-
Case 3
-
DayOfMonth = 15
-
End Select
-
-
If DayOfWeek = 2 Or DayOfWeek = 6 Then
-
For Counter = 1 To NumLoops
-
Me.TopicIDCounter = Counter
-
DateCounter = CDate( _
-
DayOfMonth & "-" & _
-
Format(DateAdd("m", Interval, DateCounter), "mmm") & "-" & _
-
Year(DateAdd("m", Interval, DateCounter)) _
-
)
-
Do While Not Weekday(DateCounter) = DayOfWeek
-
DateCounter = DateCounter + 1
-
Loop
-
Me.txtDateCounter = DateCounter
-
DoCmd.OpenQuery "QryAddActivityDate"
-
Next Counter
-
Else
-
MsgBox "Your Start Date must be a Monday or a Friday!"
-
End If
Please note that I stripped your code of the other possibilities, so you will have to insert this somewhere in between.
Notes:
Lines 1-2: Add these declarations to your variables.
Line 9: This assumes that you always begin the sessions on a Monday or Friday. If not, then you should change your start date.
Lines 10-17: Some notes behind the logic. Then, test for which day of the week we are starting on. Based on the 7 day week, the second Monday can start no earlier than the 8th of the month; the third Friday, no earlier than the 15th. These are our starting points.
Lines 19, 33-35: If this is not a Monday or Friday, inform the user.
Lines 20, 32: Your code had some strange calculating of the counter and dates. Id had quite a bit of redundancy. This is a trimmed version which keeps the looping to just the standard number of iterations.
Line 21: According to your Code, TopicID was always the same as the Counter. No need for separate variables....
Lines 22-26: Look very carefully at this code, because it is based on some of your original calculations for the DateAdd Function. We use the starting date from Lines 10-17, then the Month indicated by adding a month to our date and the Year of that same date. Assign this value to our DateCounter, which is not used for anything else until it meets other criteria.
Lines 27-29: If the WeekDay identified by DateCounter does not match the desired Weekday, then add one to the DateCounter until it does.
Lines 30-31: When DateCounter meets our criteria, use it for the text boxes on our form (and I assume for your Query).
I hope this hepps! A nice little exercise in logic and math!