Custom Search
 




Inside This Article
1. IIF function basics - the CASE statement of MS Access
2Use MS Access IIF function in GROUP BY clause
3Use MS Access IIF function in ORDER BY clause
   
4A complex example of MS Access IIF function used within GROUP BY clause
5Use MS Access IIF function to conditionally calculate field values
6Use MS Access IIF function with NZ function to convert NULL values to numerics

IIF function basics - the CASE statement of MS Access



IIF function is used to make IF...THEN....ELSE logic processing in Access queries. If you have used sql in a DBMS (Database Management System) such as Oracle, SQL Server, or MySQL, you may be familiar with CASE statement. IIF is MS Access' equivalent of CASE statement that you normally can find in enterprise level database server environment.

Code example 1:

For example, in Northwind Tradders Products table, we rank the product price as High or Low by making the following logic checks. If Unit Price is greater than $100, it's classified as High Price. Otherwise, it is Low Price.

Query Name Qry_Func_IIF_1
Logic IF...THEN...ELSE...
IIF Code IIF(UnitPrice > 100, "High Price", "Low Price")
Design view
SQL View
SELECT Products.ProductID, 
       IIf([UnitPrice]>100,"High Price","Low Price") AS [Price Level]
FROM Products;
Datesheet View 77 records returned:

Tip: To test out the query, copy and paste the SQL into SQL view in Access.

Please note that IIF is not case-sensitive, you can write it as iif, Iif, IIF... whatever way you want.

Code example 2:

If we have more than two conditions to test, you can embed one IIF in another IIF statement to achieve the expected result.

For example, in Orders table, we can rank the Freight as follows:

  • High - greater than $200
  • Medium - between $100 and $200, inclusive.
  • Low - lower than $100
Query Name Qry_Func_IIF_2
IIF Code IIf(Freight>200,"High",IIf(Freight>=100 and Freight<=200,"Medium","Low"))
Design View
SQL View
SELECT Orders.OrderID, 
       Orders.Freight, 
       IIf(Freight>200,"High",
       IIf(Freight>=100 And Freight<=200,"Medium","Low")) 
       AS [Freight Price Level]
FROM Orders;
Datasheet View 830 records returned:

To sum up, here we have illustrated how to use IIF function in MS Access SELECT statement to conditionally test or select records in Access database table. Part 2 of IIF function will show you how to use IIF function in GROUP BY clause for more funs.


Copyright© GeeksEngine.com




Inside This Article
1. IIF function basics - the CASE statement of MS Access
2Use MS Access IIF function in GROUP BY clause
3Use MS Access IIF function in ORDER BY clause
   
4A complex example of MS Access IIF function used within GROUP BY clause
5Use MS Access IIF function to conditionally calculate field values
6Use MS Access IIF function with NZ function to convert NULL values to numerics
Related Articles:



Other Recent Articles from the MS Access category:

1.Examples of MS Access DateDiff function used in query and VBA code
2.MS Access DateDiff function
3.How to find out your computer name and username by VBA
4.Examples of MS Access DatePart function
5.MS Access DatePart function
6.Examples of MS Access DateAdd function
7.MS Access DateAdd function
8.MS Access Date Expression
9.Solved: MS Access error "The text is too long to be edited"
10.Create MS Access Combo Box essential properties by VBA code
11.Create MS Access Combo Box essential properties manually
12.How to do text search in MS Access programmatically
13.Solved - the size of the Access query result is larger than the maximum size of a database (2 GB)
14.How to easily get a list of field names in MS Access
15.How to count distinct records in MS Access
16.How to do transaction based processing in MS Access
17.How to open a document (local/network file or web page) from MS Access
18.How to use ADOX to create unique composite index - the VBA approach
19.How to do cross-table update queries in MS Access - the right way
20.Three efficient ways to get the number of records by using VBA
21.How to create a composite unique index (not as a primary key) in MS Access
22.Use VBA to get the correct number of records in a Recordset object
23.Disable Access Prompt when a record is changed, table deleted, or action queries run
24.How to hide and unhide a MS Access object
25.How to return multiple values from a VBA function (Part 3)
26.How to return multiple values from a VBA function (Part 2)
27.How to return multiple values from a VBA function (Part 1)
28.Three ways to programmatically duplicate a table in MS Access by VBA
29.Create a DLL by CSharp or VB.Net for VBA
30.How to correctly reference and call a DLL
31.How to register a C# or VB.Net DLL
32.Email address validation by Regular Expressions using VBA
33.Fix MS Access error: Query must have at least one destination field
34.How to unselect radio buttons in MS Access after it has been selected
35.How to Change Query Timeout Value for MS Access SQL Queries
36.What is Northwind Traders database

Copyright © 2024 GeeksEngine.com. All Rights Reserved.

This website is hosted by HostGator.

No portion may be reproduced without my written permission. Software and hardware names mentioned on this site are registered trademarks of their respective companies. Should any right be infringed, it is totally unintentional. Drop me an email and I will promptly and gladly rectify it.

 
Home | Feedback | Terms of Use | Privacy Policy