A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Setting Up & Running Reports
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

Trying to calculate difference between two times in an access repo



 
 
Thread Tools Display Modes
  #1  
Old June 6th, 2010, 07:26 PM posted to microsoft.public.access.reports
jackie
external usenet poster
 
Posts: 216
Default Trying to calculate difference between two times in an access repo

I am trying to calculate the difference between two times in an access report
and don't know what the correct expression is.
My report has a column of "Start" and "End" that has the start time of the
project and the end time of the project. I need the difference between the 2
so I created an expression for "Total production time" -
=timediff("h",[end],[start]) but a box keeps appearing when I try to view the
report which means I have an error and I don't know where I went wrong
  #2  
Old June 6th, 2010, 08:39 PM posted to microsoft.public.access.reports
Duane Hookom
external usenet poster
 
Posts: 7,177
Default Trying to calculate difference between two times in an access repo

Try the DateDiff() function. "h" might not produce the desired results. I
would try:
=DateDiff("n",[end],[start])/60
n is for minutes.

--
Duane Hookom
Microsoft Access MVP

NOTE: These public News Groups are ending June 1st. Consider asking
questions at http://social.answers.microsoft.com/...ddbuz/threads?


"jackie" wrote:

I am trying to calculate the difference between two times in an access report
and don't know what the correct expression is.
My report has a column of "Start" and "End" that has the start time of the
project and the end time of the project. I need the difference between the 2
so I created an expression for "Total production time" -
=timediff("h",[end],[start]) but a box keeps appearing when I try to view the
report which means I have an error and I don't know where I went wrong

  #3  
Old June 7th, 2010, 12:30 AM posted to microsoft.public.access.reports
KenSheridan via AccessMonster.com
external usenet poster
 
Posts: 1,610
Default Trying to calculate difference between two times in an access repo

You might also like to try the following function, which returns the time
difference as hours:minutes:seconds:

Public Function TimeDuration(dtmFrom As Date, dtmTo As Date, _
Optional blnShowdays As Boolean = False) As String

' Returns duration between two date/time values
' in format hh:nn:ss, or d:hh:nn:ss if optional
' blnShowDays argument is True.

' If 'time values' only passed into function and
' 'from' time if later than 'to' time, assumed that
' this relates to a 'shift' spanning midnight and one day
' is therefore subtracted from 'from' time

Dim dtmTime As Date
Dim lngDays As Long
Dim strDays As String
Dim strHours As String

' subtract one day from 'from' time if later than 'to' time
If dtmTo dtmFrom Then
If Int(dtmFrom) + Int(dtmTo) = 0 Then
dtmFrom = dtmFrom - 1
End If
End If

' get duration as date time data type
dtmTime = dtmTo - dtmFrom

' get whole days
lngDays = Int(dtmTime)
strDays = CStr(lngDays)
' get hours
strHours = Format(dtmTime, "hh")

If blnShowdays Then
TimeDuration = lngDays & ":" & strHours & Format(dtmTime, ":nn:ss")
Else
TimeDuration = Format((Val(strDays) * 24) + Val(strHours), "00") & _
Format(dtmTime, ":nn:ss")
End If

End Function

Add it to a standard module and call it in your report with:

=TimeDuration([start],[end])

The principle thing about this function is that it will also correctly
compute the difference between times which span midnight where these have
been entered without any dates, provided that the times are less than 24
hours apart. Normally this would give the wrong answer as 8.00 PM is later
than 4.00 AM for instance.

For long durations between date/time values you can opt to return days:hours:
minutes:seconds if you wish by including the third optional argument:

=TimeDuration([start],[end],True)

Note that the function returns a string. If you need to aggregate a set of
time differences you can't sum the return value of the function for instance,
but you can return the difference as date/time value with this function:

Public Function TimeDurationAsDate(dtmFrom As Date, dtmTo As Date) As Date

' Returns duration between two date/time values
' as a date/time value

' If 'time values' only passed into function and
' 'from' time if later than 'to' time, assumed that
' this relates to a 'shift' spanning midnight and one day
' is therefore subtracted from 'from' time

' subtract one day from 'from' time if later than 'to' time
If dtmTo dtmFrom Then
If Int(dtmFrom) + Int(dtmTo) = 0 Then
dtmFrom = dtmFrom - 1
End If
End If

' get duration as date time data type
TimeDurationAsDate = dtmTo - dtmFrom

End Function

So you can sum the durations, in a group footer in a report for instance:

=Sum(TimeDurationAsDate([start],[end]))

Summing date/time values will not give you a result in hours;minutes;seconds
format, but as a decimal number of days, however, so you need to convert the
result with another function:

Public Function TimeToString(dtmTime As Date, _
Optional blnShowdays As Boolean = False) As String

Dim lngDays As Long
Dim strDays As String
Dim strHours As String

' get whole days
lngDays = Int(dtmTime)
strDays = CStr(lngDays)
' get hours
strHours = Format(dtmTime, "hh")

If blnShowdays Then
TimeToString = lngDays & ":" & strHours & Format(dtmTime, ":nn:ss")
Else
TimeToString = Format((Val(strDays) * 24) + Val(strHours), "00") & _
Format(dtmTime, ":nn:ss")
End If

End Function

So for a control to sum the differences and return the result as hours:
minutes;seconds you'd use:

=TimeToString(Sum(TimeDurationAsDate([start],[end])))

Ken Sheridan
Stafford, England

jackie wrote:
I am trying to calculate the difference between two times in an access report
and don't know what the correct expression is.
My report has a column of "Start" and "End" that has the start time of the
project and the end time of the project. I need the difference between the 2
so I created an expression for "Total production time" -
=timediff("h",[end],[start]) but a box keeps appearing when I try to view the
report which means I have an error and I don't know where I went wrong


--
Message posted via http://www.accessmonster.com

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 11:16 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.