Solved

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

Posted on 2009-04-09
494 Views
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 doesnt 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.

Ive tried different combinations of Format, Val, CINT, NZ and IIF.

Any help would be greatly appreciated.
0
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
• 4
• 4
• 2
• +1

LVL 13

Expert Comment

ID: 24110319
Did you try:

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

LVL 44

Expert Comment

ID: 24110338
0

Author Comment

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

LVL 13

Expert Comment

ID: 24110976
0

Author Comment

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

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

LVL 44

Accepted Solution

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 50

Assisted Solution

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

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

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 50

Expert Comment

ID: 24119051
You are welcome!

/gustav
0

LVL 44

Expert Comment

ID: 24119265
0

## Featured Post

Question has a verified solution.

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

### Suggested Solutions

Itâ€™s been over a monthÂ into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targetiâ€¦
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â€¦
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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â€¦
###### Suggested Courses
Course of the Month5 days, 6 hours left to enroll