Best Format to record time used in calc

message from JB on 19 May 2004
What is the best way to capture time so that it's
captured/entered as HH:MM:SS but it will be used in a
calculation such as: [Mass]/[RunTime]
 
John Vinson replied to JB on 19 May 2004
I'd use Long Integer seconds, with some simple VBA code to parse out
hours and minutes into seconds. The simplest way would be to have four
textboxes on a form, for hours, minutes, seconds and an (invisible)
textbox bound to the duration field. You'ld need code in the
afterupdate events of the three textboxes to add up the duration, and
in the Form's Current event to parse the duration out. Post back if
you need help with the code.

John W. Vinson[MVP]
Come for live chats every Tuesday and Thursday
http://go.compuserve.com/msdevapps?loc=us&access=public
 
JB replied to John Vinson on 20 May 2004
Yes, I would very much appreciate help with the code.
Thank you.

to parse out
to have four
(invisible)
the
duration, and
Post back if
 
John Vinson replied to JB on 20 May 2004
Ok, let's say we have textboxes txtHr, txtMin, txtSec and txtDuration;
the first three are unbound, the last is bound to a Long Integer field
named Duration (which you'll use for your calculations).

In the AfterUpdate event of each unbound textbox put:

Private Sub txtHr_AfterUpdate()
txtDuration = Me!txtHr*3600 + Me!txtMin*60 + Me!txtSec
End Sub

and similarly for txtMin and txtSec. If any of the fields is NULL the
duration will be NULL - so you may want to set the Default property of
the textboxes to 0 so the user can enter (say) 2 hours and get a
correct result.

Then in the Form's Current event:

Private Sub Form_Current()
If Not IsNull(Me![Duration]) Then
Me!txtHr = Me![Duration] \ 3600
Me!txtMin = Me![Duration] \ 60 MOD 60
Me!txtSec = Me![Duration] MOD 60
End If
End Sub

Error trapping, validation of field values (i.e. preventing the user
from entering 125 in the Min textbox) etc. is up to you...

John W. Vinson[MVP]
Come for live chats every Tuesday and Thursday
http://go.compuserve.com/msdevapps?loc=us&access=public
 
JB replied to John Vinson on 20 May 2004
Thank you very much. With a few tweaks to fit my purpose,
it works like a charm.

txtDuration;
Integer field
is NULL the
Default property of
and get a
preventing the user
 

Archived message: Best Format to record time used in calc (MS Access Error Message)