Solved

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

Posted on 2009-04-09
12
490 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 
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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

837 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