I need to know the MS Access VBA code to produce a DateAdd to give me one month from today and the same day of the week.
DateAdd for The current month and day of week
Collapse
X
-
GDC1970,
Please provide a little more information. Do you mean, for example, today is October 17, but it is the third Friday of the Month, so, one month from now would be November 21, as it is the third Friday of the month?
How do you handle special situations, such as a fifth weekday of one month with no corresponding next month?
Also, what is the nature of this question, which also might help us provide either different solutions or different options that also might suit your needs.... -
So, now I have to ask, what have you tried so far?
How are you creating these dates? Is it on a form? Are you creating a schedule in Access? How many dates do you schedule in advance?
Also, what kind of experience do you have with MS Access and VBA?
If you follow where my questions are leading, there may be more involved than just the simple answer of getting a list of every 2nd Monday or 3rd Friday of the month. I am trying to make sure you have something you can use and understand for your next project. But, we also need to know what you have envisioned for how this will work. I don't want to start leading you down one path and then find out that this is not what you need. I hope you understand.Comment
-
I am pretty good with VBA but not and expert. I do use a form to select the activity I want, then enter a start and end date. I usually schedule for 2 years at a time. This code works great for weekly, bi weekly scheduling, but I need to incorporate the monthly.
Here is my current code:
Code:Public Sub Assign_Groups_Click() Dim NumLoops As Integer Dim Freq As Variant Dim Interval As Variant Dim DateCounter As Date Dim Counter As Integer Dim TopicID As Integer Dim HolidayCount As Variant 'Frequency/Interval Freq = Forms![frmScheduleActivities]![FrmScheduleActivitiesSelectSub]![Frequency] Select Case Freq Case "Yearly" Interval = 365 Case "Monthly" Interval = 1 Case "Bi Weekly" Interval = 14 Case "Weekly" Interval = 7 Case "Daily" Interval = 1 End Select NumLoops = Forms![frmScheduleActivities]![FrmScheduleActivitiesSelectSub]![Total Topics] DateCounter = Me.StartDate Me.txtDateCounter = DateCounter Do While DateCounter <= Last_Date TopicID = 1 Counter = 1 Me.TopicIDCounter = TopicID DoCmd.SetWarnings False DoCmd.OpenQuery "QryAddActivityDate" For Counter = 1 To NumLoops - 1 DateCounter = dateadd("D", Interval, DateCounter) Me.txtDateCounter = DateCounter TopicID = TopicID + 1 Me.TopicIDCounter = TopicID DoCmd.OpenQuery "QryAddActivityDate" Next Counter DateCounter = dateadd("D", Interval, DateCounter) Me.txtDateCounter = DateCounter Loop DoCmd.SetWarnings True Me.FrmScheduleActivitiesSUB.Requery Me.FrmScheduleActivitiesSUB.Visible = True End Sub
Comment
-
Try modifying this Code to suit your needs:
Code: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!Comment
Comment