473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

#Error: when dividing by 0

Hi all,
I have this sql in my querry to calcuate the rate:
Rate:([Yes]/([Yes]+[No])). And in my report, I set the format of the field to "percentage", but then when the sum of Yes + No is =0, it shows #Error, and I do not want the report to have "#Error", instead I would like it to say: "Dem 0", then I modify my sql as follow:
Rate: IIf(([Yes]+[No])=0,"Dem 0",([Yes]/([Yes]+[No])))
the "Dem 0" works well for the ones with demoniator=0, but then I can't set the format on the form to "percentage" anymore, so it'll say 0.333333, instead of %, for the ones with demoniator is not euqal to 0.

Can someone please help? basically, I would like to get rid of the "#Error", when the demoninator =0, but still be able to keep the % format.

Thank you!
bluemoon
Apr 1 '09 #1
10 1749
I tried this way and it works well, but then I run into the problem of, for example # such as 0.333333333, when *100, it becomes 33.33333333, how can I cut of at 33.33 only?
Rate: IIf(([Yes]+[No])=0,"Dem 0",([Yes]/([Yes]+[No])*100 &"%"))

thank you!

bluemoon
Apr 1 '09 #2
ChipR
1,287 Expert 1GB
Try
IIf ( [Yes]+[No])=0,"Dem 0", Format( ([Yes]/[Yes]+[No]), "Percent") )
Apr 1 '09 #3
Hi, it doesn't work, because of "percent" in quotation, I've tried to put (), but did not work either.
thanks!

bluemoon
Apr 1 '09 #4
ChipR
1,287 Expert 1GB
You may be missing a parenthesis or comma. What is the error you're getting specifically?
Apr 1 '09 #5
Hi,
I've tried to put Format$, instead of Format only and it works.
Thanks

bluemoon
Apr 1 '09 #6
NeoPa
32,556 Expert Mod 16PB
@ChipR
This would be clearer in [ CODE ] tags, but the problem here is that there are parentheses () around the [Yes]+[No] immediately after IIf.

This should be a workable solution, to avoid using out-of-date function calls which won't be supported forever :
Expand|Select|Wrap|Line Numbers
  1. Format(IIf([Yes]+[No]=0,'Dem 0',[Yes]/([Yes]+[No])),'Percent')
Apr 2 '09 #7
NeoPa
32,556 Expert Mod 16PB
NB. It's very important to remember that with this solution (any of those mentioned in fact) the result is now a string and not a numeric entity. Generally to be avoided in SQL where possible.

Without knowing the context it's hard to say where, but it's better practice to format the number only at the point of display. Specifically not at the point of calculation.
Apr 2 '09 #8
ChipR
1,287 Expert 1GB
You're right, I missed that parenthesis in the first part of the IIF. Sorry about that.
However, I don't see how CODE tags could possibly make it any clearer, unless you prefer a smaller font, nor why you moved the IIf inside the Format function.
Apr 2 '09 #9
NeoPa
32,556 Expert Mod 16PB
Proportional fonts are not good for determining easily, exactly what is displayed. That's the main reason why they're never used for displaying code. This web font in particular does not differentiate clearly between many characters and is known to lead to confusion. Hence the site rule that all code (with the possible exception of 1 liners) must be posted within the CODE tags.

The IIf() inside the Format() is simply a matter of tidiness. I was keeping the code that determined the value together, then applying formatting to the calculated result. Experience has taught me that far fewer errors occur when thinking in logical progression.

If that sounds like criticism of your code, that is not my intention. You asked the question, I answered it as I see it.
Apr 2 '09 #10
ChipR
1,287 Expert 1GB
No problem, I respect your opinion. But applying the Format to the entire result is not a logical progression the way I see it, since the result of the IIf may be a String, and not need formatting at all. You only need to Format the part that is a number, and I found myself wondering what happens if you try to Format 'percent' on a string like 'Dem 0'. It's fine for those who know that the Format function won't have any affect on the string, but who wants to assume when it's unneccesary?
Apr 2 '09 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

65
by: Pmb | last post by:
I'm confused as to what the compiler error message I'm getting is refering to. Can someone take a gander and let me know what I did wrong? The program is below. When I compile it I get the...
1
by: Matt | last post by:
Is there any reason why I shouldn't cause an arithmetic error(say by dividing by zero) in a User Defined Function for a situation where in a stored procedure you would use RAISERROR or in code you...
1
by: mmacrobert | last post by:
Hi there, I'm having trouble making some code do a "graceful" recovery for mathematical operations. Certain functions e.g. log10, will call "matherr" and provides an avenue for intervention in a...
1
by: Andrew Chanter | last post by:
I have an application that produces examination scores. Candidates have a choice of sitting 1 of 2 subjects or both. I have produced a query that gives all the results for candidates that sat...
5
by: Aleramo | last post by:
I hope someone could say me where can i find the meaning of: gsl: sinint.c:359: ERROR: domain error Default GSL error handler invoked. Aborted or where i can find it. I don't understand the...
4
by: Sheldon | last post by:
Hi, I have a written a script that will check to see if the divisor is zero before executing but python will not allow this: if statistic_array > 0.0: statistic_array =...
2
by: hzgt9b | last post by:
I've been executing the CopyFileEx code below in a Windows Application for several months... Try If CopyFileEx(fiSource.FullName, strTargetFile, Fpr, ACTION_COPY, 0, 0) 0 Then Else Throw New...
35
by: jeffc226 | last post by:
I'm interested in an idiom for handling errors in functions without using traditional nested ifs, because I think that can be very awkward and difficult to maintain, when the number of error checks...
2
by: Omar Abid | last post by:
Reason of this project: Error handling is one of the most difficult thing that may afford a programmer. It isn't as easy as you think and handling errors in a program some time can make errors...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.