Results 1 to 8 of 8
  1. #1
    BenDatabase is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2018
    Posts
    5

    Averaging a Calculated field containing a Division calculation

    Hi - I have an Access database that I created to track fuel mileage for my Automobiles several months ago. Originally the database worked without issue. However a few weeks ago the charts I had on my main Form and my Mileage trend report quit working. I've been able to correct the chart issue but the Mileage trend report still has an issue. Originally the report would display the mileage each auto got for each tank of gas generated from a query. The report would then average those mileage numbers for each auto so that I would have an overall average mileage for each auto using the totaling on the report. That totaling has quit working. I get an error message shown in Figure #1 when trying to run the report.

    Figure #1
    Click image for larger version. 

Name:	Figure 1.JPG 
Views:	17 
Size:	15.0 KB 
ID:	34740

    I've been able to trace the issue all the way back to the table containing the data by creating a field (not sure why I didn't do this originally) to calculate the MPG for each entry in the table itself as shown in Figure #2 attached.



    Figure #2
    Click image for larger version. 

Name:	Figure 2.JPG 
Views:	16 
Size:	18.2 KB 
ID:	34741

    I've discovered that when I add the totaling row to the table in datasheet view, this MPG field the functions SUM, Average, Standard Deviation, or Variance will not work in the totaling row for that column. I have another calculated field in the same table which will do those things without issue. The only difference between the two calculated fields is that the MPG field is a division calculation and the other is a Multiplication calculation. I'm sure this is something simple but so far have been unable to figure it out. Anyone have any ideas or suggestions I could try to resolve the issue?

    Thanks
    Last edited by BenDatabase; 07-16-2018 at 09:11 AM. Reason: Correcting post

  2. #2
    kd2017 is offline Well, I tried at least.
    Windows 10 Access 2016
    Join Date
    Jul 2017
    Posts
    1,142
    Calculated Fields

    How do you get Access to store the result of a calculation?
    For example, if you have fields named Quantity and UnitPrice, how do you get Access to write Quantity * UnitPrice to another field called Amount?
    The best answer is, "Don't!"
    Calculated fields belong in queries, not tables.

    Calculated columns are part of life on a spreadsheet, but do not belong in a database table. Never store a value that is dependent on other fields: it's a basic rule of normalization. Break the rule, and you have to worry about how to maintain the calculation correctly in every possible circumstance.
    Ask yourself, "Would it be wrong if my field contained a value different from the calculation?" If you answer Yes, you must not store the field. If the difference would be meaningful, you do need the field.
    So, how do you get the calculated field if you do not store it in a table? Use a query:

    1. Create a query based on your table.
    2. Type your expression into the Field row of the query design grid:
      Amount: [Quantity] * [UnitPrice]

    This creates a field named Amount. Any form or report based on this query treats the calculated field like any other, so you can easily sum the results. It is simple, efficient, and foolproof.

    http://allenbrowne.com/casu-14.html

  3. #3
    BenDatabase is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2018
    Posts
    5
    kd2017 thanks for your reply. I agree with you and originally calculated the MPG using a query for use on the report. The reason I backed up to the table was to troubleshoot the issue described above of Access not averaging the MPG column now and giving me the error shown and described in the OP. Once I figure out what the issue is I'll be going back to calculating the MPG using the Query for the report to display.

    Thanks,
    Ben

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Is there division by zero? Although I would expect a different error message.

    I have never used Decimal and Currency number types - only Double, Single, and Long Integer.

    A Calculated type field in table is not same as code saving a calculated result. Only real issue I see with table Calculated type fields is they complicate migrating to another db platform such as SQLServer. Same goes for multi-value fields.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    BenDatabase is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2018
    Posts
    5
    Still playing around with trying to figure out the issue. The image below may help in seeing what is going on. This is from my query that calculates the MPG. When I add the totals row to the query and try to use the average function I get the error shown below which is the same error the report is giving me when trying to average the MPG on the report. The query runs fine if I don't try to average the column. All the data used is contained in the Access Database and has not been imported from another source but was entered directly into the Access database. It's like the Average function is trying to return a value that doesn't fit in the datatype of the column. However in messing with the datatype for the column nothing changes in relation to the error.

    Click image for larger version. 

Name:	Figure 3.JPG 
Views:	13 
Size:	120.1 KB 
ID:	34745

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Think we need data to analyse. If you want provide db, follow instructions at bottom of my post.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    BenDatabase is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2018
    Posts
    5
    Ok here is my database. I've left the MPG field in the Milleage table as well as in the completeMPGCalculation query. Interestingly enough I've found that it works if the datatype is currency. Of course that isn't the right datatype for the data haha but it is a note of interest.

    Thanks,
    Ben
    Attached Files Attached Files

  8. #8
    BenDatabase is offline Novice
    Windows 10 Access 2016
    Join Date
    Jul 2018
    Posts
    5
    Ok well I solved the issue though not sure why it broke. Turns out it was indeed a datatype issue. My MPG calculation was using the MilesDriven field divided by the Gallons field in the Mileage table. Both of which were decimal (6,3,3) datatype. I switched these two fields to Double datatype and the average function in the Total row started working. I set everything in the Queries and Report back as I had them originally leaving the two fields in the Mileage table datatypes as doubles and the Report calculated the overall Avg MPG for each auto as it should. As I'm pretty sure I didn't change the datatype of these two fields from when I built the database originally several months ago I'm not sure why it worked to start with and then "broke". Apparently Access is not able to calculate an Avg on a Decimal Datatype using the Avg Function in the Total Row.

    Thanks June7 it was your original comment about not having used the Decimal datatype but instead the Double datatype that I tried the Double and solved the issue.

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

Similar Threads

  1. Replies: 1
    Last Post: 05-22-2017, 05:59 AM
  2. Replies: 2
    Last Post: 12-30-2014, 01:32 PM
  3. Replies: 5
    Last Post: 01-04-2014, 02:29 PM
  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. Averaging with a blank field
    By Gabriel2012 in forum Access
    Replies: 3
    Last Post: 12-06-2012, 12:36 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