941,498 Members | Top Members by Rank

Ad:
Jul 16th, 2006
0

Difference between 2 dates by month

Expand Post »
Hi, I have an access database that I use for recording the sickness of my employees. One of the tables holds the dates, Start Date, End Date and Count of Days etc. I need to produce a query that will show me a count of all of these dates between two date parameters that I will input when the report is run. However, to complicate matters, I need a definitive count even if the end date runs over the end date of my query.

For example....

I am employee X and I am sick between...

Start_Date = 01/07/2006
End_Date = 03/08/2006

If I wanted to run a report on this employee I would type in a start date and end date of 01/07/2006 to 31/07/2006 (i.e. the calendar month). This would need to take into account that the employee's end date is not in July. Presently, this doesn't happen unless the employee is off sick and returned within my parameter query.

Any idea how I can do this? It is driving me nuts, particularly as I often need to report on absences for a calendar month.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChinDave is offline Offline
3 posts
since Jul 2006
Jul 16th, 2006
0

Re: Difference between 2 dates by month

Search on start date only.

So your criteria for Start_Date would be: Between #01/07/2006 And #31/07/2006
Last edited by hollystyles; Jul 16th, 2006 at 4:26 pm.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jul 16th, 2006
0

Re: Difference between 2 dates by month

Quote originally posted by hollystyles ...
Search on start date only.

So your criteria for Start_Date would be: Between #01/07/2006 And #31/07/2006
Not exactly, I could have somebody off say from 25/06/06 to 10/08/06, but I would like a query / report that shows me how many days were lost between a given date of my choosing, i.e. 01/07/06 to 30/07/06. The calculation would therefore need to count the number of days lost for this person, taking into consideration that the start date and end date are both before and after the date range of my query.

I need this because I often have to report the number of days lost per month, without any consideration for when the person actually went off sick or returned. For example...

Sick from 26/06/06 to 07/07/06, my query asks for a count of days lost between 01/07/06 and 30/07/06 for this person, it would then come back as 5 working days lost. In order words, it would be a definitive count of the number of days lost, regardless of when the person went off sick or returned.

!!! You can see why it is driving me nuts. All I can do at the moment is get it to calculate the number of days between two dates if both the start date and end date fall within my query range, which is unlikely.

Thanks. David.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChinDave is offline Offline
3 posts
since Jul 2006
Jul 16th, 2006
0

Re: Difference between 2 dates by month

Hmn.... are you/can you use a macro or the VB Editor? If so, you could probably build a macro for this... So, what I mean is, you could have the Query return the entire time off, and then have a macro get the number of days between your two parameters, right?
Last edited by Comatose; Jul 16th, 2006 at 10:47 pm.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Jul 17th, 2006
0

Re: Difference between 2 dates by month

Quote originally posted by Comatose ...
Hmn.... are you/can you use a macro or the VB Editor? If so, you could probably build a macro for this... So, what I mean is, you could have the Query return the entire time off, and then have a macro get the number of days between your two parameters, right?
Yep. I can use VB and Macro's but am not very good with the code. Any idea what type of code I would need for this? If so, I could probably alter the name of the database tables etc to accomodate.

Many thanks.

David.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ChinDave is offline Offline
3 posts
since Jul 2006
Jul 17th, 2006
0

Re: Difference between 2 dates by month

Here is a Pretty quick piece of code I crufted up....
' /* Declare Our Variables */
Dim d1
Dim d2
Dim RetUnit
Dim Diff

' /* Try To Get The Starting Date */
d1 = InputBox("Please Enter The Starting Date")
If d1 = "" Then Exit Sub

' /* Try To Get The Ending Date */
d2 = InputBox("Please Enter The Ending Date")
If d2 = "" Then Exit Sub

' /* Try To Get The Interval Unit To Return (Days Off, Weeks Off, etc) */
RetUnit = InputBox("Day, Week, Month, or Year?")
If RetUnit = "" Then
    Exit Sub
Else
    ' /* Set The Interval Code Based On The Users Selection */
    If lcase(RetUnit) = "day" Then
        RetUnit = "d"
    ElseIf lcase(RetUnit) = "week" Then
        RetUnit = "ww"
    ElseIf lcase(RetUnit) = "month" Then
        RetUnit = "m"
    Else
        RetUnit = "yyyy"
    End If
End If

' /* Calculate The Difference Between Date1, and Date2, With The Chosen Interval */
Diff = DateDiff(RetUnit, d1, d2)

' /* Show The User */
MsgBox "The Difference Is " & Diff
Naturally, I would imagine that you are going to want to change things so that it's a little more friendly to you, I'm not sure.... but the magic behind how it works is the "DateDiff" function. The DateDiff function calculates the difference between 2 dates, but it has to know what interval it should use..... do you want to know how many months, days, years, etc? So, that's the first piece of info you have to give datediff. In The Example above, we use a variable (RetUnit) that will contain that information based on what you type in the last popup box. So, if you want it in Days, it will automatically use DateDiff with "d" as the first parameter. If you know that you will only ever use this with Days, Then you could simply change the Line Diff = DateDiff(RetUnit, d1, d2) to read Diff = DateDiff("d", d1, d2) and it will return the value in Days Only (Always). Here Is A Complete List Of Those Interval Codes:
yyyy  	Year
q 	Quarter
m 	Month
y 	Day of year
d 	Day
w 	Weekday
ww 	Week
h 	Hour
n 	Minute
s 	Second
Also, another little Gem that I'd thought I'd throw into the mix here, is that the DateDiff function can be used in a Query.... Uh Oh. I don't do much with Queries, and you may have to modify for a different version of access or whatever (not sure), but I googled around and found the image attached below. I'm not sure if it would be easier for you to use the code in a macro or module, or just modify your Query to look something like the attachment, but either way, let me know how it turns out.
Attached Thumbnails
Click image for larger version

Name:	datediff001.png
Views:	131
Size:	8.4 KB
ID:	2179  
Last edited by Comatose; Jul 17th, 2006 at 6:41 am.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS Access and FileMaker Pro Forum Timeline: shortcut on client to connect to remote database filemaker
Next Thread in MS Access and FileMaker Pro Forum Timeline: foxpro help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC