Solved

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

Posted on 2009-04-09
12
485 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_
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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 49

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 49

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now