Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Business and personalized recommendations.Start Your Free Trial Now
- View Offline
- Naming conventions and best practices
- Working with joins
- Using comparison operators
- Defining criteria for select queries
- Creating parameter queries
- Creating calculated fields
- Working with dates and times
- Using the Expression Builder
- Creating conditional statements
- Making, deleting and appending records
- Building reports
Skill Level Intermediate
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.