Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2016
    Posts
    4

    mpg calculation

    hi all
    I am building a database for our fleet of vehicles. One of the reports that I require is mpg calculation.
    I have a table "fuelreceipts" which contain
    [vehicle]
    [date]
    [odoreading]
    [price]
    [liters]
    I know mpg is calculated from the difference between odoreadings, but as the table contains several different vehicles, I don't know how to make a record look for the previous odoreading. Well, I could do it in a query PER vehicle, but as I need a monthly report to display all vehicles with their mpg's I am sure there is an easier way
    To make things even more challenging, some vehicles have their readings in miles....
    Of course I could give more table info, but not sure what is required.



    Can someone point me in the direction? ANY is much appreciated. Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    we use 2 fields on the record, ODstart, ODend to do it easily.
    But since you do not have this, it would have to be done programmatically thru VB.
    I would add an extra field, MPG (kpL?)
    build a query to sort the records by Veh, Date
    then run this code
    Code:
    Public Sub CalcMPG()
    Dim db As Database
    Dim rst   'As Recordset
    Dim qdf As QueryDef
    Dim vCurr, vPrev, vVeh, vDate, vOD, vGal, vMPG, vPrevOD, vPrevDte, vPrevV
    Dim sSql As String
    ' table "fuelreceipts" which contain
    '[vehicle]
    '[Date]
    '[odoreading]
    '[price]
    '[liters]
    
    DoCmd.SetWarnings False
    Set db = CurrentDb
    Set qdf = db.QueryDefs("qsFuelEstSort")  'sort by vehicle,date
    Set rst = qdf.OpenRecordset(dbOpenDynaset)
    vPrevDup = "*&%"
    
    With rst
        While Not .EOF
            vVeh = .Fields("vehicle")
            vDate = .Fields("date")
            vOD = .Fields("odoreading")
            vGal = .Fields("liters")
            
            If vVeh <> "" Then
                If vPrevV = vVeh Then
                    vMPG = (vOD - vPrevOD) / vGal
                           'update the previous record
                     sSql = "update FuelReceipts set [MPG]=" & vMPG & " where [vehicle]='" & vVeh & "' and [date]=#" & vPrevDte & "#"
                     DoCmd.RunSQL sSql
                End If
            End If
            vPrevV = vVeh
            vPrevOD = vOD
            
           .MoveNext
        Wend
    End With
    DoCmd.SetWarnings True
    
    Set qdf = Nothing
    Set rst = Nothing
    Set db = Nothing
    End Sub

  3. #3
    orange's Avatar
    orange is online now Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,724
    pvl,

    I think you would benefit from working through this free tutorial from RogersAccessLibrary.
    It will show you how to describe your "problem/opportunity" in business terms. And lead you through a consistent process to identify the things of interest, their attributes and the relationships between them based on your business. The things of interest become your tables and the attributes become fields. This approach will lead you to a blueprint of the database required to support your business.
    The tutorial will take about 30-45 minutes to work through, and you must do the work to get the benefit. But you will learn concepts that you can use with any database.
    The tutorial comes with supportive documentation and a solution.
    Good luck

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The story goes "Once upon a time, long, long ago........"
    I created a MPG calculator (one of my first dBs) in an effort to help know when to do maintenance (and to learn Access). If MPG dropped... time for tune up.

    Eventually I went back to a log book I keep in the truck. But it was a good learning experience.
    Maybe it will help you. I think only the first truck (1987) has test data.
    Attached Files Attached Files

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

Similar Threads

  1. Help with a calculation
    By Cdray in forum Programming
    Replies: 9
    Last Post: 06-26-2015, 09:37 AM
  2. How to do this calculation
    By rickroller5 in forum Access
    Replies: 4
    Last Post: 09-07-2014, 01:22 PM
  3. BMI calculation
    By puush in forum Programming
    Replies: 9
    Last Post: 08-19-2014, 05:05 AM
  4. Calculation Field in table vs Query calculation
    By Leonidsg in forum Database Design
    Replies: 18
    Last Post: 02-28-2013, 09:38 PM
  5. Age Calculation
    By bmiller310 in forum Access
    Replies: 13
    Last Post: 01-31-2012, 01:57 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