1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: MS Access modifying a date and time calc in separate fields

Discussion in 'Business Applications' started by Gram123, Jan 6, 2012.

Thread Status:
Not open for further replies.
Advertisement
  1. Gram123

    Gram123 Thread Starter

    Joined:
    Mar 15, 2001
    Messages:
    1,829
    Hi there,
    I have an MS Access database (2000, but primarily operating on PC with Access 2010). The database logs wagons coming to and from our site. Mostly, these wagons are in and out in the same day, but it is possible for them to park up overnight, for which we charge a small fee.

    The situation has now changed slightly, in that we now accept wagons staying on site for longer periods (usually a weekend), so I've been asked to introduce an Exit Date field as well, and correct the existing calcualtion of time on spent site.

    Currently, the following fields appear in the main table (called Stays):

    Arrival Date
    Arrival Time
    Exit Time


    There is also a field called Duration, which (for some reason) is autofilled by an Update Query, with the Update To value set to:

    IIF([Arrival Time]>[Exit Time],1440+DateDiff("n",[Arrival Time],[Exit Time]),DateDiff("n",[Arrival Time],[Exit Time]))​


    This is clearly inaccurate - it assumes that if a wagon's exit time is earlier in the day than its arrival time, then it must have stayed for 1440 minutes + the (negative) difference between the two times, and if the exit time is later or equal to the arrival time, the wagons has been in and out of site in the same day.

    As wagons could be on site for multiple days (so the 1440 mins figure could be wrong) and could leave x amount of days plus x amount of minutes after arriving (so the assumption that it's exited the same day as it arrived could be wrong), I need to take the Arrival Date and Exit Date into account within this calculation.
     
  2. Rockn

    Rockn

    Joined:
    Jul 29, 2001
    Messages:
    21,334
    You need to have a function that will break it out into hours, days and minutes. Personally I would not have any calculated fields in a table and only do it on reports and forms.
     
  3. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Hello Gram, as the vehicle can stay for a weekend you need to add an Exit date field to count the number of days.
    You also need some validation (VBA?) to test that the exit date is equals/later than the Arrival date and if the dates are equal that the exit time is greater than the arrival time.
    If the dates are not equal and the exit time is less than the arrival time you will need to subtract a day from the duration.
    The other alternative is to use 2 Date/Time fields that capture the date & time in the 1 field.
     
  4. Gram123

    Gram123 Thread Starter

    Joined:
    Mar 15, 2001
    Messages:
    1,829
    Other things got in the way of this task, so it's been a while...

    So, I'm thinking I could do this with IIF statements in a query, rather than VBA. I've added the Exit Date field.

    Examples of the possible scenarios:

    1) Wagon arrives on Day 1 at 5am and exits on Day 1 at 10am:
    If Exit Date - Arrival Date = 0, Exit Time - Arrival Time
    = 300 minutes
    / 60 = 5hrs

    2) Wagon arrives on Day 1 at 5am and exits on Day 2 at 10am:
    If Exit Date - Arrival Date >= 1, (Exit Date - Arrival Date)*1440 + Exit Time - Arrival Time
    = 1 * 1440 = 1440
    + 300 mins = 1740
    / 60 = 29 hrs

    3) Wagon arrives on Day 1 at 10am and exits on Day 2 at 5am
    If Exit Date - Arrival Date >= 1, (Exit Date - Arrival Date)*1440 + Exit Time - Arrival Time
    = 1 * 1440 = 1440
    + -300 mins = 1140
    /60 = 19 hrs

    4) Wagon arrives on Day1 at 5am and exits on Day 4 at 10am
    If Exit Date - Arrival Date >= 1, (Exit Date - Arrival Date)*1440 + Exit Time - Arrival Time
    = 3 * 1440 = 4320
    + 300 mins = 4620
    / 60 = 77 hrs

    5) Wagon arrives on Day 1 at 10am and exits on Day 4 at 5am
    If Exit Date - Arrival Date >= 1, (Exit Date - Arrival Date)*1440 + Exit Time - Arrival Time
    = 3 * 1440 = 4320
    + -300 mins = 4020
    /60 = 67 hrs

    6) User error entering Exit Date that is less than Arrival Date,
    Else MsgBox "Yay! You've invented Time Travel! Well done, have a biscuit."


    Unless I'm missing something, as long as am-pm / 24hr clock is taken into account in the calc, it looks like a fairly simple nested IIF statement, as scenarios 2-5 should all use the same calc...
     
  5. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Have you tried it?
     
  6. Gram123

    Gram123 Thread Starter

    Joined:
    Mar 15, 2001
    Messages:
    1,829
    Just tried:

    TimeOnSite: IIf([ExitDate]<[Arrival Date],0,IIf([ExitDate]-[Arrival Date]=0 And [Arrival Time]>[Exit Time],0,IIf([ExitDate]-[Arrival Date]=0,DateDiff("n",[Arrival Time],[Exit Time]),IIf([ExitDate]-[Arrival Date]>=1,([ExitDate]-[Arrival Date])*1440+DateDiff("n",[Arrival Time],[Exit Time])))

    It might be slightly messy, but it seems to work ok, as long as the IIf checking for 0 days + negative time appears ahead of the IIf for 0 days + positive time. I'll probably stick an extra field on the report that divides the resulting figures by 1440 to 2 dp so the user can quickly see how long in days the wagons were on site.

    There's a couple of things left I need to do:

    - At present, if a wagon appears to be on site for negative days or mins, I've set the above to display zero in the query / report. I could do with getting a message box to pop up to alert the user if he enters an exit date or time into the form that would make it look like the wagon had been on site for negative time.

    - I'd also like to auto-enter "today's" date when the user enters the exit time on the form.

    Both should be pretty straightforward.
     
  7. OBP

    OBP

    Joined:
    Mar 8, 2005
    Messages:
    19,895
    Yes they are all straightforward.
     
  8. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1034859

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice