Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I have divide by zero problems

Status
Not open for further replies.

spow

Technical User
Jun 11, 2002
8
US
Let's say I have a table that looks like the following:

Table1:
[Name AB Hits]

And I have a query that calculates:

Avg = Hits / AB

But sometimes, the entries in the table have AB = 0.

When I run this query, it outputs an error, how can I make it output 0 for Avg if AB = 0 ?

Similar to the basic statement:
If AB = 0 THEN AVG = 0 ELSE AVG = Hits / AB

Only I imagine this would be in SQL or the design mode of the query. Thanks in advance for any help.

Spow
 
In the query use the following for the Field

Avg = IIf([AB] = 0,0,[Hits]/[AB])

This uses the Immediate If function. The logic is if the first parameter is true return the second parameter, else return the third parameter. Bear in mind that IIf functions are relatively slow, but sometimes it's the only way to make something work. "The Key, The Whole Key, and Nothing But The Key, So Help Me Codd!"
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top