473,574 Members | 3,144 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dateadd for weekdays only

171 New Member
Hi
I been away for sometime. Pls help me with my new problem
i need to do a leave calculation using a text box.
I have a start date (date format) and number of days(number).
The calculation i can do is
lastdate=datead d("d",-1,[start date]+[number of days])
this gives the difference, but i need to minus the weekends from the total
I tried using "w" instead of "d" but no use
pls help me

Rj
Aug 29 '07 #1
2 12165
missinglinq
3,532 Recognized Expert Specialist
For obvious reasons, this is not something that can be done with a line or two of code! I picked this function up somewhere in the past. Sad to say, I don't know who to credit for it!

In the objects dialog box, click on Modules
Click on New
Copy and Paste this code in the module
Expand|Select|Wrap|Line Numbers
  1. '**********************************************************
  2. 'Declarations section of the module
  3. '**********************************************************
  4.  
  5. Option Explicit
  6.  
  7. '==========================================================
  8. ' The DateAddW() function provides a workday substitute
  9. ' for DateAdd("w", number, date). This function performs
  10. ' error checking and ignores fractional Interval values.
  11. '==========================================================
  12. Function DateAddW (ByVal TheDate, ByVal Interval)
  13.  
  14.    Dim Weeks As Long, OddDays As Long, Temp As String
  15.  
  16.    If VarType(TheDate) <> 7 Or VarType(Interval) < 2 Or _
  17.               VarType(Interval)  > 5 Then
  18.       DateAddW = TheDate
  19.    ElseIf Interval = 0 Then
  20.       DateAddW = TheDate
  21.    ElseIf Interval > 0 Then
  22.       Interval = Int(Interval)
  23.  
  24.    ' Make sure TheDate is a workday (round down).
  25.  
  26.       Temp = Format(TheDate, "ddd")
  27.       If Temp = "Sun" Then
  28.          TheDate = TheDate - 2
  29.       ElseIf Temp = "Sat" Then
  30.          TheDate = TheDate - 1
  31.       End If
  32.  
  33.    ' Calculate Weeks and OddDays.
  34.  
  35.       Weeks = Int(Interval / 5)
  36.       OddDays = Interval - (Weeks * 5)
  37.       TheDate = TheDate + (Weeks * 7)
  38.  
  39.   ' Take OddDays weekend into account.
  40.  
  41.       If (DatePart("w", TheDate) + OddDays) > 6 Then
  42.          TheDate = TheDate + OddDays + 2
  43.       Else
  44.          TheDate = TheDate + OddDays
  45.       End If
  46.  
  47.       DateAddW = TheDate
  48. Else                         ' Interval is < 0
  49.       Interval = Int(-Interval) ' Make positive & subtract later.
  50.  
  51.    ' Make sure TheDate is a workday (round up).
  52.  
  53.       Temp = Format(TheDate, "ddd")
  54.       If Temp = "Sun" Then
  55.          TheDate = TheDate + 1
  56.       ElseIf Temp = "Sat" Then
  57.          TheDate = TheDate + 2
  58.       End If
  59.  
  60.    ' Calculate Weeks and OddDays.
  61.  
  62.       Weeks = Int(Interval / 5)
  63.       OddDays = Interval - (Weeks * 5)
  64.       TheDate = TheDate - (Weeks * 7)
  65.  
  66.    ' Take OddDays weekend into account.
  67.  
  68.       If (DatePart("w", TheDate) - OddDays) > 2 Then
  69.          TheDate = TheDate - OddDays - 2
  70.       Else
  71.          TheDate = TheDate - OddDays
  72.       End If
  73.  
  74.       DateAddW = TheDate
  75.     End If
  76.  
  77. End Function
  78. '**************  End of Code **************
Save the module and name it AddWeekdaysOnly

Since your original code was
Expand|Select|Wrap|Line Numbers
  1. lastdate=dateadd("d",-1,[start date]+[number of days])
what you're actually doing is adding ([number of days] - 1) so we make that the Interval.

To invoke the function:
Expand|Select|Wrap|Line Numbers
  1. Interval = [number of days]-1
  2. LastDate = DateAddW([Start Date], interval)
  3.  
Welcome to TheScripts!

Linq ;0)>
Aug 29 '07 #2
ADezii
8,834 Recognized Expert Expert
Hi
I been away for sometime. Pls help me with my new problem
i need to do a leave calculation using a text box.
I have a start date (date format) and number of days(number).
The calculation i can do is
lastdate=datead d("d",-1,[start date]+[number of days])
this gives the difference, but i need to minus the weekends from the total
I tried using "w" instead of "d" but no use
pls help me

Rj
This will point you in the right direction:
Calculating Weekdays
Aug 29 '07 #3

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

Similar topics

2
8049
by: Tiernan | last post by:
Hi all I'm looking for a way to find the number of weekdays between 2 dates In my form I have three fields for a begin date (dd)(mm)(yyyy) and three for the end date (dd)(mm)(yyyy) Now these values will be stored into a database and at the same time there will be an e-mail generated and send to me that contains the starting date en the end...
1
17315
by: Raghu | last post by:
Hello... I am running into a problem while running a query..can some1 help.. this is the query : ************** SELECT * from Table S where S.dtDate1 BETWEEN dateadd(year,1,dateadd(month,-1,getdate())) AND dateadd(day,-1,(dateadd(month,1,dateadd(year,1,dateadd(month,-1,getdate()))))) *************** (first part of the date calculation...
1
4186
by: Htk | last post by:
I have two date fields: -Inquiry Date -Action Date I'm trying to use a query expression on this. Using the datediff expression, I can get the amount of days that seperate the two dates. But I would like to know how I can get only a count of the weekdays only. I'm not sure if there is another expression I can use to get the result.
3
3715
by: Annette Massie | last post by:
I am trying to insert a record into a table via code and one of the values to add I would like as a dateadd calculation on a value from a query. My code looks like this: Set db = CurrentDb() ' if the table is in the same database Set rsAdd = db.OpenRecordset("tblClientTreatment") With rsAdd .AddNew !ClientID = Me.ClientID
2
3006
by: rob | last post by:
Hello all, I have a report that is always due 14 weekdays (Monday thru Friday) from the day it is suspensed. I would like to use the DateAdd function using the weekday interval ( DateInterval.Weekday), but I've tried it and it returns the same value as (DateInterval.Day). Am I doing something wrong, or do I have the wrong expection for...
8
4718
by: atiq | last post by:
I am trying to restrict the user to only enter date which is weekdays. so, it shouldn't allow a date that is weekend such as 08/04/07 (Sunday). Can someone help me with this issue. In short i want the user to enter a date which is Monday, Tuesday, Wednesday, thursday and Friday! Additionally, i'm new to Databases so, could you tell me whether...
4
1993
by: MLH | last post by:
I'm thinking of an integer field in which I could write values up to 1+2+4+8+16+32+64 and to use these values later. Each of the individual values would represent a weekday from Sunday to Saturday. So if I were to look at a record for a newspaper publication and see 9 in the field, I could interpret that as meaning the publication was...
2
2386
by: awojciehowski | last post by:
Can any one point me in the right direction here... I have a report that shows an entry and under that record there are several sub records...best way to explain it is imagine an order with several items in that order entry. I have placed a DateAdd feature in a report that will show me 90 days after the date order was made. My problem...
3
6265
by: Mel | last post by:
When I use the DateAdd function using "DateInterval.Weekday" it does not return the correct date, well at least how I thought the weekday option should work. It is counting weekends and I only want it to count the weekdays (Mon-Fri). How can I get it to work? (using Asp.net 2.0, vb.net) Example: dPlanFD = DateAdd(DateInterval.Weekday,...
0
8234
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7815
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6452
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5620
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5300
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3740
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2243
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1060
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.