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

x
?
Solved

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

Posted on 2009-04-09
12
Medium Priority
?
502 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:Lucas
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 13

Expert Comment

by:Lucas
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 375 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 375 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
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…

660 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