Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I Match the rounding in Access (query) that was in Excel

Posted on 2011-02-17
21
Medium Priority
?
324 Views
Last Modified: 2012-05-11
I am using a query to create formulas to calculate percentages as well as reimbursement amounts in Access.  Calculations were originally done in Excel, but data is stored in Access, so decided to use Access to calculate as well.  Unfortunately, Acccess not rounding as in Excel.

For example 42 / 64 =.65625
in Excel displays as .6563 rounding to 4 decimal places,
in Access displays as .6562 rounding to 4 decimal places.  

As stated, these must be exact calculations and are finding differences in Access vs. Excel.
0
Comment
Question by:softsupport
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 3
  • +1
21 Comments
 

Author Comment

by:softsupport
ID: 34920535
Need the rounding to be .6563 as in Excel.... How is this accomplished?
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34920865
Round(42/64 +.000001,4)

Access performs a Bankers round.  You need to add a trivial amount as shown above that will not affect the result but give you the correct rounding.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34920874
Use Format which is the only function in Access VBA that performs true 4/5 rounding. All the Cxxx function performs banker's rounding:

dblRoundedValue = CDbl(Format(42 / 64, "0.0000"))

/gustav
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 7

Expert Comment

by:shaydie
ID: 34920964
You could also use a custom function

Function RoundUp(ByVal Number As Variant, ByVal Places As Integer) _
                                      As Double
Dim dblTemp As Double
dblTemp = CDec(Nz(Number))
dblTemp = CDec(dblTemp * 10 ^ Places)
RoundUp = Fix(dblTemp + 0.5 * Sgn(Number)) / 10 ^ Places
End Function


http://www.consultdmw.com/rounding-numbers.htm
0
 

Author Comment

by:softsupport
ID: 34938685
GRayL:

Your solution appears to have corrected most of my issues, except one.
I must round all formulas to 4 decimal places when possible.

There are 4 variables ie: A+B+C=D (these variables are field names)
A is / by D and must round giving 4 decimal place answer.
B is / by D and must round giving 4 decimal place answer.
same for  C
I have one record where A = 26, B =6 and C =47 making D= 79
When dividing B by D,  [ 6 / 79 ] answer shows as .076, which i need to display as .0759 to calculate properly.  

How can i adjust this so that all other records, which appear to calculate correctly, do not change.  this is the only record now not calculating properly.

Once again, this is done in a query, that I must export to excel for further calculations.

Thank you all again for any assistance
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34939120
Once again, Format does this correctly:

BbyD: CDbl(Format(6 / 79, "0.0000"))

returns 0.0759

Likewise for your other calculations.

/gustav
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34943093
softsupport:  cactus_data has the more elegant solution.
0
 

Author Comment

by:softsupport
ID: 34952881
Apologies, but the Format still not rounding properly. Now instead of one instance of not rounding, have 5 instances.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34952945
Instances??
Are you posting to the right thread?

/gustav
0
 

Author Comment

by:softsupport
ID: 34952948
For example now i have .1875 * 1.18 = .2212 but should round to .2213
0
 

Author Comment

by:softsupport
ID: 34953010
Yes, Posting to the correct thread, I stated earlier that GRayL corrected most of my rounding issues except one.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34953024
That's Banker's rounding and why you should round yourself for 4/5 rounding:

dblRoundedValue = CDbl(Format(0.1875 * 1.18, "0.0000"))
returns 0.2213

/gustav

0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34953035
But 5 "instances" ...?

/gustav
0
 

Author Comment

by:softsupport
ID: 34953235
Not able to send file, information confidential.  But not returning result of .2213.  There are several calculations that take place in this query.  all using your Format command to round out decimal places and not consistent in the rounding.  This information is sent to the state and must be accurate before sending.
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34953363
Well, if you can't or won't tell us what you are doing, it is rather difficult to help.

The expression using Format will and does work. In fact, it is the simplest (though not the fastest) proven method in VBA to perform correct 4/5 rounding under any condition:

http://www.xbeat.net/vbspeed/c_Round.htm
http://www.xbeat.net/vbspeed/c_Round.htm#Round16

/gustav
0
 
LVL 44

Expert Comment

by:GRayL
ID: 34953724
Re:  Your post at http:#a34952948

? cdbl(Format(0.1875 * 1.18,"0.0000"))
 0.2213


0
 

Author Comment

by:softsupport
ID: 34953972
Attached are 2 files.  Excel is where the correct calculations reside that i must use.  The data actually resides in a database, therefore, thought we should use be able to use access to do the calculations also.  Problem is the rounding is not exact.  This example : Little Leaders of Tomorrow on Excel rounds to .2213, in access rounds to .2212.  Is doing eventhough I used the CDbl(Format),"0.0000"))

Any assistance will be appreciated. TEST.xlsx Database1.accdb
0
 

Author Comment

by:softsupport
ID: 34955739
http://www.lincoln.ac.nz/About-Lincoln-University/outreach/Spreadsheet-Resource-Reservoir/Spreadsheet-Articles/Rounding-problem-4/

Found this information online.... demonstrates my problem..... How can I make the VBA Round function behave like the Excel Round function as demonstrated in the link above?
0
 
LVL 51

Accepted Solution

by:
Gustav Brock earned 1000 total points
ID: 34961548
No, that is not the source.

The reason is that in your USDA table, data type Single is used throughout.
Therefore, when casted to double, 1.18 resolves to 1.17999994754791 which is smaller than 1.18, thus your rounded value ends up slightly smaller.

So, change all fields to data type Double and reentry your values.

/gustav
0
 

Author Closing Comment

by:softsupport
ID: 34961923
Geez..... thank you for the assistance.  Pulled my hair out looking for the answer and needed additional eyes to locate the problem.  Much appreciation !
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 34962001
Thanks. You are welcome!

/gustav
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question