Building the query
Video: Building the queryThe next step in creating our reporting tool is to construct the query. I'll go up to the Create menu and I'll create a new query in Design view. Let's add the tables that we're going to need. First, I'm going to choose my Orders table. This will give us information about the OrderDate. I'll choose my Products table. This will give me information about the product's price that will aggregate over the year. I also need information for my States table, but in order to get there I need to have a link. We'll go to DirectCustomers. That will give us the customer that ordered it and the state it went to, and then we can go to States, which will give us the states from the Customers table.
- Next Steps
Viewers: in countries Watching now:
In this course, author Adam Wilbert illustrates how to create and leverage real-world queries and turn raw data into usable information. The course covers setting up queries, performing calculations, using the built-in Access functions to further refine query results, and identifying top performers or areas for improvement based on a range of criteria.
- 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
Building the query
The next step in creating our reporting tool is to construct the query. I'll go up to the Create menu and I'll create a new query in Design view. Let's add the tables that we're going to need. First, I'm going to choose my Orders table. This will give us information about the OrderDate. I'll choose my Products table. This will give me information about the product's price that will aggregate over the year. I also need information for my States table, but in order to get there I need to have a link. We'll go to DirectCustomers. That will give us the customer that ordered it and the state it went to, and then we can go to States, which will give us the states from the Customers table.
Let's go ahead and close the Show Table window and I'll rearrange these just a little bit here. Move my customer over here and expand it. Okay, so now we can see the relationships between the four tables in our query. I got my Orders table, from which we'll extract the year from this date. Linked to that I have got my Products table which will take the price and aggregate over the year. I also have my DirectCustomers table, which has the State that the order went to, and from the States lookup table I can get the full name of the state instead of just the abbreviation, and I'll also get the DivisionName that we'll aggregate by.
So these are the four tables we'll use in this query. Let's go ahead and populate the fields into the field area that we'll use in our report. The first field we want is a year and we can construct this using the Expression Builder. So let's right click on this field and go into our Expression Builder. We'll type in the year function, year(, and then we'll double-click on it in the table. We'll drill down into our TwoTrees database, Tables, and we'll go to our table tbl_Orders. From there we'll double click on OrderDate.
Finally, we'll add a closing parenthesis to finish this statement and say OK. The next field we want to add is our Price. Let's double-click on that in the Products table and we'll turn on our Totals row, so that we can sum those together to get the total price over the year. In the Total row I'll change Group By to Sum. The last two fields will be our StateName and our DivisionName and we'll use these full text titles in the report. Let's go ahead and run this query to see where we're at and we'll see we've got the years.
We've got the total price, each state listed, and then the division that those states are in. Let's go back into our Design view and we'll input our query parameters. We want the Criteria for our year column to come from the Year drop-down menus on our form. We can select those using the same Expression Builder that we used earlier. In the Criteria row we'll right-click and say Build. We'll drill down into our database. This time we'll go into Forms > All Forms, and if you have saved your form earlier you can use that, but I'll go ahead and choose this frm_SalesByDivision-complete form.
From there I'll choose those combo boxes, cbo_Year. I'll double-click on it to add it to my expression. So our form had two drop-down boxes. We have the year and the comparison year. We want our query to return results based off of both values, so I'm going to add an Or statement here and I'll double-click on cbo_CompYear. We'll go ahead and say OK. The other parameter is going to be based on the DivisionName. We'll go to our Criteria for Division, right-click, Build, we'll do the same thing.
We'll drill under TwoTrees Forms > All Forms > SalesByDivision-complete > cbo_ division. I'll double-click on that, type in the word Or, and double-click on CompDivision. This will link the two drop-down menus for divisions in our form as query parameters. We'll go ahead and say OK and there's our completed query. If at this point we try and run it, Access is going to look for our form, which I currently don't have opened, and it's not going to know what data is in here.
So let's go back into our Chapter 7 folder. We'll open up the form and we'll input those values. I'll go ahead and say New England for the year 2008 compared to let's just say Pacific for the year 2008. Now that these values are in the form, when I run the query the query parameters are going to look to the form and find the values that I'm looking for. So let's go ahead and switch into the data sheet view and you'll see that I get the information that I asked for in the form.
I've got the year 2008 and I've got the Pacific and New England divisions. So now that we have two pieces down, the last step is to create the report that's based off of these query results, and we'll do that in the next movie.
There are currently no FAQs about Access 2010: Queries in Depth.