From: jackie on
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
From: Duane Hookom on
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/Forums/en-US/addbuz/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
From: KenSheridan via AccessMonster.com on
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