Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6252427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:47:59+00:00 2026-05-24T13:47:59+00:00

I am trying to prevent #error being displayed in a report I am creating

  • 0

I am trying to prevent #error being displayed in a report I am creating

This happens when I divide 2 numbers and one of them is zero

So I tried using an if/switch statement to check if either of the 2 number are 0 before doing the divide :

    =IIf(Fields!Field1.Value = 0 
            or Fields!Field2.Value = 0 
            or Not(IsNumeric(Fields!Field1.Value)) 
            or Not(IsNumeric(Fields!Field2.Value)), 
        0, 
        (Fields!Field1.Value/Fields!Field2.Value)*100
    )


    =Switch(
            Fields!Field1.Value = 0 or Fields!Fields.Value = 0, 0,
            IsNumeric(Fields!Field1.Value) or IsNumeric(Fields!Fields.Value), (Fields!Field1.Value/Fields!Fields.Value)*100
    )

Both of these still throw the error. It seems that the else condition is still evaluated even if the if statement is true

If I change the code to just print X or Y for the if and else, then it works – so there isn’t an error in the if statement

This sems ridiculous to me

Please tell me I am doing something wrong? I can’t believe the language would evaluate the else when the if is true

EDIT

So it seems that the else condition is evaluated. So how do you get around a potential divide by zero error?

here’s the answer taken from :
http://www.sqlservercentral.com/Forums/Topic442497-150-1.aspx#bm1115960

Another option (especially if you’ve got a report with many expressions that could result in divide by zero situations is to use a Custom Code function.

In the Code tab/window of Report Properties, enter something like the following:

Public Function DivideBy(ByVal Exp1, ByVal Exp2)
If Exp2 = 0 Then
DivideBy = 0
Else : DivideBy = Exp1 / Exp2
End If
End Function

Then insert the expression
=code.DivideBy(Field!ToBeDivided.Value,Field!DividingBy.Value)
into any cell that has the potential for divide by zero problems.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T13:48:00+00:00Added an answer on May 24, 2026 at 1:48 pm

    Yes, both the true and false parts of an IIf function get evaluated:

    http://en.wikipedia.org/wiki/IIf#Side_Effects

    Side Effects
    Another issue with IIf arises because it is a library
    function: unlike the C-derived conditional operator, both truepart and
    the falsepart will be evaluated regardless of which one is actually
    returned. Consider the following example:

    value = 10 
    result = IIf(value = 10, TrueFunction, FalseFunction)
    

    Although TrueFunction is the function intended to be called, IIf will
    cause both TrueFunction and FalseFunction to be executed.

    Also consider this one:

    a = 10 
    b = 0 
    result = IIf(b <> 0, a / b, 0) 
    

    While the programmer
    intends to avoid raising an error by performing a division by zero,
    whenever b is zero the error will actually happen. This is because the
    code in the snippet is to be read as

    a = 10 b = 0
    _temp1 = a / b ' Error if b = 0
    _temp2 = 0
    _temp3 = b <> 0 result = IIf(_temp3, _temp1 , _temp2) 
    

    This issue makes
    the IIf() call less useful than the conditional operator. To solve
    this issue, Microsoft developers had considered converting IIf to
    an intrinsic function; had this happened, the compiler would have been
    able to perform type inference and short-circuiting by replacing the
    function call with inline code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a DropDownList and I'm trying to prevent it from being used as
I'm trying out this basic authentication using jQuery. But nothing happens when I enter
I have been trying to develop some user controls, one being a text edit
I am trying to prevent VS from breaking on JS errors. I have the
Trying to prevent access to a specific file, not any files with a certain
I am trying to prevent data races in a multihreaded server. My problem is
I'm trying to prevent A HREF from actually opening the link, but to execute
I am trying to prevent any SQL injection in all my queries and would
I'm trying to prevent information to be copied from a page (for non-technical users
Not that I'm trying to prevent 'View Source' or anything silly like that, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.