Solved

No Decimal, Rounding, If Null Return 0, Number Data Type

Posted on 2009-04-09
12
498 Views
Last Modified: 2012-06-27
I have a crosstab query in Access (2003 SP3).  The result for the value field should meet these criteria when the query is run:

No decimals
If null return 0
Arithmetic rounding (if .5 or greater round up, if less than .5 round down)
Return a number when exported to Excel (using DoCmd.OutputTo acOutputQuery)

The code below gives me the look I want but doesnt export as a number field into Excel:

40000lb units: Format(nz(Sum([10000lb units]/4),0),"0")

If I wrap Val or CInT around it I get the decimals.

Ive tried different combinations of Format, Val, CINT, NZ and IIF.

Any help would be greatly appreciated.
0
Comment
Question by:Carolyn_
[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
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 13

Expert Comment

by:lucas911
ID: 24110319
Did you try:

Format(nz(Sum([10000lb units]/4),0),"#")
0
 
LVL 44

Expert Comment

by:GRayL
ID: 24110338
How about:  Round(Sum(Nz([10000lb units],0)/4)1),0)
0
 

Author Comment

by:Carolyn_
ID: 24110707
Format(nz(Sum([10000lb units]/4),0),"#"):
1. Doesn't produce the NZ zeros.
2. Doesn't result in a Number data type.

Round(Sum(Nz([10000lb units],0)/4)1),0):
1. Round() in Access does "banking" rounding (if odd such as 1.5 rounds up to 2, and if even such as 2.5 rounds down to 2).  I want to always round up on .5
2. For grins I tried this portion only:  Sum(Nz([10000lb units],0)/4) and didn't get the NZ zeros.



0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 13

Expert Comment

by:lucas911
ID: 24110976
0
 

Author Comment

by:Carolyn_
ID: 24111054
Finally, I've got it.  My very first formula that I used for awhile was this and it didn't matter that I had the decimals:
40000lb units: Val(Format(Sum([10000lb units]/4),"#")).

Somewhere in my "travels" I changed the Format property to Fixed (ugh).  Now that I've set it to General Number or leave it blank, I get what I want.
This takes care of the rounding and gives me the zeros.

Thanks to all for your suggestions...  it kept me going and trying different approaches.

This is my first post, and I want to give credit where credit is due!  However, I was not provided with the solution so I'm not planning to award any points.  If I'm mistaken on this, please let me know.
0
 
LVL 44

Expert Comment

by:GRayL
ID: 24111181
Banking round does not apply when there are no decimals.  My solution holds.
0
 
LVL 44

Accepted Solution

by:
GRayL earned 125 total points
ID: 24111217
Ignore the last post, of course it does.  Some how the 1 got in there.  For the record:

Round(Sum(Nz([10000lb units],0)/4) + 0.0001),0)
0
 
LVL 51

Assisted Solution

by:Gustav Brock
Gustav Brock earned 125 total points
ID: 24112142
Carolyn's solution is quite nice as it uses Val to return 0 for a zero length string and is as simple and compact as possible.
Further, Format() is the _only_ function in VBA that does correct arithmetic rounding. Round() should be avoided for any serious purpose:

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

/gustav
0
 

Author Closing Comment

by:Carolyn_
ID: 31568675
GrayL, thank you for looking into this for me and providing the good link on rounding.

Gustav, thank you for summarizing my solution so nicely.  The clarification on Format() in terms of rounding was helpful.
0
 

Author Comment

by:Carolyn_
ID: 24119016
GrayL and Gustav, I wanted to accept my own comment as the solution, and award each of you 1/2 of the points for assistance but I messed it up, and now it seems there is no way to correct it.  Via Request Attention I asked how to change this, but the respond I got was that the original question was already closed and did not provide an answer!  I'm going to let it stand as is.  Thanks again for working with me!
0
 
LVL 51

Expert Comment

by:Gustav Brock
ID: 24119051
You are welcome!

/gustav
0
 
LVL 44

Expert Comment

by:GRayL
ID: 24119265
Thanks, glad to help.
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

636 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