Results 1 to 5 of 5
  1. #1
    Mark256 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    5

    Subtracting Hours

    Hi

    I have a query with two calculated fields that return the business hours between two sets of dates in the format "h:nn".

    What I would like to do is subtract the first field [HoursA] from the second [HoursB], but the expression [HoursA] - [HoursB] doesn't work like it would in excel. I have also tried DateDiff("n", [HoursA], [HoursB]), but this only works if either field is less than 24 hours.

    Does anyone have a expression for subtracting these calculated fields e.g. 20:00 - 5:00 = 15:00 hours?

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Need to convert the h:nn to decimal hours. So:

    20:25 = 20.42

    5:12 = 5.2

    20.42 - 5.2 = 15.22 = 15:13

    Review http://en.kioskea.net/faq/9089-vba-v...cimal-to-hours

    Don't have to use VBA. The calcs can be done by expression in query. Instead of the Split() function from the first procedure, use other string functions like Left, Mid, InStr, Len to break up the time value.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Mark256 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    5
    Thanks June7, but do you know how I can use this as an expression in the query?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Like any expression in query.

    Val([HoursA]) + Mid([HoursA], InStr([HoursA],":")+1, 2) / 60
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Mark256 is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    5
    Quote Originally Posted by June7 View Post
    Like any expression in query.

    Val([HoursA]) + Mid([HoursA], InStr([HoursA],":")+1, 2) / 60
    Thank you June7!

    I used this expression for the switch back to hours: CInt([yourfield]) & ":" & Format([yourfield] * 60 MOD 60, "00")

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subtracting two records
    By laipeka in forum Queries
    Replies: 2
    Last Post: 08-30-2012, 04:12 AM
  2. Replies: 1
    Last Post: 08-08-2012, 01:42 PM
  3. Replies: 4
    Last Post: 08-06-2012, 10:25 AM
  4. Adding/Subtracting
    By JayX in forum Programming
    Replies: 3
    Last Post: 12-15-2011, 03:13 PM
  5. Adding/Subtracting
    By JayX in forum Access
    Replies: 1
    Last Post: 12-15-2011, 01:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums