Calculating difference between 2 times - 24/military time (1 Viewer)

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
I have a query that calculates the elapsed time between [StartTime] and [EndTime], both stored as text in the 24 hour/military time format. This is what I am currently using but it only returns a whole number, such as 1 or 2, but ignores the minutes, so if it should be 1:30 (1.5) is just shows as 1. This is what I am using:

Elapsed: Val(Left(Format(TimeValue(Format(TimeValue(Left([starttime],2) & ":00"),"Short Time"))-1-TimeValue(Format(TimeValue(Left([Endtime],2) & ":00"),"Short Time")),"Short Time"),2))
 

plog

Banishment Pending
Local time
Yesterday, 20:31
Joined
May 11, 2011
Messages
11,643
both stored as text

Ugh, why? Store it as a Date/Time and this is trivial. You would simply use the DateDiff function.

Barring that, I you have too much going on in that algorithm to do it all in one line, time for a function in a module. You pass it your two values, it converts, does math, formats and returns your value. Better yet, it allows you to easily debug as you do it so you can see what is happening operation by operation.

But yeah, store that as a Date/Time.
 

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
I would love to, but it is external ODBC data being linked to Access, is there a way to convert the field to date/time in a query and then use DateDif?
 

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
I am having some trouble using TimeValue - I am putting the following in the "Field" : Expr1: TimeValue([StartTime])
 

plog

Banishment Pending
Local time
Yesterday, 20:31
Joined
May 11, 2011
Messages
11,643
define "trouble". Error message, no results, unexpected results?
 

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
Yes sorry, I should have specified, I tried it in the "EndTime" field and get #Error" in the "Expr1" column.
 

plog

Banishment Pending
Local time
Yesterday, 20:31
Joined
May 11, 2011
Messages
11,643
So what was the value of EndTime?
 

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
Here is the SQL from the query - I'm very new to access and usually build queries in the design view -

SELECT IMCDate([AssignDate]) AS [DATE], OfficerAttendance.AssignCode, OfficerAttendance.DutyCode, PersonnelFileInfo.LastName, PersonnelFileInfo.FirstName, OfficerAttendance.StartTime, TimeValue([EndTime]) AS Expr1, OfficerAttendance.VarPortion1, OfficerAttendance.VarPortion2
FROM OfficerAttendance INNER JOIN PersonnelFileInfo ON OfficerAttendance.ID = PersonnelFileInfo.ID
WHERE (((IMCDate([AssignDate]))>=[Start(mm/dd/yyyy)] And (IMCDate([AssignDate]))<=[End(mm/dd/yyyy)?]) AND ((OfficerAttendance.AssignCode)="E8" Or (OfficerAttendance.AssignCode)="E1" Or (OfficerAttendance.AssignCode)="EO") AND ((OfficerAttendance.Division)="HI"))
ORDER BY IMCDate([AssignDate]);
 

plog

Banishment Pending
Local time
Yesterday, 20:31
Joined
May 11, 2011
Messages
11,643
Nope. TimeValue([EndTime]) caused on error. What was the value of [EndTime]?
 

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
The EndTime field varies depending on the rest of the query, for example in the one I am trying to pull it gives 1600 if I just have the "EndTime" value
 

plog

Banishment Pending
Local time
Yesterday, 20:31
Joined
May 11, 2011
Messages
11,643
"1600" isn't a valid value that TimeValue can accept ("1600:00" is though). So, now its time to write that function I talked about or use string fucntions (Mid, Left, Right) to make your values conform to what TimeValue expects.
 

JonesD

Registered User.
Local time
Yesterday, 21:31
Joined
Dec 4, 2018
Messages
11
I apologize, I'm definitely a new user - I thought I was using TimeValue correctly and adding the :00 in my original function:

Val(Left(Format(TimeValue(Format(TimeValue(Left([starttime],2) & ":00"),"Short Time"))-1-TimeValue(Format(TimeValue(Left([Endtime],2) & ":00"),"Short Time")),"Short Time"),2))
 

Users who are viewing this thread

Top Bottom