473,441 Members | 1,974 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,441 software developers and data experts.

dateadd for weekdays only

171 100+
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=dateadd("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 12152
missinglinq
3,532 Expert 2GB
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 Expert 8TB
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=dateadd("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
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...
1
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...
1
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...
3
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() ...
2
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 (...
8
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...
4
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....
2
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...
3
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.