Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Creating an IIf function

From: Access 2010: Queries in Depth

Video: Creating an IIf function

In this video, we will begin writing our own conditional statements using the Query Designer in Access. We will be using the Expression Builder to help us generate proper syntax and we'll be using some mathematical expressions to evaluate our data. So let's get started. First, we need to define the question. The question we want to answer with this example is which sales team members met the goal of $20 in the month of June 2010? First, we will build a select query to fill trap only the information we need from the database. We will go up to the Create tab and create a new wuery in Design view.

Creating an IIf function

In this video, we will begin writing our own conditional statements using the Query Designer in Access. We will be using the Expression Builder to help us generate proper syntax and we'll be using some mathematical expressions to evaluate our data. So let's get started. First, we need to define the question. The question we want to answer with this example is which sales team members met the goal of $20 in the month of June 2010? First, we will build a select query to fill trap only the information we need from the database. We will go up to the Create tab and create a new wuery in Design view.

We will add the Employees table, the Orders table, and the Products table. Let's go ahead and close Show Table and we will start filling in the fields. From the Employees table, let's add Employee ID and FirstName. From the Orders table we will grab the date and from the Products table we will grab the Price. Now let's start filling in our criteria. For the OrderDate, we're only interested in records that fall between June 1st, 2010, and June 30th, 2010. So let's expand the field and we will write that criteria.

I'll write Between 6/1/2010 and 6/30/2010. I will go ahead and press Enter to accept that value and Access updates our syntax. Now let's take a look at Price. For the price, we are looking for the sum total over the entire month. So activate our Totals row and change the Grouping level to Sum. The last thing we need to do is change the Show checkbox for the OrderDate to tell Access that we are not going to show this field, but we just want to use it for filtering.

We also don't want to group by the OrderDates, so we will change this to a Where clause. So let's go ahead and run our query to see the results. We can see that we have 36 employees that had sales in the month of June and we can see their total sales here in this column. Let's go back into Design view real quick and we will change our column header so we can reference it later on in the Expression Builder. Go into Design view and before Price, we will enter the alias June Sales followed by a colon.

Let's try it one more time to make sure everything looks good and I think that looks great. So let's go ahead and save it out. We will save this as qry_JuneSales. I will press Ctrl+S, qry_JuneSales. The next thing we want to do is create a new query that include the conditional statement that will evaluate the calculated field here and tell us whether that meets or is below sales plan. We will go to the Create tab and we will create a new query in Design view. This time instead of building it up from the table, we will build it off of the query we just constructed.

We will switch the Querys tab and find the JuneSales query we just created. I will scroll down here and there it is there. We will add that to our pane. Let's go ahead and close Show Table. We'll add JuneSales to the field and we will build our wxpression in the second column. I will right click on the Field and say Build. Now, to construct our IIf condition, we can find that under Functions > Built-in Functions. I will scroll down to Program flow and there is IIf.

I will double click on it to add it to my expression. We can see that there is three required parts. We have the expression to evaluate as true or false, the part to return if it's true, and the part to return if it's false. So the expression that we want to evaluate is whether the June sales total is over $20. So let's go ahead and write out that expression. I could finally calculate a field if I drill into my database, in the queries. Let's open this up a little into JuneSales and there is the JuneSales calculated field.

I will double-click on that. Now we want to evaluate if this field meets the goal of $20. So we will say JuneSales is greater than or equal to 20. If the JuneSales are greater than or equal to 20, we are going to say that they "Met Sales Plan." So I'll return that text written in quotes. If the JuneSales is below 20, they did not meet the sales plan. So for the false part, I will write "Did Not Meet Sales Plan" and I will wrap that in quotes.

So there is our completed expression. If the JuneSales is greater than or equal to 20, then they met sales plan. If the JuneSales was not greater than or equal to 20, they did not meet the sales plan. Let's go ahead and say OK and that will add to our query. We can see Access put in an alias here that reads Expr1 and that's not very descriptive. So let's go ahead and change that to Sales Plan. I will highlight it and say Sales Plan. Let's add a little bit more information to our query here so we can see what the results are. We will invoke the Show Table window.

This time we'll use the Show Table button on the ribbon and we will use the Employees table. Let's go ahead and close this out and we can add EmployeeID and FirstName to our query for reference. Okay, let's go ahead and run it and see what the results are. We can see that we have a total of 36 records. Those are the 36 employees that had sales and we can see that we have a calculated field here using our expression to tell us whether they Met Sales Plan or Did Not Meet Sales Plan. Now we have a little issue here with the way the query is returning the results. If you think back to that movie where we talked about joins, you might realize that some of our employees didn't have sales at all and that they should be represented in this list as well.

So let's go back in our Design view and we will add those people to our query. Back in Design view, I will double click on the line that joins the JuneSales query with the Employees table. Now the option that we are looking for is to Include ALL records from the Table Employees and only those records from JuneSales where the join fields are equal. This means we'll have all the employees whether they had sales or not and for the employees that did have sales we will have their data. Let's go ahead and say OK and take a look at our results.

Now we have 200 people and that's not right either. Now we are getting all of employees in the entire company, not just the Sales team. So I have to add one more field to our query to finish it up. Let's go back into Design view. We will add our department to the query. And we want to show the criteria is just the Sales department. Okay, this should wrap it up. Let's go ahead and go Run and we will double check our results. There's our 84 sales team members. For the people who had over $20 in sales, they met their sales plan.

For the people who had under $20 of sales or didn't have any sales at all, they did not meet the sales plan. So there is our first conditional statement. We made use of the Expression Builder to help us format our statement properly and to reduce errors and we were able to verify that returned the expected result. I hope that at this point, you're beginning to see why I like conditions so much. Being able to instruct Access how to make a decision on its own is pretty cool. But more importantly, I want you to take a moment before moving onto the next movie and think about all the ways that your workflow could benefit from using conditional functions, and I promise that once you start looking you are going to come up with a ton.

They're extremely flexible and can help out in unexpected ways. In the next movie, we will look at expanding the usefulness of the conditional statement by making use of several IIf functions that sit together. We will see you there.

Show transcript

This video is part of

Image for Access 2010: Queries in Depth
Access 2010: Queries in Depth

46 video lessons · 14234 viewers

Adam Wilbert
Author

 
Expand all | Collapse all
  1. 9m 9s
    1. Welcome
      1m 10s
    2. Using the exercise files
      41s
    3. Introducing the database
      4m 29s
    4. Previewing the course
      2m 49s
  2. 17m 17s
    1. Understanding queries
      3m 31s
    2. Following naming conventions and best practices
      2m 56s
    3. Using the Query Wizard
      5m 21s
    4. Exploring the design interface
      5m 29s
  3. 26m 39s
    1. Defining criteria
      5m 40s
    2. Understanding comparison operators
      3m 19s
    3. Defining the column headers
      2m 49s
    4. Exploring the property sheet
      7m 32s
    5. Printing query results
      2m 41s
    6. Working with joins
      4m 38s
  4. 14m 14s
    1. Understanding parameter queries
      4m 27s
    2. Obtaining parameters from forms
      5m 17s
    3. Creating a combo box
      4m 30s
  5. 23m 24s
    1. Understanding the Totals field
      5m 31s
    2. Creating aggregate calculations
      3m 31s
    3. Exploring the Expression Builder interface
      4m 28s
    4. Using mathematical operators
      5m 46s
    5. Applying text functions
      4m 8s
  6. 24m 23s
    1. Understanding dates as serial numbers
      2m 42s
    2. Specifying a range of dates or times
      3m 47s
    3. Formatting dates
      4m 31s
    4. Using other Date/Time functions
      3m 47s
    5. Defining today's date
      2m 41s
    6. Calculating time intervals
      6m 55s
  7. 20m 9s
    1. Introducing the conditional IIf function
      2m 57s
    2. Creating an IIf function
      7m 31s
    3. Nesting IIf functions
      4m 57s
    4. Using the Switch function
      4m 44s
  8. 20m 41s
    1. Understanding the reporting tool
      2m 13s
    2. Building the form
      6m 57s
    3. Building the query
      5m 4s
    4. Building the report
      3m 30s
    5. Finalizing the reporting tool
      2m 57s
  9. 25m 37s
    1. Finding duplicate records
      2m 17s
    2. Identifying unmatched records
      2m 29s
    3. Creating crosstab results
      2m 57s
    4. Creating backups
      1m 29s
    5. Creating update queries
      3m 22s
    6. Making, deleting, and appending records
      5m 36s
    7. Uniting tables
      3m 16s
    8. Embedding SQL code in queries
      4m 11s
  10. 1m 0s
    1. Next Steps
      1m 0s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Access 2010: Queries in Depth.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.