Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Report field data type problem 2

Status
Not open for further replies.

CHTHOMAS

Programmer
Jun 16, 1999
106
AE
I have a field in my report whose data type properties are set as follows.<br>
Format-&gt;Standard<br>
Decimal Places-&gt;2<br>
I set the same type of properties for my table and query.<br>
I am using the function sum to find the sum of the field in the report. My problem is i am getting 1 Penny/fill difference for the sum. I believe it may be due to the rounding of values, but not sure about how to solve it. Any suggestions or solutions will be highly appreciated.<br>
Thanks in advance<br>
Charley
 
Sounds like a rounding up/down problem try the currency format or force the result with format £ #,#0.00;£ -#,#0.00;£ 0.00;£ 0.00 (+ve,-ve,zero and null formats)
 
I am having the same problem with the currency round up/or down one penny.&nbsp;&nbsp;I'm try to take the sum of several values in a query.&nbsp;&nbsp;the totals that are in the query come from the multiplication of unitprices that have 4 decimals (0.0234)etc.&nbsp;&nbsp;When this unitprice gets multiplied by the quantity, the sum this single price will be correct, but when I add the sum of several parts prices in a report the sum will be +/- one penny.&nbsp;&nbsp;Did any one help you with this problem.&nbsp;&nbsp;If so I would love to hear how I can solve this problem.&nbsp;&nbsp;reply me at <A HREF="mailto:crownknox@aol.com">crownknox@aol.com</A>&nbsp;&nbsp;thanks.
 
The best I was able to do was to round the fractional penny results using the computation: CCur(Int(X*100+0.5)/100).&nbsp;&nbsp;It depends on X being small enough to multiply safely by 100.&nbsp;&nbsp;The intermediate results in this computation seem to be double precision floating point.&nbsp;&nbsp;I'm hoping that the error doesn't make it back into the third and fourth digits of the currency representation when it converts.&nbsp;&nbsp;I haven't seen any wierd numbers yet.
 
The solution to the above mentioned problem is as follows. I did get it some where from the net, probably Microsoft, but don't remember correctly.<br>&nbsp;<br>The Format property of a control can round a Number or Currency field to the number of decimal places that you want. However, this does not change the underlying data, which may contain additional digits that the control does not display. If you add the values in this control, the sum is based on the actual values and not on the displayed values. This may make the total seem inaccurate. <br>&nbsp;<br>This article shows you how to create four user-defined functions to round or truncate data to two decimal places so that the displayed and formatted value and the actual numeric or currency data are the same. <br>&nbsp;<br>This article assumes that you are familiar with Visual Basic for Applications and with<br>creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the &quot;Building Applications with Microsoft Access&quot; manual. <br>&nbsp;<br>NOTE: Visual Basic for Applications is called Access Basic in Microsoft Access versions 1.x and 2.0. For more information about Access Basic, please refer to the &quot;Introduction to Programming&quot; manual in Microsoft Access version 1.x or the &quot;Building Applications&quot; manual in Microsoft Access version 2.0 <br>&nbsp;<br>&nbsp;<br>MORE INFORMATION<br>&nbsp;<br>The functions are presented in two styles. The first style is appropriate for the AfterUpdate property of a form control to ensure that the data entered matches the data that is displayed. The second style is for use in expressions and calculated controls. <br>&nbsp;<br>To round or truncate numbers to two decimal places, create a new module and add the following functions. <br>&nbsp;&nbsp;<br>'******************************************************<br>' Declarations section of the module<br>'******************************************************<br>&nbsp;<br>Option Explicit<br>Const Factor = 100<br>&nbsp;<br>'=====================================================<br>' RoundAU and TruncAU are designed to be added to the<br>' AfterUpdate property on a form control.<br>'=====================================================<br>Function RoundAU(X As Control)<br>&nbsp;<br>X = Int(X * Factor + .5) / Factor<br>&nbsp;<br>End Function<br>&nbsp;<br>Function TruncAU(X As Control)<br>X = Int(X * Factor) / Factor<br>End Function<br>&nbsp;<br>'=====================================================<br>' RoundCC and TruncCC are designed to be used in<br>' expressions and calculated controls on forms and reports.<br>'=====================================================<br>Function RoundCC(X)<br>&nbsp;<br>RoundCC = Int (X * Factor + 0.5) / Factor<br>&nbsp;<br>End Function<br>&nbsp;<br>Function TruncCC(X)<br>TruncCC = Int (X * Factor) / Factor<br>End Function <br>&nbsp;<br>Examples of Using the Round and Truncate Functions<br>&nbsp;<br>The following examples use the sample database Northwind.mdb (or NWIND.MDB in version<br>2.0 or earlier). <br>&nbsp;<br>CAUTION: Following the steps in these examples will modify the sample database<br>Northwind.mdb (or NWIND.MDB in version 2.0 or earlier). You may want to back up the<br>Northwind.mdb (or NWIND.MDB) file and perform these steps on a copy of the database. <br>&nbsp;<br>Example 1<br>&nbsp;<br>Use the TruncAU() function to the AfterUpdate property of a form: <br>&nbsp;<br>1.Open the sample database Northwind.mdb. <br>&nbsp;<br>2.Create a new module called Rounding, and type the procedures in the preceding<br>section. <br>&nbsp;<br>3.Open the Products form in Design view, and add the TruncAU() function to the<br>AfterUpdate property of the UnitPrice field (or Unit Price field in version 2.0 or<br>earlier): <br>&nbsp;<br>Form: Products<br>--------------<br>Control Name: Unit Price<br>AfterUpdate: =TruncAU([UnitPrice]) <br>&nbsp;<br>If a user accidentally enters $23.055 instead of $23.05, the TruncAu() function<br>catches the mistake and changes the value to $23.05. If you use the RoundAu()<br>function instead, the function changes the value to $23.06. If you use neither<br>function, the value is displayed as $23.06, but the entered value, $23.055, is used in<br>any calculations. <br>&nbsp;<br>Example 2<br>&nbsp;<br>Use the RoundCC() function with an expression in a report's group footer. This example<br>assumes that you have already created the Rounding module in step 2 of Example 1: <br>&nbsp;<br>1.Open the sample database Northwind.mdb. <br>&nbsp;<br>2.Open the Summary Of Sales By Year report in Design view and use the RoundCC()<br>function in the ControlSource property of two controls in the report's group footer: <br>In Microsoft Access 7.0 and 97: <br>&nbsp;<br>Report: Summary of Sales By Year<br>--------------------------------<br>Control Name: QuarterSales<br>ControlSource: =Sum(RoundCC([SubTotal]))<br>&nbsp;<br>Control Name: YearTotal<br>ControlSource: =Sum(RoundCC([SubTotal])) <br>&nbsp;<br>In Microsoft Access 1.x and 2.0: <br>&nbsp;<br>Report: Summary of Sales By Year<br>--------------------------------<br>Control Name: Total Sales for Quarter<br>ControlSource: =Sum(RoundCC([Order Amount]))<br>&nbsp;<br>Control Name: Total Sales for Year<br>ControlSource: =Sum(RoundCC([Order Amount])) <br>&nbsp;<br>If you use RoundCC(), the report sums the values displayed in the report, even<br>though the actual values may contain hidden digits. <br>&nbsp;<br>NOTE: To change the number of decimal places that the functions use, open the Rounding<br>module in Design view and change the value of the global constant, Factor, as follows: <br>&nbsp;<br>10 = 1 decimal place<br>100 = 2 decimal places<br>1000 = 3 decimal places, and so on <br>&nbsp;<br>Limitations<br>&nbsp;<br>These functions should only be used with Currency data. If used with Double or Single<br>numbers, you may still receive minor rounding errors. The reason for this is that Single and<br>Double numbers are floating point. They cannot store an exact binary representation of<br>decimal fractions. Therefore there will always be some error. However, Currency values are<br>scaled integers and can store an exact binary representation of fractions to 4 decimal<br>places.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top