Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93

    Calculate time

    Hi Everyone, hoping to get some help.
    I have three fields, Time Received, Time Arrived, and Time Cleared. I need to be able to calculate the total amount of time in minutes. The fields are in military time.
    For example,
    Time Received 1300
    Time Arrived 1305
    Time Cleared 1330


    A total of thirty minutes right? So, how can I formulate Access to do that for me?
    Any help would be appreciated.
    Im using Access 2010.

    FunkyG

  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows XP Access 2003
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,540
    Take a look at the DateDiff() function in the help files
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    are your fields stored as time fields or are they stored as text or numeric?

    If they're time fields bob fitz's suggestion is the right place to look. If they're being stored as text or numeric you will likely have to run a conversion first for instance with the cdate function to change your string to a date then do the datediff function on it.

  4. #4
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93
    Hi, thanks for replying. They are currently stored as a numeric field. I want the end user to enter the information as numbers. Can you provide an example of the cdate function?

  5. #5
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    well, what happens if the time is 0030 and it's a number field it would just show as 30?

    what if it's 5 minutes after midnight would it just show as 5?

  6. #6
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93
    Ok, so I did some researching and I found a formula that I think could help me achieve this.
    Here it is;
    =(IF[TimeClearedResult]-[TimeReceivedResult]<0,[TimeClearedResult]+24*0.041666667-[TimeReceivedResult],[TimeClearedResult]-[TimeReceivedResult])*1440
    It works in an excel spreadsheet, so does anyone think this could work in VB?
    Thanks, FunkyG

  7. #7
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    if it works in excel it should work in access but you never did answer my previous question :P

    At any rate you should be more clear in your bracketing with formulas this part of the formula:

    [TimeClearedResult]+24*0.041666667-[TimeReceivedResult]

    Is going to perform 24*.041666667 first

    Then it will likely perform the addition second then the subtraction because the addition appears first

    What you likely want is this:

    ([TimeClearedResult]+(24*0.041666667))-[TimeReceivedResult]

    Where you're adding time to your cleared result to get a positive result though you could just as easily have

    ([TimeClearedResult]+1)-[TimeReceivedResult]

    Unless I've misinterpreted your bracketing altogether

  8. #8
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93
    OIk, I have the formula in the Control Source, but now my form is displaying the #NAME? error. I dont get why it is doing so.

    This is the formula I have;
    [Total Time=IF(TimeClearedResult]-[TimeReceivedResult]<0,([TimeClearedResult+(24*0.4666667))-[TimeReceivedResult],[TimeClearedResult]-[TimeReceivedResult]*1440]

    Total Time is the name of the field I need the result stored in once the calculation is done.

    Thanks for your help.
    FunkyG

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    =IF(TimeClearedResult]-[TimeReceivedResult]<0,([TimeClearedResult+(24*0.4666667))-[TimeReceivedResult],[TimeClearedResult]-[TimeReceivedResult]*1440

    is all you need, if the formula only appears in a text box on a form.

  10. #10
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93
    Hmm, ok. Let me give that a try. For some reason when I type the formula in to the control source, it doesn't save it. Would you advise using a unbound field, or a field from the property field pick list.?

  11. #11
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    you can't have a formula as the control source and have it named after a field in your record source.

    For instance if you have a field in your record source called MUFFINS and you name a second (unbound) textbox MUFFINS with a formula in it things are going to break. If you are going to use a formula as control source it has to be unbound.

  12. #12
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93
    OK, so if I need the result of this calculation to go into a table, do I still put it in an unbound control? I dont know why, but I am severely confused.
    Thanks, FunkyG

  13. #13
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    no, you never store a calculated value. There's no reason to, any time you want to show it on a report or query or form you can perform the calculation at the time the record is displayed. Storing calculated values is a really bad idea unless you have absolutely no other choice and I have only found it necessary in 1 or 2 instances when dealing with very large datasets

  14. #14
    funkygoorilla is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    93
    Ok, so I did some research and you are correct. Storing calculated values in a table is not recommended. With that put to bed, how do I get this formula to work? This is what I currently have;
    =(IIf([TimeClearedResult]-[TimeReceivedResult]<0,[TimeClearedResult]+24*0.041666667-[TimeReceivedResult],[TimeClearedResult]-[TimeReceivedResult]))*1440

    It does calculate, but somewhere in here I believe the brackets are wrong or something. I tried your suggestion earlier but that didn't work either. Any ideas?
    Thanks! FunkyG.

  15. #15
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    What is wrong with it, is it giving you an unexpected value or is it just giving you something like #error? in the text box it's in?

    Can you give an example of your data and what you expect your results to be. I'm assuming you want it to be in a number of minutes but you never did answer my question earlier about how your data is stored. For instance is 15 minutes after midnight stored as 0015 or is it stored as 15? is the field text or numeric? or is it set up as a time field?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calculate Interest
    By jgelpi16 in forum Queries
    Replies: 3
    Last Post: 08-19-2016, 08:01 PM
  2. Replies: 7
    Last Post: 08-04-2011, 07:49 PM
  3. Calculate Miles
    By rbiggs in forum Access
    Replies: 4
    Last Post: 06-30-2011, 01:03 PM
  4. Replies: 12
    Last Post: 02-22-2011, 03:39 PM
  5. Calculate Time and Display Records
    By w2vijay in forum Reports
    Replies: 1
    Last Post: 02-12-2010, 01:58 AM

Tags for this Thread

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