473,398 Members | 2,335 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,398 software developers and data experts.

How to calculate dates based on a 5 day week

Breeves22
Hi all,

I was wondering if it was possible to use Access 2000 to auto calculate a date in a field operating under a 5 day working week

Thankyou in advance
Sep 14 '10 #1

✓ answered by TheSmileyCoder

Here you go, I threw this together for you:
Expand|Select|Wrap|Line Numbers
  1. Public Function addWorkDays(DateInput As Date, intDays As Integer) As Date
  2.     'Function that only adds days if they are workdays (monday-friday)
  3.     'Holidays are not excluded.
  4.     'Code by: TheSmileyOne
  5.     'First presented on: www.BYTES.com
  6.     ' Code my be freely used, redistributed and eaten. 
  7.     ' If you value my work enough to use it, 
  8.     ' please value it enough to leave my name in it.
  9.  
  10.  
  11.     'Setup a working date
  12.         Dim myDT As Date
  13.         myDT = DateInput
  14.  
  15.     'Setup number of days remaining to add
  16.         Dim intDaysRemain As Integer
  17.         intDaysRemain = intDays
  18.  
  19.     'Now add days 1 at a time,
  20.     ' while counting down intDaysRemain,
  21.     ' but only if day is a working day
  22.         Do Until intDaysRemain = 0
  23.             myDT = DateAdd("d", 1, myDT)
  24.             Select Case Weekday(myDT)
  25.                 Case vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday
  26.                     intDaysRemain = intDaysRemain - 1
  27.                 Case vbSaturday, vbSunday
  28.                     'Don't modify intDaysRemain
  29.             End Select
  30.         Loop
  31.     'Return answer
  32.         addWorkDays = myDT
  33.  
  34. End Function
Now this will only exclude saturday/sunday, not holidays. If you need to exclude holidays as well, let me know, and I will work something out for that (the solution would involve you manually entering holidays into a table.)

19 2568
TheSmileyCoder
2,322 Expert Mod 2GB
Yes.

Give us some more information to work with, we can write you a general answer, but its much easier to write something specific. Give us an example of what you want to accomplish.
Sep 14 '10 #2
Thankyou for your time.

What i am trying to do is create a database to monitor all our active cases. My company works on a 5 day week (Monday to Friday) and the database needs to reflect that. For example once we have drawn up a schedule of works we send it to the local council for approval and they have 15 days to return it to us with their comments. Now if i use DateAdd to just add 15 days from the date sent it will come up with the wrong date because of our only counting week days.
Sep 14 '10 #3
TheSmileyCoder
2,322 Expert Mod 2GB
Here you go, I threw this together for you:
Expand|Select|Wrap|Line Numbers
  1. Public Function addWorkDays(DateInput As Date, intDays As Integer) As Date
  2.     'Function that only adds days if they are workdays (monday-friday)
  3.     'Holidays are not excluded.
  4.     'Code by: TheSmileyOne
  5.     'First presented on: www.BYTES.com
  6.     ' Code my be freely used, redistributed and eaten. 
  7.     ' If you value my work enough to use it, 
  8.     ' please value it enough to leave my name in it.
  9.  
  10.  
  11.     'Setup a working date
  12.         Dim myDT As Date
  13.         myDT = DateInput
  14.  
  15.     'Setup number of days remaining to add
  16.         Dim intDaysRemain As Integer
  17.         intDaysRemain = intDays
  18.  
  19.     'Now add days 1 at a time,
  20.     ' while counting down intDaysRemain,
  21.     ' but only if day is a working day
  22.         Do Until intDaysRemain = 0
  23.             myDT = DateAdd("d", 1, myDT)
  24.             Select Case Weekday(myDT)
  25.                 Case vbMonday, vbTuesday, vbWednesday, vbThursday, vbFriday
  26.                     intDaysRemain = intDaysRemain - 1
  27.                 Case vbSaturday, vbSunday
  28.                     'Don't modify intDaysRemain
  29.             End Select
  30.         Loop
  31.     'Return answer
  32.         addWorkDays = myDT
  33.  
  34. End Function
Now this will only exclude saturday/sunday, not holidays. If you need to exclude holidays as well, let me know, and I will work something out for that (the solution would involve you manually entering holidays into a table.)
Sep 14 '10 #4
JDDDDD
15
Hiii

Im trying to do the same. could you please tell me where to enter the code
Sep 16 '10 #5
TheSmileyCoder
2,322 Expert Mod 2GB
The code itself, should go in a seperate access module. In the main access object browser (the datebase window) select Modules, then New. Simply copy & paste the code into the window there.

Wherever you need to use it, you can simply call the function, like any other function. If per say its a calculated control it could like this in the controlsource:
Expand|Select|Wrap|Line Numbers
  1. =addWorkDays([OriginalDate];5)
Sep 16 '10 #6
JDDDDD
15
ok i got where to put it sorted dudes. but when i try your function code it just comes up with
The visual basic model contains a syntax error. check the code and then recompile it.

i copied and pasted your code Smiley but changed Original date to Date courswork given and changed the 5 to 15
Sep 17 '10 #7
Unfortunatley i am having the same problem as JDDDDD. I am unable to get =addworkdays the code to work on my form. I tried adapting it a little to make it similar to the way i use a DateAdd but still no luck. any help would be greatly appreciated.
Sep 17 '10 #8
TheSmileyCoder
2,322 Expert Mod 2GB
Sometimes when I copy code blocks from here, the line numbers get copied with, they of course need to be removed. Im wondering if maybe you deleted to much/little.

I retried the code, this time in Access 2007, and it still works fine. I made a small modification to the way I call it from within a form, so that it could more smoothly handle cases where you had not yet entered your original date.
Expand|Select|Wrap|Line Numbers
  1. =IIf(IsNull([OriginalDate]);"";AddWorkDays([OriginalDate];5))
Depending on regional settings you may need to replace ; with ,

Again, this is the syntax to be used in the controlsource of a textbox on a form, and it would give a CALCULATED field to display on your form.(I.e. the info is not stored in your table)

If your using it in another way, provide more details (such as code if relevant).
Sep 17 '10 #9
JDDDDD
15
Still not working :'( this time it says you omitted and operand or operator, you entered an invalid comma or character or you entered text without surrounding it in quotation marks. I copied the code exactly. then only changed Original date to read DateCourseworkGiven.

The code i used before when doing a 7 day week was =DateAdd("d",15,[DateCourseworkGiven]) and that works fine for a full week but i want to base it of university days so i got an accurate image of when i need to do things by
Sep 20 '10 #10
TheSmileyCoder
2,322 Expert Mod 2GB
I don't know what to say, as its working fine for me. Best I can offer is that you take a screenshot, showing exactly where/how you are using it, and then post it here.
Sep 20 '10 #11
JDDDDD
15
could it be because i am using access 2000. Maybe that version doesnt support what i am trying to do
Sep 20 '10 #12
TheSmileyCoder
2,322 Expert Mod 2GB
I can't say for sure, since I dont have access 2000, but there is no special references being used in the code, and its all simple enough code. My guess is still that your using it some odd place, or with incorrect syntax. Thats why im asking for a screenshot, as an image can say more then a 1000 words :P
Sep 20 '10 #13
JDDDDD
15
ok thanks dude. i will try and post a screenshot of my form in the morning. Will try again tonight to get it working and fingers crossed it will work now!
Sep 20 '10 #14
JDDDDD
15
OK Dude. Please find attached a scheen shot showing my setup and the code.

What im trying to do is imput the date each action occurs and then have it automatically update the field next to it with the date the next action should be done by. But i only want to use weekdays cause thats what uni runs off savvy?
Attached Images
File Type: jpg Coursework Database Screenshot.jpg (48.5 KB, 496 views)
Sep 21 '10 #15
TheSmileyCoder
2,322 Expert Mod 2GB
I don't know what the issue is. I've tested the code and it works, so you must be doing something wrong, or misunderstood something.

You can upload the database here and I can look at it, other then that I have no more to offer.
Sep 22 '10 #16
JDDDDD
15
Its ok dude. managed to sorts it out. I changed the ; to a , and it works perfectly. must be some random thing about access 2000
Sep 22 '10 #17
TheSmileyCoder
2,322 Expert Mod 2GB
I hate to say I told you so....but.....
TheSmileyOne Post #9
Depending on regional settings you may need to replace ; with ,
Sep 22 '10 #18
Thanks smiley one.

your help with this problem was greatly appreciated.
Sep 22 '10 #19
JDDDDD
15
yo guy totally. you rulez. such a help now i can gets me database to tell me when all me coursework needs to be done 4.

cheerz dudez
Sep 22 '10 #20

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

Similar topics

6
by: jochen scheire | last post by:
Is there a way I can calculate a field in a form based on another field in the same form. When clicking submit, both values should be posted to the next page. I want to be able to type in a value...
6
by: Alex | last post by:
Using the function datepart I can return a week number given any date. What I now want to do is return a date (say monday) given a week number. How can I do this? Alex
1
by: Alicia | last post by:
I basically wanted something that would do a count of the dates, then group them by their week name.. BEFORE: Resource Date Total number of times that date Appeared (Count) 4/3/03 ...
2
by: Rustan | last post by:
Hi Im using GregorianCalendar to find out the current years week numbers. When the user chooses a week number in a dropdown i want to show that week in a table with the corresponding dates. For...
3
by: Tim Chase | last post by:
I've been trying to come up with a good algorithm for determining the starting and ending dates given the week number (as defined by the strftime("%W") function). My preference would be for a...
2
by: Kevin | last post by:
Hi all, Does anyone know of a script or package that will allow me to calculate the localtime given a country code (and optional state/province for US/Canada)? It should factor in daylight...
1
by: Karen Price | last post by:
I am trying to calculate dates by having a beginning date and adding the applicable number of months and coming up with a new date. EX: Original Date = 06/09/06 and need to add three months to...
1
by: sang | last post by:
Hi how to list the inserted dates by week,month and year. That is how to select the dates by past one week. similarly the month and year of the particular filed. Thanks in Advance Sang
9
by: FunkHouse9 | last post by:
I am trying to set up a page that calculates a total ad price based on a word count value. I have the word count displaying properly in op3, but just can't get the price even close to working right....
1
by: favor08 | last post by:
have serval date fields in my table that i need to perform calcualations to bring bring a certain # of items. Fields: RptDte Date filed closed always the last date of previous month--- 6/30 for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.