Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Calculate MPG in MS Access
Raimundo Torres 
    
4 years ago
Hello,
I have a Gas Expense table (see fields below) that I would like to calculate the MPG, but in order to perform the calculation I have to get the previous records miles, except for the 1st record which it has to be 0.

I think the calculation is like:
MPG = (current record Miles  previous record Miles) / current record TotalGallons.

I dont know how to execute and perform the calculation on the continuous form?

Use a button or double-click on the MPG field?

Please Help. Thanks

GasExpense table:
GasExpenseID autonumber and primary key
TotalGallonsnumber
Milesnumber
MPGnumber

Note: other fields omitted for simplicity.
Adam Schwanz  @Reply  
           
4 years ago
Dim PrevMiles
PrevMiles = DMAX("Mileage","Table","Mileage<" & MileageField)
MPG=(MileageField-PrevMiles)/TotalGallonsField

You will need to add more to the criteria like the vehichle ID or something to uniquely identify it if you do multiple vehicles. Otherwise this will take your Mileage field and subtract the biggest number that is below that number (which would be your previous mileage record), then divide by the gallons.
Raimundo Torres OP  @Reply  
    
4 years ago
Thank you so much, it help Immensely.
Scott Axton  @Reply  
        
4 years ago
Check out the Subforms video as well.  Richard shows an example of Mileage Tracking in that too.
Paul Kiener  @Reply  
      
4 years ago
Adam Schwanz, I am following this thread with interest since I have been trying to figure this out for myself.  My question is, where do you place the "Dim PrevMiles....." code?  Also, what is meant by "MileageField"?  Would this be the same as "Mileage"?  (In my database, I am using the label "Odometer", which refers to the what the odometer reads at the time of gas fillup.  I think that in your example, I would substitute "Odometer" for "Mileage", correct?)

Sorry for all the questions, but I'm still very new to this.  Thank you for your help.
Adam Schwanz  @Reply  
           
4 years ago
MileageField would be the name of your field that you put the Mileage in yes, whether that be Mileage or Odometer or whatever you called it.

You could put it in a lot of places, I guess the simplest would be to make a button and put that inside the code editor in the on click event. Again with the second part, MPG assumes that MPG is the name of the field on your form that you want the result to go into, if yours is called MilesPerGallon that would be MilesPerGallon=(YourMileageField-PrevMiles/YourGallonsField)

This only works with one car, if you have multiple cars you need to add additional criteria to the DMAX statement to get the unique values for the car you're working on.
Paul Kiener  @Reply  
      
4 years ago
Thank you.  Will give it a try.  I appreciate you getting back to me so quickly.
Paul Kiener  @Reply  
      
4 years ago
A follow-up, as it seems to be working for me.  I created a form, based on a query for gas fill-up.  On the form, I have a lookup box which selects the proper vehicle.  That lookup is the criteria in the query so I only have the gas data for that particular vehicle.  Back on the form, there is a subform which displays the data from the query.  I then have added unbound text box on the subform where I placed:  =DMax("Odometer","qryGas","Odometer<" & [Odometer])  in the Control source.  For me, this is working perfectly and when I go to enter a new "fill-up", the proper value is displayed automatically.  I also have a 2nd unbound text box where I calculate the MPG, also working correctly.

Again, thank you for your suggestion regarding "DMax".  I had tried that before your suggestion, but couldn't figure out the 3rd portion ("Odometer<"[Odometer]) part.  That was the missing solution.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 6/16/2026 4:11:07 PM. PLT: 1s