ACC: How to Use the IIf() (Immediate If) Function
ID: Q106288
|
The information in this article applies to:
-
Microsoft Access versions 1.0, 1.1, 2.0, 7.0, 97
SUMMARY
Moderate: Requires basic macro, coding, and interoperability skills.
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:
- Open the sample database Northwind.mdb (or NWIND.MDB in version 1.x or
2.0)
- Create the following new query based on the Orders table:
Query: Test
---------------------------------------
Field Name: Sum(IIf("[Freight]>50",1,0))
- 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.
- 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))
- 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 : kbusage ExrOthr
Version : 1.0 1.1 2.0 7.0 97
Platform : WINDOWS
Issue type : kbhowto
|