Results 1 to 14 of 14
  1. #1
    HMac is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    6

    How to automatically populate start mileage from previous record end mileage.

    Access 2010 Novice! I have 2 tables - Daily Trip Log and Daily Trip Log Stops. Daily Trip Log Fields are dtlID (Autonumber), departdate, departtime, departmiles, arrivetime, arrivemiles, totalmiles, and totaldrivinghours. Daily Trip Log Stop Fields are dtlID (Autonumber), date, reasonforstop, stoptime, mileage, placestopped, and departtime.



    A driver can stop many times during the day for deliveries, lunch, fuel, etc.

    My Form is Daily Trip Log with a subForm Daily Trip Log Stops.

    When a driver enters the departdate on the primary form I would like the departmiles to automatically populate using the previous days arrivemiles. I have tried DLast to no avail.

    Total time is arrivalmiles minus departmiles and totaldrivinghours is arrivetime minus departtime. In addition to this I would like to know how to add the numerous days stops enroute (departtime minus stoptime) to then be deducted from the daily total driving hours.

    Everything works fine except having the departmiles on the new record reflect the arrivemiles from the previous day and how to total the amount of stopped time for all the stops during the day.

    Thank you in advance.

  2. #2
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Cant you do it with a Dlookup ?
    Code:
    Private Sub departdate_AfterUpdate()
    Me.departmiles = Dlookup ("Arrivemiles", "[Daily Trip Log]")
    If there are several drivers (or cars) youll have to include a WHERE clause in the Dlookup so Access knows from wich record to extract the Arrivemiles from. The WHERE clause in this case should be a personell or licenceplate number for example.

  3. #3
    HMac is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    6
    Thank you JeroenMioch. Dlookup works except it always takes me to the first record in the table instead of reading the arrivemiles in the previous record. Could you explain how to work around that please.

  4. #4
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Youll have to specify wich record access needs to select for you

    Code:
    Me.departmiles = Dlookup ("Arrivemiles", "[Daily Trip Log]", "LicenceplateNumber = me.YourLicencePlateNumberControl")
    In above code. Access will fill the departedmiles control of your form, with the value it finds in the Daily Trip Log table, arrivedmiles field WHERE the licenceplate field is YourLicencePlateNumberField on your form.

    With no WHERE clause, access just returns the first record it finds

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Quote Originally Posted by JeroenMioch View Post
    Youll have to specify wich record access needs to select for you

    Code:
    Me.departmiles = Dlookup ("Arrivemiles", "[Daily Trip Log]", "LicenceplateNumber = me.YourLicencePlateNumberControl")
    That would get the first record for that vehicle, which would likely not be the previous day's record.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Quote Originally Posted by pbaldy View Post
    That would get the first record for that vehicle, which would likely not be the previous day's record.
    You might have a valid point there mr. Baldy ^^

    Still not that difficult though. Unfortunately im not at work anymore and i dont have Access at home.
    So feel free to help out here.
    Else
    I work it out when back at work tomorrow

  7. #7
    HMac is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    6
    Thanks but that will not work as I need the previous day's record. The arrivemiles at the end of the day is what I want to use populate the new record departmiles field the next day. I have found some code but cannot get it to work. My field dtlID is an Autonumber field. The code example says it will look at sequential numeric fields and is shown below:
    Private Sub dtlDate_AfterUpdate()
    Me.dtlDepartMileage = DLookup("dtlArriveMileage", "[Daily Trip Log]", "[dtlID]=Forms![Daily Trip Log]![dtlID]-1")
    End Sub
    After I enter the date in the dtlDate field I get the following: Run Time Error '3075'. Invalid use of '.', '!', or '()' in query expression '[dtlID] = Forms! [Daily Trip Log] ! [dltID- '.
    Can you please give me the correct statement to make this work. The error has something to do following the "Table" portion of the code but I don't know what or how to correct it.
    Thank you.

  8. #8
    JeroenMioch's Avatar
    JeroenMioch is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2012
    Location
    Den Haag, Netherlands
    Posts
    368
    Normally i check these kind of codes with examples within my databases. But im not at work momentarely so i cant do that now.

    What you can do is try this :

    Code:
    Me.dtlDepartMileage = DLookup("dtlArriveMileage", "[Daily Trip Log]", "([dtlID] = me.dtlID)-1")
    
    Its aircode, but worth a shot..

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I might try a DMax() on the ending mileage field with a criteria on the vehicle. Presuming you enter in date order, I'd think that would work.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    HMac is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    6
    Jeroen the "Aircode" you provided does not work. Pbaldy could you please explain how to use Dlookup and DMax together. I only have 15 records of test data and as you know Autonumber is sequential/numeric and the trips are entered in date order with dtlArriveMileage field in the last record has the highest numeric value and that is the value I would like to be the dtlDepartMileage value when I start a new record. Thank you.

  11. #11
    HMac is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    6
    Jeroen I do not get an error using your aircode it just does not populate the dtlDepartMileage field in the new record.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    I wasn't suggesting using them together, I was suggesting using DMax() alone. As long as you enter in date order, the maximum ending mileage in the table would be the last one, thus the beginning mileage for the vehicle being entered.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    HMac is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Dec 2011
    Posts
    6
    Thank you pbaldy. That works and solved the problem. Below is the code:
    Private Sub dtlDate_Change()
    Me.dtlDepartMileage = DMax("[dtlArriveMileage]", "Daily Trip Log")
    End Sub
    I used the date change on the new record so when I click in the dtlDepartMileage field it automatically populates from the last dtlArriveMileage.
    Thanks again for your help.

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    No problem. I take it there's only 1 vehicle being tracked? If not, that will give you the max mileage overall, not for a given vehicle. I would expect a criteria on vehicle ID.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Full App - Mileage/Timesheet Example
    By pkstormy in forum Sample Databases
    Replies: 9
    Last Post: 05-27-2015, 03:37 PM
  2. Vehicle Mileage Database
    By Mtyetti2 in forum Access
    Replies: 6
    Last Post: 10-07-2013, 01:50 PM
  3. Vehicle Mileage
    By Mtyetti2 in forum Forms
    Replies: 5
    Last Post: 02-16-2013, 08:23 AM
  4. Mileage App
    By neo651 in forum Access
    Replies: 1
    Last Post: 06-01-2012, 03:29 PM
  5. Database for Mileage Reports
    By luvsmel in forum Database Design
    Replies: 1
    Last Post: 03-21-2012, 10:07 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