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