473,699 Members | 2,612 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 12171
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
8055
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 date . But now I want an extra line in this e-mail that tells me howmany days there are between the...
1
17327
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 comes out to be '2005-05-01' and
1
4194
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
3725
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
3008
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 this
8
4725
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 the validation rule given should be used during table design or on the form date field. Any help...
4
2002
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 released on Sundays (1) and Wednesdays (8). A value of 127 would indicate a daily paper publishing...
2
2393
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 lies in that the DateAdd feature is now showing multiple times under each order...it lists the same...
3
6270
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, 10, "11/04/2008") 'dPlanFD = #11/14/2008# 'I want it to return #11/18/2008#
0
8623
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9197
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8941
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8897
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7785
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
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
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.