473,385 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

DateAdd for The current month and day of week

17
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.
Oct 17 '14 #1
14 1692
twinnyfo
3,653 Expert Mod 2GB
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....
Oct 17 '14 #2
GDC1970
17
Thats exactly what im looking for. There would be no special exceptions. I am scheduling activities for a treatment program and they are monthly on the 3rd fri of month, every month.
Oct 17 '14 #3
GDC1970
17
Or if my start date is a monday the 2nd monday of the month, I want it to add one month later on monday the 2nd monday of the month.
So I want it to use the start date info to produce the next month and day of week
Oct 17 '14 #4
twinnyfo
3,653 Expert Mod 2GB
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.
Oct 17 '14 #5
GDC1970
17
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:

Expand|Select|Wrap|Line Numbers
  1. Public Sub Assign_Groups_Click()
  2.  
  3.    Dim NumLoops As Integer
  4.    Dim Freq As Variant
  5.    Dim Interval As Variant
  6.    Dim DateCounter As Date
  7.    Dim Counter As Integer
  8.    Dim TopicID As Integer
  9.    Dim HolidayCount As Variant
  10.  
  11.  'Frequency/Interval
  12.     Freq = Forms![frmScheduleActivities]![FrmScheduleActivitiesSelectSub]![Frequency]
  13.     Select Case Freq
  14.         Case "Yearly"
  15.            Interval = 365
  16.         Case "Monthly"
  17.            Interval = 1
  18.         Case "Bi Weekly"
  19.            Interval = 14
  20.         Case "Weekly"
  21.            Interval = 7
  22.         Case "Daily"
  23.            Interval = 1
  24.     End Select
  25.  
  26.     NumLoops = Forms![frmScheduleActivities]![FrmScheduleActivitiesSelectSub]![Total Topics]
  27.  
  28.      DateCounter = Me.StartDate
  29.      Me.txtDateCounter = DateCounter
  30.  
  31.      Do While DateCounter <= Last_Date
  32.      TopicID = 1
  33.      Counter = 1
  34.      Me.TopicIDCounter = TopicID
  35.      DoCmd.SetWarnings False
  36.      DoCmd.OpenQuery "QryAddActivityDate"
  37.      For Counter = 1 To NumLoops - 1
  38.      DateCounter = dateadd("D", Interval, DateCounter)
  39.      Me.txtDateCounter = DateCounter
  40.  
  41.      TopicID = TopicID + 1
  42.      Me.TopicIDCounter = TopicID
  43.      DoCmd.OpenQuery "QryAddActivityDate"
  44.      Next Counter
  45.      DateCounter = dateadd("D", Interval, DateCounter)
  46.      Me.txtDateCounter = DateCounter
  47.      Loop
  48.  
  49.      DoCmd.SetWarnings True
  50.      Me.FrmScheduleActivitiesSUB.Requery
  51.      Me.FrmScheduleActivitiesSUB.Visible = True
  52.  
  53.      End Sub
Oct 17 '14 #6
twinnyfo
3,653 Expert Mod 2GB
Great explanation! I'm also glad you have a basic understanding of VBA.

Let me take a look at what you have and see what I can come up with to guide you along!

P.S.: Don't forget to use the Code Tags for your code!
Oct 17 '14 #7
GDC1970
17
I really appreciate your help
Oct 17 '14 #8
twinnyfo
3,653 Expert Mod 2GB
Try modifying this Code to suit your needs:

Expand|Select|Wrap|Line Numbers
  1.     Dim DayOfWeek As Integer
  2.     Dim DayOfMonth As Integer
  3.  
  4.     NumLoops = Forms![frmScheduleActivities]![FrmScheduleActivitiesSelectSub]![Total Topics]
  5.  
  6.     DateCounter = Me.StartDate
  7.     Me.txtDateCounter = DateCounter
  8.  
  9.     DayOfWeek = Weekday(DateCounter)
  10.     '2nd Monday = Day Between 8 and 14 and WeekDay = 2
  11.     '3rd Friday = Day Between 15 and 21 and WeekDay = 6
  12.     Select Case DayOfWeek
  13.         Case 2
  14.             DayOfMonth = 8
  15.         Case 3
  16.             DayOfMonth = 15
  17.     End Select
  18.  
  19.     If DayOfWeek = 2 Or DayOfWeek = 6 Then
  20.         For Counter = 1 To NumLoops
  21.             Me.TopicIDCounter = Counter
  22.             DateCounter = CDate( _
  23.                 DayOfMonth & "-" & _
  24.                 Format(DateAdd("m", Interval, DateCounter), "mmm") & "-" & _
  25.                 Year(DateAdd("m", Interval, DateCounter)) _
  26.                 )
  27.             Do While Not Weekday(DateCounter) = DayOfWeek
  28.                 DateCounter = DateCounter + 1
  29.             Loop
  30.             Me.txtDateCounter = DateCounter
  31.             DoCmd.OpenQuery "QryAddActivityDate"
  32.         Next Counter
  33.     Else
  34.         MsgBox "Your Start Date must be a Monday or a Friday!"
  35.     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!
Oct 17 '14 #9
GDC1970
17
Thanks for your help, im trying the code and im getting an error on:

DateCounter = CDate(DayOfMonth & "-" & Format(dateadd("m", Interval, DateCounter), "mmm") & "-" & Year(dateadd("m", Interval, DateCounter))
Oct 18 '14 #10
GDC1970
17
I added a ) to:
DateCounter = CDate(DayOfMonth)
I think it was missing, the code is not red. But it does stop and debug.
Oct 18 '14 #11
GDC1970
17
I changed
Dim DateCounter As Variant instead of As Date and it does not debug on this line.
Now it debugs on:
Do While Not Weekday(DateCounter) = DayOfWeek
Oct 18 '14 #12
twinnyfo
3,653 Expert Mod 2GB
Please post your entire sub again. Anything less and we don't know what you have changed or what you have kept and how you have modified the code.

Thanks!
Oct 19 '14 #13
GDC1970
17
I reworked the code and I got it working great. I really appreciate all your help. This is going to make a huge difference.
Oct 20 '14 #14
twinnyfo
3,653 Expert Mod 2GB
Great! I'm glad that we could be of service!
Oct 20 '14 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Gary | last post by:
How do you get the current month - 1 month? (so not a particular day, but the whole month) Thanks, Gary
1
by: Ben | last post by:
Hi I am currently building a database in Access 97 and have a (quite easy) question. I want to produce a query / report that will bring out all the people in the database that have a birthday...
3
by: JC21 | last post by:
Hello all, I have a question on MS calendar. On one of my forms in my DB I have the MS calendar. My question is how do I set the calendar so that the current month is always displayed. Currently...
5
by: soni2926 | last post by:
Hi, I have a web application, asp.net and c# done in 2.0, which is going to return rows from the db with dates or certain events. The db is going to have events dates for the entire year, but on...
0
by: remya1000 | last post by:
I need to display current month,last 3months,6months and 1year records seperatly. and first i'm tring to display current month's records. and the error i received is this. Microsoft VBScript...
1
by: kennyscripts | last post by:
Hi Friends...... How to get the current month and current week in vb.net 2003 Can view the month textbox details and can only enter the week detail. How to get? Anybody knows give the answer....
1
jamesd0142
by: jamesd0142 | last post by:
New here, need to store the current month in a variable? How can this be done? -------------------------------------------------------------------- Or if anyone can... i want to get the...
1
by: guyborn | last post by:
I have been trying to get data from the database from the of the previous month to the second last week of the current month.I only managed to get data from the previous month to today's date. ...
6
by: abhishekbrave | last post by:
i have created following drop down box for displaying year... i want to populate the drop down box upto current year.. and need to write a java script for this .. any pointers in this regard will be...
1
LoanB
by: LoanB | last post by:
Hey gang Need some help please. Below I have code which returns the week number of the year. I need some code which simalarly return the week number of the current month. dtDate below gets...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.