DateAdd(W) Query

scgoodman

scgoodman
Local time
Yesterday, 23:39
Joined
Jun 6, 2008
Messages
87
DateAdd Functions
I am trying to perform some date due functions, however have an issue with it. I am performing a DateAddW expression, however...I need to account for Sunday's and Saturdays. See table below.

TAT Schedule Examples attached.
 

Attachments

Your spreadsheet shed no light on your problem, there is no code or explanation of what you are trying or have tried.

Brian
 
I am performing a DateAddW function.

Due Date: IIf([Account]=PSG, DateAddW('d','5',[ReceivedDate])

So all accounts = PSG.
I want to add 5 work days to the received date and deliver on the 6th day. Problem is with Saturdays and Sundays.
Example:

Sunday 4/5/2009, the actual due date is 4/13/2009.
Saturday 4/11/2009, the actual due date is 4/20/2009

Does this help?
 
What is confusing is this is the queries thread, your code is certainly ACCESS but your data is in EXCEL.

Brian
 
If your data is in ACCESS then I would expect that you are going to have to use weekday or some technique to check for the day the result gives and adjust accordingly. Probably better in a function.

Brian
 
Ignore the excel. I just had that in there for examples of due dates. That is all.
 
As I told you you need to check on the day of the result, if I have to do all the work it would be
duedate: IIf([account]="PSG",IIf(Weekday(DateAdd("w",5,[fdate]))=7,DateAdd("w",7,[fdate]),IIf(Weekday(DateAdd("w",5,[fdate]))=1,DateAdd("w",6,[fdate]),DateAdd("w",5,[fdate]))))

I used fdate as I couldn't remember your field name.

Note
ITs Dateadd not Dateaddw
The number of days being numeric is not in quotes

A more carefull read of help on Dateadd is called for, also read help on weekday to ensure that you understan what is going on.

Best of luck

Brian
 
This was fun to play with. To get it working:

1) Imported the spreadsheet to tblTat.
2) Added Account field
3) Renamed other fields DayOfWeek, TAT, RecDte, DueDte. Note that Date is a reserved word in Access and should not be used as a field name. Spaces in field names are not only a pain, but inhibit some internal Access functions (e.g. Intellisense)
3) Hand input PSG, ESS to [Account] as appropriate
4) Deleted unnecessary rows

The date fields (RecDte, DueDte) imported as strings containing the double numbers (e.g. "39908" as stored by Access. To convert them to dates, added 2 datefields (RecDteX, DueDteX) and ran an Update query where the Update To looked like:
CDate(Int(CDbl([RecDte]))) and CDate(Int(CDbl([DueDte])))
Deleted fields RecDte and DueDte, then renamed fields RecDteX and DueDtex

The following function adds (or subtracts) business days (Mon - Fri) to a date. It doesn't, however, mimic the logic of using the next Monday as the start date if the original start date is a Saturday or Sunday.

Code:
Function UpBusDays3(pstart As Date, _
                    pNum As Integer, _
                    Optional pAdd As Boolean = True) As Date
'*******************************************
'Purpose:   Add or subtract business days
'           from a date
'Coded by:  raskew
'Inputs:    1) +) ? UpBusDays3(#2/17/06#, 3, True)
'           2) -) ? UpBusDays3(#2/22/06#, 3, False)
'Output:    1) +) 2/22/06
'           2) -) 2/17/06
'*******************************************
                    
Dim dteHold As Date
Dim i       As Integer
Dim n       As Integer

    dteHold = pstart
    n = pNum
    For i = 1 To n
       If pAdd Then  'add days
          dteHold = dteHold + IIf(WeekDay(dteHold) > 5, 9 - WeekDay(dteHold), 1)
       Else          'subtract days
          dteHold = dteHold - IIf(WeekDay(dteHold) < 3, Choose(WeekDay(dteHold), 2, 3), 1)
       End If
    Next i
    UpBusDays3 = dteHold
    
End Function

...so, created an Update query that computed the due date as:

DueDteX: UpBusDays3(DateAdd("d",InStr("17",Weekday([RecDte])),[RecDte]),IIf([Account]="PSG",5,4),,True)

this statement: InStr("17",Weekday([RecDte])) adds 1 day if RecDte is Sunday, 2 days if RecDte is Saturday, otherwise 0.

this statement: IIf([Account]="PSG",5,4) returns the number of business days to add. If there were more than 2 Accounts, this would have to be modified, probably using the Switch() function rather than Iif().

In testing, the resulting dates corresponded completely with your DueDte.

HTH - Bob
 
Last edited:
Bob
I had to look up the Ozarks , it is not part of the US I have visited, looks nice, why are you bored? :D

The guy already has a DB the EXCEL was a red herring.

If there is more than one account to allow for with different "adds" I would use a function using Select Case and basing the alterations on the date recieved, but he signed off last night, my time, without responding to my suggestion, so not sure were we stand at the moment.

Brian
 
Brian -

I'd said previously:

The date fields (RecDte, DueDte) imported as strings containing the double numbers (e.g. "39908" as stored by Access.

What's strange is that after saying that, I re-downloaded the xls and the 'dates' came thru as dates, couldn't get the '39908' if I tried.

Still trying to figure that out.

Bob
 

Users who are viewing this thread

Back
Top Bottom