Microsoft KB Archive/106288

From BetaArchive Wiki
Knowledge Base


ACC: How to Use the IIf() (Immediate If) Function

Article ID: 106288

Article Last Modified on 1/18/2007



APPLIES TO

  • Microsoft Access 1.0 Standard Edition
  • Microsoft Access 1.1 Standard Edition
  • Microsoft Access 2.0 Standard Edition
  • Microsoft Access 95 Standard Edition
  • Microsoft Access 97 Standard Edition



This article was previously published under Q106288

Moderate: Requires basic macro, coding, and interoperability skills.

SUMMARY

You may receive unexpected results when you use the IIf() (Immediate If) function to evaluate an expression. Your results will depend on the use of double quotation marks in the function's arguments.

MORE INFORMATION

The following example demonstrates how the use of double quotation marks in the IIf() function's arguments can affect the results:

  1. Open the sample database Northwind.mdb (or NWIND.MDB in version 1.x or 2.0)
  2. Create the following new query based on the Orders table:

          Query: Test
          ---------------------------------------
          Field Name: Sum(IIf("[Freight]>50",1,0))
                            
  3. Run the query. The result will be the total number of records in the table, not the number of records with Freight charges greater than 50.
  4. View the query in Design view. Remove the double quotation marks so the query matches the following:

          Query: Test
          -------------------------------------
          Field Name: Sum(IIf([Freight]>50,1,0))
                            
  5. Run the query again. The result will be the correct number of records where the freight charge amount is greater than 50.

The double quotation marks around the "[Freight] > 50" argument cause Microsoft Access to evaluate the expression as a string instead of as a comparison. A string will always evaluate to true, while a comparison will test for the condition to be met before returning a value.

REFERENCES

For more information about the IIf function, search for "IIf function," and then "IIf Function" using the Microsoft Access 97 Help Index.

Keywords: kbhowto kbusage KB106288