dbTalk Databases Forums  

Query returns error if zero

comp.databases.ms-access comp.databases.ms-access


Discuss Query returns error if zero in the comp.databases.ms-access forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Rosy
 
Posts: n/a

Default Query returns error if zero - 04-23-2009 , 01:56 PM






I have a calculated field in a query - Rate: [Fees]/[BilledHours]

If the fees are zero and the hours are zero I get an Error in that
field. How can I have it return a zero when it's dividing by zero?

Reply With Quote
  #2  
Old   
MGFoster
 
Posts: n/a

Default Re: Query returns error if zero - 04-23-2009 , 02:13 PM






Rosy wrote:
Quote:
I have a calculated field in a query - Rate: [Fees]/[BilledHours]

If the fees are zero and the hours are zero I get an Error in that
field. How can I have it return a zero when it's dividing by zero?
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Use the IIf() function.

Rate: IIf([BilledHours]=0,0,[Fees]/[BilledHours])



Syntax: IIf(expression, true result, false result)

HTH,
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
** Respond only to this newsgroup. I DO NOT respond to emails **

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBSfC9s4echKqOuFEgEQI/WACfW62fjIzyjlfbFBXtbdW3IcJ2N/oAoKcn
ohsszN3UxJdIR95jeUlePSCW
=IWNQ
-----END PGP SIGNATURE-----


Reply With Quote
  #3  
Old   
Rosy
 
Posts: n/a

Default Re: Query returns error if zero - 04-23-2009 , 02:46 PM



On Apr 23, 1:56*pm, Rosy <krm... (AT) portlite (DOT) com> wrote:
Quote:
I have a calculated field in a query - Rate: [Fees]/[BilledHours]

If the fees are zero and the hours are zero I get an Error in that
field. *How can I have it return a zero when it's dividing by zero?
Perfect! Thanks!


Reply With Quote
  #4  
Old   
paii, Ron
 
Posts: n/a

Default Re: Query returns error if zero - 04-23-2009 , 04:45 PM




"MGFoster" <me (AT) privacy (DOT) com> wrote

Quote:
Rosy wrote:
I have a calculated field in a query - Rate: [Fees]/[BilledHours]

If the fees are zero and the hours are zero I get an Error in that
field. How can I have it return a zero when it's dividing by zero?

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Use the IIf() function.

Rate: IIf([BilledHours]=0,0,[Fees]/[BilledHours])




You may get an error with that expression, VBA executes the entire
expression even if [BilledHours] = 0.

If you do, try.

IIf([BilledHours]=0,0,[Fees]/IIf([BilledHours]=0,1,[BilledHours]))




Reply With Quote
  #5  
Old   
lyle fairfield
 
Posts: n/a

Default Re: Query returns error if zero - 04-23-2009 , 05:37 PM



VBA, yes. JET Sql, no.

Try these tests:

Sub temp()
Debug.Print IIf(1, 5, 5 / 0) 'error
End Sub

Sub temp2()
Debug.Print DBEngine(0)(0).OpenRecordset("SELECT IIf(1, 5, 5 / 0) FROM
MSysObjects")(0) 'r
End Sub

<rant>
That being said I am 100% opposed to IIf([BilledHours]=0,0,[Fees]/
[BilledHours])
If there are no hours then there is no rate.

My answer to:
How can I have it return a zero when it's dividing by zero?
is
take a time warp to another universe ... no, not even that. God cannot
make x / 0 = 0. Probably we should not try what defies God.

It is better both from a logical point of view and from modelling real
life to do:
Rate: [Fees]/[BilledHours]
WHERE BilledHours <> 0

and leave rate as null where BilledHours = 0.

Inevitably IIf([BilledHours]=0,0,[Fees]/[BilledHours]) will lead to
error because it is a contrivance, a dangerous bad contrivance.
Somwehere, sometime it will grab you and destroy you. Why. Because
it's WRONG!
Is this arrogant and pedantic? Sure. The reverse is why North American
manufacturing and business is failing. Rigor? We don't need no
stinking rigor! Just make some money today and get that bonus.
Eventually this will bring failure, has brought failure.
</rant>

On Apr 23, 5:45*pm, "paii, Ron" <n... (AT) no (DOT) com> wrote:

Quote:
You may get an error with that expression, VBA executes the entire
expression even if [BilledHours] = 0.

Reply With Quote
  #6  
Old   
paii, Ron
 
Posts: n/a

Default Re: Query returns error if zero - 04-24-2009 , 06:59 AM



Quote:
Is this arrogant and pedantic? Sure. The reverse is why North American
manufacturing and business is failing. Rigor? We don't need no
stinking rigor! Just make some money today and get that bonus.
Eventually this will bring failure, has brought failure.
/rant
Rant = Yes
Correct = 100%





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.