How to delete weekends

  • Thread starter Thread starter csr
  • Start date Start date
C

csr

Guest
Wanted : Query to return the number of days less non working days between two fields.

Currently tried : Select Query with calculated field as follows
Max TAT: Max(DateDiff("d",[Date In],[Date Out])+1)
This works but includes weekends as well

Have looked at NETWORKDAYS but cannot seem to get it working - if that is the solution.
Any help most appreciated.
 
Function FindWkDays(vardate1 As Variant, vardate2 As Variant, _
incl As Boolean) As Integer
'*******************************************
'Name: FindWkDays (Function)
'Purpose: Simple, non-formula method of
' determining weekdays between
' two given dates.
'Inputs: ? findwkdays("10/6/00", "09/17/01", false)
'Output: 246
'Note: if incl = true then include vardate in the
' calculation, otherwise don't
'*******************************************
Dim date1 As Date, date2 As Date, n As Integer
'date1 will be the lesser date
date1 = IIf(DateValue(vardate1) > DateValue(vardate2), DateValue(vardate2), DateValue(vardate1))
date2 = IIf(DateValue(vardate1) > DateValue(vardate2), DateValue(vardate1), DateValue(vardate2))

n = 0
Do While date1 < date2
If IsWeekday(date1) Then
n = n + 1
End If
date1 = date1 + 1
Loop

'modify the following depending on how you
'want to include starting and ending dates
If incl = True Then
If IsWeekday(date1) And IsWeekday(date2) Then
n = n + 1
End If
End If

FindWkDays = n

End Function
'********************
Function IsWeekday(varDate As Variant) As Boolean
Dim dteDate As Date

dteDate = DateValue(varDate)
IsWeekday = IIf(WeekDay(dteDate, 1) > 6 Or WeekDay(dteDate, 1) = 1, False, True)
End Function
 
Thanks raskew, but is there anyway of doing this from the design view of the query itself? I'm not that much of a programmer.

Also the way the query is structured so far is that a date range is entered via a parameter query(normally a month) and then each record that has qualified is then calculated with the datediff function in a separate field.

For example, 6 records may have been retrieved that are within the date range I have specified, is there any simple way,not involving complex code ( to me
frown.gif
), that will then eliminate the possible weekends??

Thanks for the reply you sent.
 
csr-
Have just looked back at two of your inquiries to which I responded. In both cases you've used the "I'm not much of a programmer...and couldn't you make it simplier" cop-out.

Yes, of course! Send money, I'll do it all for you. If that isn't a viable option, then you probably need to spend some time learning the rudiments, since this is how the rest of us did it. Hate to be rude, but what's the alternative?

Best wishes,

Bob
 
Raskew,

I am not willing to tip on your toes, all the more that I share the general idea you have on this matter. But not necessarily on some specific cases. I remember when (not so long ago, just a few months) I was myself learning everything about ACCESS and VBA from scratch, and at the same time needed to do things that were still beyond my knowledge...


Csr

I am still not clear about whether you want to get the number of weekdays or identify dates that are weekdays, so I ll show you both:
This will give you the number of saturdays and sundays between the dates FromDate and ToDate (you will have to decide on whether you want to include ToDate in the calculation).

(DateDiff("ww", FromDate, ToDate, vbsunday) - Int(vbsunday= Weekday(FromDate))) + (DateDiff("ww", FromDate, ToDate, vbSaturday) - Int(vbSaturday = Weekday(FromDate)))

You ll have to display or store the result.
I put unecessary parenthesis to show that this is twice the same formula applied to different constants (vbSunday and vbSaturday).


Determine if a given date is a saturday or sunday: dDate is your date

(vbsunday = Weekday(dDate)) or (vbsunday = Weekday(dDate))

The result will be true (-1) or false (0)
It can be used 'as it' as a criteria in a query

I hope that you will have the curiosity to to check how this works, using access help. Post if you have questions.


Alex

[This message has been edited by Alexandre (edited 09-17-2001).]
 
Alexandre-

I'm sure you believe I abused CSR unfairly. If so, that's OK. However, my point is, if you want to learn how to program in any language (not just VBA), you've got to jump in and make an effort. What does anyone learn when their response is "...oh, that looks so difficult, can't you make it easier?"

Of course it looks difficult. There's not a one of us that doesn't suffer from the same problem when presented with a new, unfamiliar situation. The idea is either you attack and conquer the problem, or you wimp-out and learn nothing.

Why would we (you) want to defend someone who isn't willing to give it their 'best shot'?

Look forward to hearing from you.

Best wishes,

Bob
 
Bob

Why are you bothering to reply to these messages posted by people such as me?

I thought these types of forums were designed to help all people, no matter what level of understanding they have in Access. I didn't know we had to have a certain level of expertise, such as yours!, to join in!
Lucky you, you must have been born brilliant to know how to do everything! Sorry but most people are not like that.

All I was wondering was if there was a way to perform the task I required at a level that I currently am at. To complain, "...oh, that looks so difficult, can't you make it easier?" geez..... lighten up. I was just asking If there was a simple function or what ever that had the code already built in that I required. For example, Command button wizards - predetermined commands that perform specific tasks. Do you scoff at these and insist on re-writing the code that is behind these because you belive they are too simple for your brain to accept?

I agree with what you say about the need to give it a try, but due to circumstances beyond my control, I do not have the resources to do what you say, and that's not because I am not willing to give it a go, I am.

I am on a ridiculous deadline that my employer has given and came across this forum with the hope of receiving a little help and understanding. Obviously those who post cannot filter out those who reply.

Thankyou Alexandre for your understanding.
 
raskew,

don't really want to stick my nose in this, but...

two things occur to me:
1. csr requested a 'Query' to return the number of days
2. csr posted the question to the 'queries' section, not modules/VBA.

you generously responded with a VBA function. csr then re-posted stating a desire to accomplish the task with a query, then was flamed.

i did not look at any previous postings from csr (and that might the point of your contention). but, i think you must recognize that there are multiple ways of accomplishing a task in access (yes, even macros) without rejecting those ways that are not your particular favorites.

think you may be out of line on this one...

al
 
Thanks Al-

Please post your solution to the query problem.

Bob
 
raskew,

Alexandre's post should be adequate...did you read it?

al




[This message has been edited by pcs (edited 09-19-2001).]
 
csr,
What you are requesting can be accomplished with a query, but it takes some fairly advanced SQL knowledge to do it. If you would be willing to e-mail me the table that the query is to be based upon, I will create the query for you.
My e-mail address is:

dahuit@qosi.net

Darrin@CB69
 
raskew

I m posting in the hope you won t be yet too fed up with this thread to read anymore. I appreciated that you answered to me and believe it is fair to let you know that for the essential I share your point of view.
It took me a moment to admit, but at the end I came to the conclusion that this may be essentially a question of degree between your attitude and mine.

I think it is important to 'grant the benefice of doubt' (?? literally translated from French), and I do not believe you can positively decide on whether "someone [is or] isn't willing to give it their 'best shot'" on the base of two posts. At least, I think that when you do, you should admit that you might very well be misjudging that person and refrain from flaming.

After all, someone who doesn t make effort doesn t get anywhere neither. One can 'survive' like that for a moment. But not for long. He/she can do a few things but will never achieve any consistent result, nor will fool anybody regarding his attitude/real abilities for long. Sooner or later he/she will get his due.

So when I get irritated (I do just like you), I refuse to take the risk of beeing unfairly offensive, and I just switch to a more interesting person.

But maybe... that just means that you are more direct and frantic than I am?


Alex
 
Absolutely nothing to do with this thread Alexandre, I'm just curious where in the world you live?
 
Well, i am French. However, I ve been essentially leaving abroad in so called 'developping' countries for the last few years since my areas of specialization are rural development and economics. I am presently working in Angola.

I would be curious to know what raised your curiousity?

Alex
 
Returning to the scene of the crime-

The requester hoped for a query, thinking it would be simple and something with which he/she'd had some experience. In fact, a query solution is probably the most difficult way to resolve the problem. Consider this:

The process of determining work days normally involves:

(1) Determine the number of workdays in the start week.
(1) Determine the number of full, 7-day weeks, between the start and end dates, and multiplying that by 5 (number of workdays in a full week)
(1) Determine the number of workdays in the end week.
(1) Total steps 1, 2 & 3.
(1) Determine if we want both start and end dates included in total. For example, if a process starts on Wednesday and concludes on Thursday, is the number of workdays involved 1 or 2? The answer can vary. If we start a process at 0800 and conclude it at quitting time (1630), we've used an entire day so we might say that process involved one day. Conversely, if we start a process at 1600 on day 1 and conclude it at 1615 on day 2, how many days do we record? Another example: start at 0800 on day 1, conclude at 1630 on day 2. Keeping in mind that we're recording only days, not times, the answer on how to handle the situation involves an arbitrary decision on the part of the user. Once determined, we need to add some code to correspond with the desired response.
(1) Once we've resolved this ugliness, design a work-around to deal with the situation where the start and end dates fall within the same week, because steps 1 & 3 won't work in this scenario.

I'd hope that you'd agree that putting all of the above into a query is probably not a job for the inexperienced or faint-of-heart. While it can be accomplished with a query, the resulting query-SQL is going to be 'the mother of all queries'.

Having dealt with this over a period of years, finally figured out that it would be much easier to let the computer do what it does best-looping through a group of variables. In other words, create a variable named n and give it the value of 0, then

Starting with the Start Date:

(1) Is the start date a weekday? If so, add 1 to n.
(1) Add 1 day to the start date
(1) Repeat Step 1.
(1) Continue to do this until the Start Date = End Date.

When it's looped its way through, you only need to deal with the problem discussed in (5), above.

So, what's wrong with this solution? It's code versus query. The inexperienced requester is afraid of code, thus he/she is asking for a solution expressed in a query (the most difficult one to achieve).

I'll be the first to admit that my response was possibly unnecessarily harsh. If so, I apologize (but I doubt that that was the case)!! If you've viewed and responded to Access forums for any length of time (I've been doing it for 4+ years), I hope that you've noted that some requesters will post multiple (sometimes into the 100s) requests for assistance, and invariably use the "…oh poor me, I don't know much about Access and will you please provide a response based on a macro, a query, or whatever." These folks would like you to believe that they are just incapable of learning anything and are entirely confused. And, at the end of every pay-period, they draw a paycheck, for being an Access programmer.

Have you noticed that while they present pitiful requests for assistance, those requests are always business based? When was the last time you saw someone asking for help with his or her stamp-collection? The point being, they are getting paid to program in Access! This is their job, for which they earn money. All of us, me included, occasionally run up against a brick-wall, and need someone to put us back on the right track.

That's what forums are all about. Conversely, they're not about helping people who aren't interested in helping themselves but rather look to leach off of other's expertise. Please tell us why you want to defend these parasites.

Look forward to hearing from you,

Bob
 
For anyone interested, here is the VBA solution to the original problem.
One form, three text boxes (StartDate, EndDate and WeekDayCount). All code goes in the form's module:

Option Compare Database
Option Explicit

'-----------------------------------------------------------------------
Public Function RetWeekDays(varStart As Date, varEnd As Date) As Long
Dim lngStart As Long
Dim lngEnd As Long
Dim lngDiff As Long
Dim lngCounter As Long
Dim lngChkDate As Long
Dim lngDayCount As Long

If varStart > 0 And varEnd > 0 Then

lngStart = Format(varStart, "#")
lngEnd = Format(varEnd, "#")

lngDiff = Abs(lngEnd - lngStart)

For lngCounter = 1 To lngDiff
lngChkDate = Format(lngStart + lngCounter, "w")
If (lngChkDate = 2) Or (lngChkDate = 3) Or (lngChkDate = 4) Or (lngChkDate = 5) Or (lngChkDate = 6) Then
lngDayCount = lngDayCount + 1
End If
Next

Else
lngDayCount = 0
End If

RetWeekDays = lngDayCount

End Function

'-----------------------------------------------------------------------
Private Sub EndDate_AfterUpdate()
StartDate_AfterUpdate
End Sub

'-----------------------------------------------------------------------
Private Sub StartDate_AfterUpdate()
WeekDayCount.Value = RetWeekDays(Nz(StartDate, 0), Nz(EndDate.Value, 0))
End Sub

'-----------------------------------------------------------------------


Good Day,
Darrin@CB69
 
Bob,

Your reply "That's what forums are all about. Conversely, they're not about helping people who aren't interested in helping themselves but rather look to leach off of other's expertise. Please tell us why you want to defend these parasites."

Have a look at http://www.access-programmers.co.uk...........

'Visit our established and growing MS Access community where you can ask questions, chat, get help and find out more about MS Access.'

Key words ........
ASK
GET HELP
FIND OUT MORE

When you registered for this forum, it did not specify that you had to be employed as an Access programmer (which I am not, obviously). If you want to control the parasite factor, may I suggest you start your own forum where parasites dare not enter. Because I am not the first and will not be the last one who innocently asks for help that does not know everything.
 
bob,

your last post perhaps should have been your first post.

it is well thought-out and addresses the problem. it further addresses the question of when to use queries or vba.

----you wrote---
I'll be the first to admit that my response was possibly unnecessarily harsh.
----------------

and that, largely, was the basis for my 'sticking my nose' into this thread. your response was, in fact, unecessarily harsh.

i did not view csr's question as a 'pitiful request for assistance'.
nor, do i see csr as a 'parasite'.

unloading on a person who has made only 4 posts to this board is over the top.

hope you were just having a 'bad day'
smile.gif


al

---------
csr,
welcome to the board,mate!
it's not usually this abusive for a new member... you just got lucky
smile.gif





[This message has been edited by pcs (edited 09-20-2001).]
 
Already knew you where French ,I just couldn't figure out country of residence from the times of your posts.Is Angola still turbulent?
 

Users who are viewing this thread

Back
Top Bottom