Search code examples
databasevb.netms-access

In Microsoft Access, how do I add time fields together and then output the total as 2 hours and 45 minutes as opposed to 2.75 hours.?


I'm trying to work out the total amount of time spent working, with multiple (up to 3) start and finish times in a day: Start Time 1, Finish Time 1, Start Time 2, Finish Time 2, Start Time 3, Finish Time 3.

The first problem I'm having is with calculating the total time, Start Time 1 and Finish Time 1 will always have an entry, but the other fields will not always be populated, so when I add these together unless every field has an entry my addition calculation will not work. How do I add these together with the empty cells being added as a 0?

Secondly, how do I display the total time as x hours and y minutes, instead of hours with a decimal place? (Example: 2 hours and 45 minutes, instead of 2.75 hours).

Thanks in advance.

Time total query with empty cells


Solution

  • In Access, it could be (the ControlSource of the textbox):

    = CDate(Nz(CVDate([Finish Time]), #00:00#) - Nz(CVDate([Start Time]), #00:00#) + _
        Nz(CVDate([Finish Time 2]), #00:00#) - Nz(CVDate([Start Time 2]), #00:00#) +  _
        Nz(CVDate([Finish Time 3]), #00:00#) - Nz(CVDate([Start Time 3]), #00:00#))
    

    Apply the Format h:nn to the textbox where you display the total time.