Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Within the Program Flow category of functions in the Expression Builder is another function called Switch. The Switch function is similar to IIf in that it allows Access to return various responses depending on the results of an evaluated expression. It does this by using matched pairs of expressions and values. We have exprA and if that's true we return valueA. If the exprA is False, we'll move on down the line to exprB and evaluate it. In this way, Switch only branches in the false direction.
In other words, Access only returns a value if the function is true. If it's not, it moves on down the line. If the condition evaluates to false at the very end, Access returns a null value or a blank cell. So let's take a look at an example of how we can apply this. In this example, we want to take a look at each employee's total lifetime sales with the Two Trees Olive Oil Company as part of our rewards and recognition program. Let's open up our Chapter 6 custom group, and we'll open this qry_TotalSales query. We'll see that we've got a column for Employee Name, the Sales Department, and this is the Sum Total of all the sales throughout their entire tenure with the company.
We'll use this query as a basis for deciding what level of recognition they're at. We'll go to our Create tab and create a new query in Design view. Let's go ahead and grab that query, let's enter the Queries tab, and we'll look for qry_TotalSales. We'll double-click on it to add it to the query. Go ahead and say Close and we'll add each field to our query. Double-click on EmpID, FirstName, Department, and TotalSales.
In the next field, we'll create our switch expression. So we'll right-click and say Build and we can find that in the Program group under Functions > Built-In Functions > Program Flow > Switch. Now Switch needs matched pairs of expressions and values. So the expression we want to evaluate is whether they're in our Platinum group, which means they're over a thousand-dollar sales level. Let's go into our TwoTrees database, we'll go into Queries, and we'll find the qry_TotalSales query.
From there we'll grab the TotalSales calculation. We'll double-click on it to add it to the expression. So the evaluation expression that we need is TotalSales > 1000. If that's true, we're going to say they're in our Platinum group. So for the value, I'll type in Platinum. We'll wrap that in quotation marks and we'll add a comma to tell Access that we're done with the first pair and we're ready to start typing out the second pair. Now we're going to look for our TotalSales as being over $900. So we'll go back to our query, we'll double- click on TotalSales again, and we'll say greater than 900.
If that's true, they're in our Gold group. We'll do another one for 800 at the Silver level and we'll do one more at the Bronze level for $700. So there is our statement. We've got a Switch function. We're going to look at TotalSales > 1000, and if that's true, they're in our Platinum group.
If that's not True, we'll go to the next. TotalSales > 900. Oh, and I see I have a little typo here. We'll fix that. We'll change that from less than to greater than. So TotalSales > 900, and if that's true, we'll say they're in our Gold group. If that's false, we'll move on. TotalSales > 800. If that's true, they're Silver. if that's false, we'll move on. Are they greater than 700? If they are, that's Bronze group. If not, Access will return a null value, so it'll just be a blank cell.
Okay, let's go ahead and say OK. So we'll go ahead and run our query to see the results. Let's sort based upon our Total Sales. We'll sort Largest to Smallest. Now we can see all of our employees that in our Platinum group, all of the employees that are in our Gold group, all the employees that are in Silver, Bronze, and all the employees who've not yet leveled up. So when used appropriately, both IIf and Switch can be extremely valuable tools to have in your pocket. Anytime you can automate routine tasks and allow Access to make decisions for you, it can be a huge benefit to increasing your productivity.
But beyond simply taking decision- making tasks off your plate, learning to use the Program Flow functions increases the reliability and consistency of your database. One of the biggest benefits is that they can ultimately prevent data inconsistencies due to the introduction of human error.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 74465 Viewers
80 Video lessons · 129695 Viewers
52 Video lessons · 63939 Viewers
59 Video lessons · 49720 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
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.
Your file was successfully uploaded.