Using the IIF function in Access and Creating Invoice-Type
Reports
This tutorial willl use the Homeworks database: homeworks.mdb
Download this file to your U: drive before using!
Using the IIF function:
The IIF function is used to create conditional values in Access. It is useful
when two different values are needed in two different situations.
The format is
IIF( conditional
statement, true value, false value)
- The conditional statement
is something like Scores > 50, or Paid = true.
- If the conditional statement
is true, the value of the IFF expression is the true
value.
- If the conditional statement
is false, the value of the IFF expression is the false
value.
Examples:
Using IIF in a calculated field:
- In the Homeworks database from above, create a query using all three tables
and all the fields from each table that are unique and not keys Put the fields
in this order: Last Name, First Name, Description, Score, MaxScore.
- Run the query to make sure it works.
- Add a new calculated field with the following as a field value:
Warning: IIF( (Score/MaxScore) <
0.5, "Low Score!" , "")
- This will create a new field called "Warning" and will display
either "Low Score!" if the percentage score on any homework is less
than 50%. Otherwise nothing will be displayed.
- Run the query to make sure it works.
- Save the query -- call it "HWScores"
Creating an Invoice-Style Report
An invoice-style report is essentially a master-detail report where each "customer"
is on a new page.
To create an invoice-style report in the Homeworks database:
- Create a report using the wizard.
- Select the HWScores query from above.
- Select all the fields.
- Select LastName and FirstName as grouping fields. LastName should have the
higher priority.
- Sort the records by the homework description.
- Select "Stepped" layout.
- Choose whatever report style you'd like. . Check that it comes up showing
a nice master-detail display.
- Go to the Design mode for the Report.
- Move the FirstName field control (the one that is in its own row -- not
the column title) up to the same row as LastName.
- Right click the FirstName and select Properties. Select All and then change
Border Style to "Transparent".
- Change the First Name font to Arial, 14 pt. You may need to resize it field
control.
- Anywhere on the grid, right-click and selecct "Sorting and Grouping".
- On the row for "FirstName", select Group Header = No.
- Right-click on the "LastName Header" bar and select "Properties"
- Under "All", set "Force New Page" to "Before Section"
- That's it!