navigate site menu

Start learning with our library of video tutorials taught by experts. Get started

Access 2007: Queries in Depth

Access 2007: Queries in Depth

with Adam Wilbert

 


In Access 2007: Queries in Depth, 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. Exercise files are included with the course.
Topics include:
  • Naming conventions and best practices
  • Working with joins and primary keys
  • Using comparison operators
  • Printing query results
  • Creating parameter queries
  • Creating calculated fields
  • Using the Expression Builder
  • Making conditional statements
  • Appending queries
  • Updating queries

show more

author
Adam Wilbert
subject
Business, Databases
software
Access 2007
level
Intermediate
duration
2h 8m
released
Sep 09, 2010

Share this course

Ready to join? get started


Keep up with news, tips, and latest courses.

submit Course details submit clicked more info

Please wait...

Search the closed captioning text for this course by entering the keyword you’d like to search, or browse the closed captioning text by selecting the chapter name below and choosing the video title you’d like to review.



Introduction
Welcome
00:04Hi! My name is Adam Wilbert and I would like to welcome you to Access 2007: Queries in Depth.
00:09In this course, I am going to reveal the tools that you will need to answer some
00:12of the most complex questions that you might have about your data.
00:14I will walk you through some real world scenarios and show you how to leverage
00:17queries to bring a new level of insight and clarity to your raw data tables.
00:21We will look at setting up queries from scratch to filter and sort your data
00:24in your database and I will demonstrate how to perform calculations on
00:27numerical values and use some built-in Access functions to further refine the
00:31way your data is presented.
00:32We will also use queries to identify top reformers as well as to automate some
00:36repetitive analysis task.
00:38Now, I like to say that a good database can give you more than what you put
00:40into it and queries is where that magic happens, where raw data becomes useful information.
00:45Together we are going to cover all of that and more in Access 2007: Queries in Depth.
Collapse this transcript
Using the exercise files
00:00If you're a premium member of the lynda. com Online Training Library, you'll have
00:04access to the Exercise Files used throughout this course.
00:06The Exercise Files are in the Excess Files folder, which I have placed on the desktop.
00:10You can store it wherever you like.
00:12There are files for most of the movies and they reside in subfolders named
00:15according to the chapters.
00:17It's not necessary for you to use these files; you can use your own files in place of them.
00:21If you're a monthly or annual subscriber to lynda.com, you don't have access to
00:25the Exercise Files, but you can follow along with your own work.
00:28So let's get started.
Collapse this transcript
Reviewing the Access interface
00:00Before we can get into learning about queries in depth, I wanted to review
00:03some of the common Access interface elements that we will be using throughout this course.
00:06On the left-hand side, we have a navigation pane which lists all of the tables,
00:09queries, forms, and reports within our database.
00:12If it's not currently open, you can toggle it by pressing F11 on your
00:15keyboard and then once it's open, you can continue pressing F11 to minimize and expand it.
00:19Microsoft Access uses the same Ribbon interface that's common throughout the
00:22entire Office 2007 suite.
00:25Our tabs at the top might change depending on the task that we are
00:27currently performing.
00:28We are going to spend a lot of time in this Create tab using the Query Wizard
00:32and query design tools.
00:33Finally I want to take a look at our Database Tools tab and the
00:36Relationships window.
00:37The Relationships window describes the relationships between all of our tables
00:40within our database.
00:41Now, if any of this seems unfamiliar to you, you might want to review
00:44the Access 2007 Essential Training title available in the lynda.com Online Training Library.
Collapse this transcript
1. Creating Queries in Access
Query basics
00:00The data in our database can become overwhelming with tables growing to
00:03thousands of records easily.
00:05Luckily, Access provides us with a set of objects called queries that we can use
00:08to quickly filter out and get to the information that we're interested in.
00:10But before we can get into making queries, we need to understand the data that
00:13we have available to work with.
00:15Now if you're inheriting a database like we are today, you want to go through
00:18all of the tables to see what's in them.
00:19If you have been working with your database from scratch, you are likely to be
00:22intimately familiar with the data that's in there.
00:25But for now we are going to go ahead and go through our tables and see what
00:27we have to work with.
00:27Right now I am going to open up our tbl_DirectCustomers table and we will see
00:32that this table contains some information about all of the customers that have
00:34done business with the Two Trees Olive Oil Company.
00:36We have got our FirstName, our LastName, phone number, some contact information,
00:41their email address.
00:42On the far right side, we have a Newsletter column that simply tells us whether
00:45they've subscribed to our online newsletter or not.
00:47I can go ahead and close this table and then open up the Employees table, and we
00:51will see that we have some similar information about the employees that work for
00:54the Two Trees Olive Oil Company.
00:55We have got FirstName, MiddleName, LastName, contact information and email
00:59address, and scrolling right, we can also see some information about when
01:03they're hired, the rate of pay that they currently receive, and the department that they work in.
01:07I am going to close this and go to our Orders table.
01:11The Orders table has an OrderID, and this is a unique ID for each order that's placed.
01:15We have got the date that the order was placed, an ID number that links to
01:19customer that placed the order, an ID number that links to the product that was
01:22ordered, and an ID number that links to the employee that assisted them with the order.
01:25I can close this and open up our Products table and we will see those
01:30ProductIDs that we just saw in the Orders table.
01:32We can also see what the name of that product is, the code for the specific oil
01:36that's in the product, the size of the product in ounces, the wholesale cost,
01:41the retail cost for the customer, and then we have some information about how
01:45much that's marked up.
01:45So we have got the markup in dollars and the markup in percent.
01:48I am going to go ahead and close the Products table.
01:50There are mainly four tables in our database give us some lookup information
01:53about some of our other products.
01:54So if I open up the GradeID, we will see that we have a numerical code for the
01:58grade and then we have the text that code represents.
02:02Similarly for states, if I open that up, we see we have a two-digit abbreviation
02:07for the states, and then the text that represents.
02:09I am going to go ahead and close that.
02:11Now, we can see the relationships between all of the tables in our database by
02:14going up to Database Tools, and then clicking on Relationships, and this will
02:18open up the Relationships grid, where we can see all of our tables laid out and
02:21how they relate to one another.
02:23So we can take a look at the Orders table here and moving to the left we can
02:27see that the order is linked to a product that was ordered.
02:30The products are linked to the OilID, and the Size, and also the Grade.
02:35Moving right from the Orders table, we see the customer and the employees that
02:39are associated with that order, and we also have a lookup table for states that
02:42feeds information into both our customers and our employees.
02:44So let me go ahead and close our Relationships window and we are going to take
02:48a look at how we can get some more information out of our Employees table now.
02:51So I am going to open up our Employees table again and if you're used to
02:55working in Excel, you might be familiar with some of the filtering options that are in Excel.
02:58For instance I can click on Illinois here, IL, and in Access if I right-click
03:03on that and then say Equals 'IL', it will filter down to all of my employees
03:07that live in the state of Illinois, and you can see on the very bottom here we
03:10have got four employees.
03:11If I now left-click on that icon, you will see that we have got a list of all
03:15the states that are represented in our database for the Employees table and you
03:18will see that Illinois is currently selected.
03:20I can turn on another state, for instance Kansas, KS, and if I go ahead and
03:24say OK to that, it will show me all the employees that either live in Illinois or Kansas.
03:28left-clicking again, I can scroll through the list and I will turn off
03:32Illinois and now I am just down to Kansas, and you can see that I have got
03:36total of three employees that live in the state of Kansas.
03:39Going through the Filter menu can be a little bit tedious and you see that if I
03:42go ahead and close our table now with the filter in place, it's going to ask me
03:45if I want to save the table.
03:46And I can go ahead and say Yes.
03:48Now, if I go ahead and open up the Employees table again, you will notice that
03:50our filter is no longer applied.
03:52In the Sort & Filter options on our Ribbon here, we have got a button here
03:56called Toggle Filter.
03:57You will notice that if I press that to toggle it back on, it returns to the
04:00state where Kansas is selected.
04:01But it no longer gives us any information about state of Illinois, and you can't
04:05go back-and-forth. You can only save one filter at a time.
04:08The better way to do this is to go through a query.
04:10So let me go ahead and show you one of our queries that we've got set up right now.
04:14Under queries, I have got a qry_ EmployeesAZ and if I double-click on that,
04:19you will see that it gives me just the information for the employees live in Arizona.
04:22I have also got one for New York.
04:24I can go ahead and save as many of these queries as I want.
04:26It will give us information about just the state that we are interested in.
04:29Now, every time you run a query, Access creates what's called a record set and
04:33basically what a record set is, is a sub-selection of your main table.
04:37So every time I run this Employees New York query, Access goes back to our
04:40Employees table, grabs all of the employees that live in New York, for instance
04:44let me remove this filter and switch to New York, and you'll see that we have
04:50our three employees that live in New York, which are the same three employees
04:53that's being returned by this query.
04:54Every time you run a query, Access goes back to the Employees table and pulls
04:58the most current information.
04:59Now, in our database, we also have a form called EmployeeLookup and this is
05:02a little more advanced way of running a query and we will get to this later in this course.
05:06Basically if we were to run this form, we could select from a list of states,
05:09and that will provide us all of the available options instead of having a single
05:12query listed out for Arizona, New York, and every state possible.
05:15So you don't need to have 50 queries to do a single task.
05:19So while at their most basic level a query is simply a sub-selection of a table --
05:22we can get some of that information from running a filter on our table --
05:25a query is definitely a much more robust way to go.
05:27We can save our queries out, and we can return to them at anytime.
05:31As we will see throughout the course, queries can also do some more complex
05:34tasks that you can't simply do with a filter.
Collapse this transcript
Naming conventions and best practices
00:00Now the user interface in Access gives us a little bit of a clue as to what type
00:04of object we are dealing with.
00:05We have got an icon here that indicates a query, we have also got an icon here
00:08that indicates a table.
00:09So let me go ahead and open up a table.
00:10We have got a DirectCustomers table.
00:12I am also going to open up our EmployeesAZ query.
00:14You will see that the structure of their query is almost identical to the
00:17structure of the table.
00:18Without the three letter code here I'd be hard-pressed to tell that this was a
00:21query and not a table.
00:22Now, we do have the interface here.
00:24It shows us a little icon and I know that this is the icon for a query and this
00:27one is icon for a table.
00:28But there are a lot of areas in Access where Access will lump all of our queries
00:32and tables together, and it won't give us any icons, and it won't give us any
00:35indication of whether we are actually looking at a query or a table.
00:37For instance if I go up to our Database Tools and then I click on our
00:41Relationships window, we will see that Access puts all of our tables in the
00:44relationship, but it doesn't show us any icon indicating that these are tables.
00:48If I click on Show Table, and then select Query, I can add a query to our
00:53relationships, and again it doesn't show us any icon to indicate that it's a query.
00:57So without the tag saying that this is a query and that this is a table, I'd be
01:01hard-pressed to tell the difference between the two.
01:03So let's take a look at some of the tags that we will be using throughout the course.
01:06We will be using the tag tbl to indicate a table, we will be using the tag tlkp
01:10to indicate a Lookup Table which is basically a simple table that just provides
01:14some additional information.
01:15We will be using qry, and this is a big one, for queries, rpt for reports, and
01:21frm for Forms, mcr for Macros and we can also use these codes to indicate other
01:27objects within our database.
01:28for instance cbo is a Combo Box and cmd stands for Command button.
01:32Now, let's go back into Access and see how those fits together.
01:35In the Navigation pane we can see how using these three letter codes will help
01:38us keep our database structured and organized.
01:40Now Access is perfectly happy letting you name your objects whatever you'd like.
01:43You can use long file names, spaces, capital letters whatever you want.
01:47Access will take care of all the organization behind the scenes.
01:50But as we move deeper into queries, using these three letter codes will help us
01:53be able to read our query, and understand how they're functioning.
01:56If you think that using this type of naming convention is a little bit complex
01:59to the type of database that you'll be working with, like I said, feel free to
02:01break all of these rules.
02:02You can name your objects whatever you like and Access will work just fine.
02:05If however there is a slimmest of possibilities that your project will grow
02:08beyond a simple personal endeavor, then I would encourage you to consider
02:12putting some of these practices into place.
02:14Being consistent and deliberate in your methods will only help you out down
02:17the road.
Collapse this transcript
Using the Query Wizard
00:00As with many of the tables and objects that you'll create within Access, one of
00:03the easiest way is to get started is to use one of the built-in wizards.
00:06We could find the Query Wizard within the Create tab, so I'll move up to the
00:09top and then on the far right side we've got our query options.
00:12We've got Query Wizard and Query Design.
00:14Let's go ahead and open up the Query Wizard, so I'll click on the button there
00:17and we'll see that we have four options.
00:18We've got a Simple Query, Crosstab Query, Find Duplicates Query and
00:22Find Unmatched Query.
00:23We'll talk about these last three in a future movie, but for now we are going
00:26to select the Simple Query Wizard.
00:28So, go ahead and say OK.
00:30The next window that opens up gives us the ability to build our query.
00:32We've got a drop-down list that shows all of the queries and tables within our database.
00:36Right now, I am going to select our customers, so tbl_DirectCustomers, and in the
00:41lower two panes we've got the Available Fields, which is all the fields from the
00:45DirectCustomers table, and then we've got the Selected Fields that will be
00:47actually used in our query.
00:48I will take the FirstName, select it, and using the single arrow, we are going
00:53to move that to the right to move it to Selected Fields.
00:55I can do the same thing with the LastName, move that over, and we are also going
00:59to pull the Email address over.
01:00Now, I've got a couple of other buttons here.
01:02If I wanted to choose all of our fields, I can click the double arrow over and
01:06that will move everything over.
01:07If I want to move everything back, I can use the double arrow back or I can
01:10move them individually.
01:12So I can move them one at a time over, or I can move them all back.
01:15When you're selecting a field, you can also double-click on it as a shortcut,
01:18instead of using this button to move it.
01:19So, I'll go ahead and double-click on FirstName, double-click on LastName, and
01:22double-click on Email.
01:24Let's go ahead and say Next and Access brings up the option to save our query.
01:28We are going to go ahead and name this one CustomerEmail, so using our three
01:32letter tags, I am going to say qry_CustomerEmail.
01:36We had two additional options here, to Open the query to view information, or we
01:40can Modify the query design.
01:42We'll take a look at the query design here in the next movie, so for now I'll go
01:44ahead and just open it up to view the details that are pulled.
01:46We'll go ahead and say Finish, and we'll see that our query if pulling
01:49information from our Customers table. We've got a FirstName, the LastName and
01:53the email address for each customer.
01:54Let's go ahead and close this query and we'll jump into the Query Wizard one
01:57more time and we'll take a look at some additional options.
02:00So Query Wizard, we are going to choose the simple query again, go ahead and say OK.
02:05This time I am going to choose our Customers table and we'll grab the
02:08FirstName and the LastName one more time, and this time I am going to choose the second table.
02:12So, we are going to pull information from two different tables. We are going to
02:14choose the tbl_Products table, and we are going to choose the price.
02:17I am going to go ahead and say Next, and you'll notice that we get an additional
02:21screen this time that we didn't the last time.
02:23The first query that we created, the Customer Email, only had names and email addresses.
02:27It didn't have any numerical data.
02:28This time we are including the price, which is a numerical field, and
02:31Access gives us some additional options that we can use when building a
02:33query with numerical data.
02:35We could choose our summary and then take a look at some of the options that are available.
02:39From our Price field, we can choose some of the summary values that we want to
02:41have Access calculate.
02:42We can choose the Sum, which is the grand total, the Average, the Minimum, the
02:46Maximum, and we can also count up the number of records that are returned.
02:50Now, this will be easier to understand what's going on in the results, so let's
02:53go ahead and just select them all for now and say OK.
02:55Go ahead and say Next and this query we are going to go ahead and name
03:00OrderHistory, so once again qry_OrderHistory.
03:05One more time we are going to open up to view the information and say Finish.
03:09Now, I am going to go ahead and expand these cells open a little bit so you can
03:12see the numbers that have returned.
03:14To make things a little bit more clear, we are going to go ahead and sort on
03:17the summary of price.
03:18So, clicking on that little drop-down arrow, we are going to choose Largest to
03:21Smallest, and this will sort all of our summary prices to the top.
03:24Now, we can see Sebastian Rich is the customer that spent the most money with
03:27the company. He has floated up to the top.
03:29The total amount of money that Sebastian has spent with the company is $260.24.
03:33We can see that the average order that he places is approximately $43, the
03:38smallest order he has replaced with $16.33, and the largest order he ever
03:42replaced was $80.83.
03:44We can also see that Sebastian has placed the total of six orders with the Two
03:47Trees Olive Oil Company.
03:48So, the Query Wizard provides a quick and easy way to get started writing queries.
03:51You can combine fields from multiple tables and you can apply some basic
03:55aggregate calculations as well.
03:56The Simple Query Wizard walks you through the build process step-by-step, and it
03:59can provide a fairly solid base from which you can build upon later.
04:03We are going to take a look at the Design Environment in the next movie where we
04:05can build upon this query.
Collapse this transcript
Exploring the Query By Example (QBE) interface
00:00All of the queries in Access are stored in a common programming language called SQL.
00:04SQL stands for Structured Query Language, and it could be a pretty powerful way
00:07to create queries, but a little bit daunting at first.
00:10So, Access actually provides a second way that we can create our queries called
00:13the QBE grid or the Query By Example.
00:16I am going to go ahead and open up one of the queries that we just created
00:18in the previous movie.
00:19I am going to open up the qry_ CustomerEmail query, and if I double-click on it
00:22you'll see that the query runs again, and it's pulling the FirstName, the
00:25LastName, and the Email address out of our DirectCustomers table.
00:28Now, there are a couple of ways that we can switch to the QBE environment.
00:32At the very top on the Home tab we've have got this icon here that's a triangle
00:35and a pencil and a ruler and this will switch us to our Design View.
00:38I can also select on the drop-down menu below that and select from a couple of
00:41additional views that we have.
00:43So, we've got the Datasheet View, which we are looking at right now. I also have
00:46PivotTable, PivotChart, SQL View if you want to take a look at the text behind
00:49the scenes, and we have our Design View as well.
00:51There is another option that we have when we want to switch between Datasheet
00:54View and Design View, and we can find that in the lower right-hand corner.
00:57We've got a couple of little icons down here and these are really tiny.
01:00We've got the Datasheet View, the PivotTable View, the PivotChart View, SQL View, and
01:05finally in the lower right corner we've got a small icon for Design View.
01:09I use these all the time, because they are really convenient, they are always
01:11there, and sometimes it beats moving up to this menu, opening the drop-down menu,
01:15and then selecting the view that you want.
01:16But right now we are going to go ahead and just select Design View, so you can
01:19use whatever method you like.
01:20We'll take our query that we opened up and we'll move it into our Design View.
01:23You'll notice that when we switch into Design View, Access opens up a new tab
01:26called Design and we've got some additional options here that are specific to
01:29the query design environment.
01:31The Results section here has two options. We change our View again, and we can also run it.
01:35Now, for most of the queries that we are going to be creating in this course,
01:37running is actually identical to viewing the Datasheet View.
01:40We'll talk about why that's different when we run action queries later on in the course.
01:44The second section is Query Type, and you'll recognize some of these from the wizard.
01:47We've got the Append, Update, Crosstab. These are all the different
01:50queries that we can create.
01:52Right now, we are doing a simple select query, so that's the option that
01:54we've got selected.
01:56The third section of the menu is called Query Setup, and this is where we can
01:59choose some of the options that will help us build our query.
02:02The fourth section of the menu, called Show/Hide, has some of the options that we
02:05can use to manipulate the grid below, and we'll take a look at that in moment.
02:08So, the Query Design environment has two windows.
02:10We've got the upper pane, it's called the Table pane, and this will have all of the tables that we are
02:13using to create our query.
02:15Right now, we've got our DirectCustomers table. The lower half of the window is
02:18the Design Grid and down here we can take a look at all of the tables that are
02:21being used to create a query.
02:23So, we can see that we are pulling the FirstName from the DirectCustomers table.
02:26We've got the LastName from the DirectCustomers table and we've got the Email
02:29from the DirectCustomers table.
02:31We can move the tables around for organization.
02:33We can expand them open a little bit if we want to view all the fields within each table.
02:37To add additional tables to our query, we've got a couple of options.
02:39We've got the Show Table button up here. We can also right-click in any open
02:42space here and choose Show Table.
02:44That will bring open a new window called Show Table and we could choose any
02:48additional tables from our database that we want to include in our query.
02:50For instance I can include a tbl_Products.
02:52We'll go ahead and close this out, and I am going to expand this open so I can
02:56see a little bit more.
02:56To add a field from a table, down into our query design environment, all we need
03:00to do is double-click on it.
03:01So for instance, if I want to include a ProductName into our query, I would just
03:04double-click on it and then drop it down.
03:06There are couples of other ways we can do it. We can click and drag.
03:08So, for OilCode, I could click and drag that down and drop it.
03:12I can also select a range of fields by using some of the standard Windows
03:15shortcuts. So I can click on the first one. I can Shift+Click and click on the
03:19third one to select a range.
03:21I could also use the Ctrl key on my keyboard. If I press that down, I can select
03:24individual records to add to the group.
03:26At that point, I can go ahead and click and drag and select all of those and
03:29drop those down into our design grid.
03:31So, that added a lot of fields here to our query that I actually don't really
03:34need, and we can get rid of them in a couple of different ways.
03:36I don't want to actually include a ProductName into our query, so I can click
03:40on this gray bar at the very top of the field name, and the whole field will select.
03:43Once it's selected I can right-click on it and say Cut or I can say Delete
03:47Columns from the Query Setup menu.
03:48So, let me go ahead and delete this column.
03:50I can continue deleting columns and that will delete out of my query.
03:53Another way to delete some of your fields is just click on the gray bar above
03:56the field name, and again, you can Shift+Click to select a range.
04:00Once it's done you can right-click and say Cut and we'll get rid of all those as well.
04:04Finally, to remove a table from our query designer, we can right-click on the
04:07table header and say Remove Table, and this goes back to where we started.
04:11So, let's go ahead and take a moment and we are going to recreate this query
04:14from scratch using some of the methods we just took a look at.
04:16So, I am going to go ahead and close this and Access is going to ask if we
04:20want to save changes, and I don't necessarily need to, so I am going to go ahead and say No.
04:24This time we are going to go to the Create tab and we are going to choose Query Design.
04:29Access opens up a new Query Design environment and it automatically opens up the
04:32Show Table window where we can select the tables that we want to use.
04:34So, we are going to recreate the customer email query from scratch.
04:38So I am going to pull in the DirectCustomers table just by double-clicking on it.
04:41I am going to go ahead and say Close, because I am done with this window and I
04:45am going to expand this out a little bit.
04:46Now, if you remember, the customers query includes the FirstName, the LastName,
04:50and the Email address.
04:52I find it easiest just to go through and double-click on everything, so I
04:54am just going to double click on FirstName, double click on LastName, and
04:57double click on Email, and you'll see that they all get dropped down into
05:00our query environment.
05:01If I go ahead and say Run, I'll switch to Datasheet View, and we'll see that we get the
05:05results from the query that we built using the wizard in the last movie.
05:08We've got the FirstName, the LastName and the Email address pulled from the
05:12tbl_DirectCustomers table.
05:13So, we just created our query from scratch using the Design environment and this
05:16query is identical to the one that we created using the Wizard.
05:19In the next chapter we are going to take a look at how we can use the Design
05:22environment to move beyond the options that are available within the Wizard.
Collapse this transcript
2. Creating Simple Select Queries
Defining a single criterion
00:00One of the most common uses of queries is to filter data within your database
00:04down to a specific subset of information that we are looking for.
00:07We saw a little bit of this earlier in the movie where we talked about how we
00:09can filter out our data tables.
00:11Let's take a look at how we can save some of our queries to do the same thing.
00:14Right now, I am going to open up our Employees query and I am going to open that
00:17straight into Design View by right- clicking on it and then saying Design View.
00:20This will open up that query in our design grid.
00:23I am going to maximize the window to give us a little more breathing room and
00:26we'll move the grid down.
00:27We can see that this query is pulling information from our Employees table and
00:32we are gathering the EmpID, the FirstName, the Middle initial, the LastName and
00:37the State from our Employees table.
00:39If I run it, we will notice that the query is pulling all of the employees that
00:42we have in our entire table.
00:43So, we've got a total of 200 employees and they are all represented in this query.
00:47Let's go ahead and switch back to our Design View and start working with some
00:50criteria to filter things down.
00:51You'll notice in the query grid we have a line called Criteria and anything
00:55that we type in here is going to match criteria based off of the record that we're in.
00:59So, for instance under FirstName I am going to write "Kevin" and I am going to
01:04press Enter and now I am going to go ahead and run the query.
01:08When I run the query you'll notice that Access is returning only the records
01:11where the first name matches Kevin and I've got a total of two employees
01:14with the name of Kevin.
01:15Let's go ahead and switch back to our Design View.
01:18Let me go ahead and delete this.
01:20We can do the same thing with States, so instead of a name of Kevin I am going
01:24to write NY for New York.
01:25This time, I didn't write any quotation marks, so you'll notice that when I
01:29press Enter, Access automatically enters those in for me.
01:31We'll go ahead and run this and we'll notice we've got three employees that live
01:36in the state of New York.
01:37Let's go ahead and go back to Design View.
01:39So, let's see what happens when I do a sort based off of the FirstName and the State.
01:43If I write the word Kevin in here again and tab away from it so that Access puts
01:47in the quotation marks, now we've got as a query that's asking for all the
01:51employees whose first name is Kevin and that live in the state of New York.
01:54I go ahead and run this, you'll notice that I actually don't get any
01:57employees here because we don't any employees named Kevin that live in the state of New York.
02:00Let's go back into Design View.
02:03I am going to go ahead and remove the criteria for New York and we are just
02:06going to focus on the FirstName for a moment.
02:09Instead of writing out the FirstName and making an exact match we can make use
02:12of what Access calls wild cards.
02:14If we use the asterisk key, which is Shift+8 on your keyboard we can create a
02:18query that's asking for any employee whose first name starts with a K. If I go
02:26ahead and click run, we'll notice that we have a total of 16 employees whose
02:30name starts with K. Back to Design View.
02:31Now the asterisk character matches any number of characters and any characters,
02:34which is why we can use the asterisk key to define any number of any character,
02:35which is why we can say K* and that will match any employee whose name starts
02:36with a K and has any number of characters afterwards.
02:37We can also put an asterisk at the beginning.
02:38Now, we are asking for any employee that has a K anywhere in their first name.
02:40So, if I go ahead run this we'll find Jackson, who has a K in the middle of his
02:43name, Derek who has a K at the end of his name, and Kevin that has a K at the
02:46beginning of his name.
02:48Let's go back to our Design View and we'll clean this out, get back to where we started.
02:52There are a couple of other things that we can do making use of our Criteria line.
02:57Now, we've already seen that if we include a criterion on two different records,
03:00for instance, we had Kevin as a first name and New York as a state, Access
03:04treats that as an And statement.
03:06So, basically it's looking through a database and finding all of the employees
03:08who both have a first name of Kevin and live in the state of New York.
03:12We can also use this Or line down here and this is a different way of writing a statement.
03:18So, we can write a query that looks at a couple of different states.
03:21For example, I can say ME for Maine or NH for New Hampshire.
03:26Now, what Access is going to do is return all the employees that either live in
03:29Maine or New Hampshire or if I run that we'll see that we have a total of six.
03:34Let's go back to our Design View.
03:36There is one other way we can write this.
03:39Instead of writing it on multiple lines, we can include everything on one line.
03:42So, I am going to get rid of New Hampshire from the second line and I am going
03:46to expand our window out so we have a little bit of more room.
03:48Now, let's say that we are interested in finding all of the employees that live
03:51in our New England states.
03:53There are six New England states and that includes Maine.
03:55Now I am going to write the word Or.
03:58the next New England state is New Hampshire Or Vermont Or Connecticut Or Rhode
04:04Island Or Massachusetts, MA.
04:08Now, when I press Return Access fills in all of our quotation marks, so it has
04:11the order of operations correct.
04:13So, now we can see that we are selecting Maine or New Hampshire Or Vermont Or
04:17Connecticut Or Rhode Island Or Massachusetts.
04:20Now, interestingly we have a state called Oregon and the abbreviation for
04:24Oregon is actually OR.
04:25So what happens when we write Or OR?
04:27If I press Enter because of the ordering Access knows that we are looking for
04:32Massachusetts Or Oregon here.
04:35So, because of the order it actually knows this is the value and not another Or statement.
04:39I am actually not interested in Oregon.
04:41I just wanted to point that out really quick.
04:42So, I am going to delete that out and now when I run our query, we'll find all
04:47of the employees that live in New England states.
04:48Okay, so let's go back to our Design View.
04:53So, Or is actually just one of three logical operators that we can use in our
04:56databases, the other two being And and Not.
04:59So I can write a statement that says for instance Not NY, and if I run that
05:05we'll get all the employees who do not live in New York.
05:07Let's go ahead and change this back to our Datasheet View.
05:11The third logical operator is And and it's a little bit tricky to use the word
05:14And when we are dealing with text based data.
05:16For instance, if I wrote NY and NH for New York and New Hampshire and I run
05:22this, you'll notice that we don't get any records back.
05:25Let's go back into Design View and figure out what happened here.
05:28So, right now we are looking for all of the employees who live in New York and New Hampshire.
05:32Now, Access is pretty literal in its understanding of the word And.
05:36It makes sense that new employee actually lives in both New York and New
05:39Hampshire simultaneously, so that's why it will not returning any records.
05:43Typically, you are not going to be using the word And on single Criteria line.
05:47We saw earlier that when you put criteria for two different records, for
05:51instance we have that first name of Kevin and the state of New York, Access
05:54treats that as an And statement.
05:55But typically when you are dealing with text you are not going to have the word
05:58And on a single line.
05:59
06:01in the next movie.
06:11 There are some exceptions to that and we'll take a look at that
Collapse this transcript
Comparison operators
00:01When the fields in your data table are of a numerical data type, Access provides
00:04some additional operators that we can use when we're constructing our queries.
00:08One type is called a comparison operator and they sort and qualify data based
00:12upon the numerical value of each record.
00:14Comparison operators don't simply match an exact value or pattern like we saw
00:18with a like AND, OR, and NOT operators in the previous movie.
00:23Comparison operators return records that match the relationship to a value, for
00:26example whether it's below or above that value that we specify.
00:30So take a look at some of the comparison operators that we can use in our queries.
00:34The first one is less than and we can find that by pressing Shift+Comma on our keyboard.
00:38And the type of questions that the < character will help us answer is
00:42something like all orders below $100.
00:45We also have a greater than character which is Shift+Period.
00:48And this will help us find all orders above the $100 for example.
00:51Then equal to and that will help us find records where the employee number is
00:55equal to 53, or the department is equal to marketing.
00:59We also have less than or equal to.
01:01So for instance if we're looking for a part-time employee situation, we have
01:05worked 32 hours or less. Or if we are looking for an overtime situation, we can
01:10find all employees that work 40 hours or more using greater than or equal to.
01:13We also have a not equal to option which is basically the less than and greater
01:18than characters put together.
01:19And this will help us to find all records where employee is not ID 53 or all
01:24departments except Marketing.
01:25So let's switch into Access and take a look at these comparison operators in action.
01:30So I've got a query called Products and I'm going to right-click on that and
01:35say Design View to open it directly into Design View.
01:37We go ahead and maximize this, so it gives me some more room here.
01:40And we this query is pulling information from our Products table and from it
01:45we're getting our ProductName, the size in Ounces, the wholesale Cost, the Price
01:50to our customers, and then the MarkupDollars, and the MarkupPercent.
01:54Now, we can use our comparison operator in the Criteria section, just like
01:57we were doing before.
01:58For instance, I can say <16 for Ounces to find all the products that are
02:04less than 16 ounces.
02:05And if I run it, we'll see all the products that are less than 16 ounces.
02:09Let's go ahead and switch back to Design View.
02:11Now, we can use the comparison operators in the same way that we're using
02:15criteria in the last movie, where we can select couple of different things at once.
02:19For instance, we can find all the products whose size is <16 ounces and the
02:24Markup is >5 dollars.
02:26I go ahead and run that we'll see that there's only 8 products that we carry who
02:31is less than 16 ounces in size, but their markup is greater than $5.
02:34Let's go ahead and witch back to Design View again.
02:38By modifying our criteria slightly, I am going to change this to <=16 and this will
02:44find all the products who's ounces are 16 or less and the markup is >5 dollar.
02:48Also we have a few more products. Now we have some of the 16 ounce sized
02:51products that are mixed in.
02:52Let's go ahead and switch this back and I'm going to get rid of those Markup
02:57criteria, and I'm going to get rid of the Ounces criteria.
03:01Now, we also have two other comparison operators. We have equals, which is
03:05basically the same thing as a Like operator that we saw in the previous movie.
03:08So I can say =16 and then it'll show us all of our products where the size is 16.
03:15I can also say <>16 and that'll show us all the products that are not 60.
03:20So everything else.
03:22The comparison operators are another tool that we can use when filtering down
03:25our data to specific records that we're interested in working with.
03:29They're especially useful when you want to find records that meet or exceed a
03:32certain criteria within your database.
03:34By using comparison operators in your queries, you'll be able to quickly
03:37and efficiently locate the records with the most relevancy to your specific questions.
Collapse this transcript
Defining the column headers with aliases
00:00When we're creating our queries using calculated or filtered records it's always
00:04a great idea to properly document your workflow, so that your results will be
00:08more easily understood by other users.
00:10One way to do this is to rename the field headers in your queries to give them
00:13more meaningful and accurate names.
00:15We can take a look at our Products query that we've already built.
00:18And we can see that the field names that we have here in our query come directly
00:21from the Products table.
00:22So we've here ProductName, Ounces, Cost, Price,
00:25MarkupDollars, and MarkupPrercent.
00:27Now, you can rename these headers so that they'll be more meaningful and provide
00:31a little more information about the query that we were returning.
00:33So let's go ahead and switch into our Design View and right now the field header
00:38for product name is ProductName, all pushed it together.
00:40I can go ahead and give this a new title by clicking at the very beginning of
00:43the field and typing in where we want the new title of the field to be.
00:47So for this I'm just going to simply name it Product.
00:49After the Product, I'm going to put a colon so that Access knows that everything
00:53before the colon is the new header and everything after the colon is where the
00:56data is actually coming from.
00:57Now, if I re-run the query, we'll see that the field header has now changed
01:00to the word Product.
01:01I can go back into Design View and do the same for the rest of these.
01:05So instead of Ounces, I'm going to put the word Size and then in parenthesis
01:09I'm going to put the units. So this is fluid ounces, Fl Oz.
01:14Now, you can't use periods in here, as you normally would with an abbreviation,
01:18because Access won't allow you to use the period in a header name.
01:21So I'm just going to go ahead and abbreviate fluid ounces and then add a colon
01:24to denote that that's the new field title.
01:26For Cost, this is actually the wholesale cost or the cost of the company.
01:30So I'm going to designate that Wholesale Cost.
01:32That will be a little more specific. Again finishing with the colon.
01:36The Price is our customer's price, so we're going to name that Retail Price with a colon.
01:41MarkupDollars and MarkupPercent, we're going to change that a little bit.
01:45We're going to write it Markup, and then inparenthesis we're going to denote the unit,
01:48so this one is dollars, again, finishing with a colon.
01:53And for MarkupPercent, we're going to do the same thing, Markup and then in
01:56parenthesis is the unit, this in percentage.
02:00Finally, we'll put a colon there to denote that that's the new alias for that field.
02:04And now when we run our query again, we'll notice that we have all new
02:07headers up here at the top.
02:08They're a little bit more specific and accurate based off of the results
02:11that we're returning.
02:12And we can see that it's a lot more clean and easier to understand.
02:15So by clearly identifying the fields returned in your queries, you'll aid other
02:19users that will be working with your database.
02:21This also helps communicate the meaning of a field as you build reports and
02:24other forms off of your query results.
02:26Adding a custom field header in the form of an alias is a simple and effective
02:29way to document the meaning of a particular column in your queries.
02:32And we'll see this will become more important as we move into some more complex
02:35calculations later on in the course.
Collapse this transcript
Exploring the field properties
00:01In the previous movie we took a look at how we can apply aliases to our column
00:04headers to be a little bit more specific about the types of data that we're
00:07returning in our queries.
00:08Now, we can go into our qry_Products one more time and we can see the saved
00:11aliases that we created in the last movie.
00:14Now, I'm taking a look at our table here that's being returned by this query and
00:17I'm noticing a few other areas that we can clean this up.
00:19Let me go ahead and maximize this.
00:21And let's take a look at retail price.
00:23Now, I know this is supposed to represent a dollar value, but our decimals
00:26are all over the place.
00:27We've got 2 decimals here, we've got no decimals here, and we've even got values
00:31that have only a single decimal.
00:33So I want to clean that up a little bit.
00:34Same thing with Markup $, and Markup % actually has a different problem here.
00:38Right now we're saying this as a percent, but it's written as a decimal fraction.
00:42So for instance, we've got our Wholesale Cost of $10, our Retail Price is $15,
00:47which represents 150% markup, but we're multiplying that by 1.5, so this the
00:51decimal representation of that percent.
00:53So I want to actually change that so that it matches what we're saying it is up
00:56here at the column header.
00:57And we can do all of this using the Property Sheet of our query.
01:00So let's go ahead and take a look at that.
01:02I'm going to go ahead and switch into Design View, and we can get to our
01:05Property Sheet in a couple of different ways.
01:07I've got a button up here on our ribbon and this is actually a toggle, so I can
01:10click it on to turn our Property Sheet and turn it off.
01:13I also have a couple of shortcut keys on our keyboard and we can press Alt+Enter
01:17and that will toggle it on and off.
01:18I can also press the F4 key and that will also turn on our Property Sheet off and on.
01:25Finally, we can go ahead and right- click on the field and from the drop-down
01:28menu select Properties from the list and that will open up the Property Sheet.
01:32Now, we have to pay attention to where we've clicked in here, because the
01:34Property Sheet simply tells us that we're looking at field properties.
01:37And right now I know that I'm adjusting the field properties for this field
01:40because it's the last one that I clicked in.
01:41If I click in a different field, the Property Sheet still says Field Properties,
01:45so it doesn't indicate which field that we're looking at.
01:47So you have to actually pay attention to where you're clicking in your design
01:49grid to know which properties you're adjusting.
01:52So let's go ahead and do our Wholesale Cost and I'm going to actually change the
01:54Format of this number to Currency.
01:57And what this is going to do is apply a formatting rule where we have a dollar
02:01sign before the number and then everything is rounded to two decimal places.
02:04We're going to do the same thing for Retail Price. Change to that using the
02:08drop-down menu to Currency.
02:11The Markup $ is also a Currency value.
02:15And Markup % we're going change the Format to a Percentage.
02:20We can also adjust the number of decimal places that we showed to increase our
02:23level of precision that we want to indicate.
02:24Right now I'm just going to leave it to 2 decimal places.
02:26Let's go ahead and re-run our queries.
02:30And we can see that the numbers that are returned are a lot more clean and
02:32easy to read, so we've got a dollar sign, we've got two decimal places for all of
02:35our currency values,
02:36and we also have our percentage data that's represented as a natural percentage.
02:40So I want to point out a little caveat about doing this. While we're adjusting
02:43the field properties, all that it's actually doing is adjusting how the numbers
02:46are formatted in our query results.
02:48It's not actually changing any of the data from our source table.
02:51So for instance, right now we've got percentage values, but in our data table
02:54these are still represented as a decimal.
02:56If I go into our Design View and I use the comparison operators that we saw
02:59before, for instance I want to find all of our percentages that are >150%,
03:03we might think that we're going to enter it this way, >150, but I were to
03:08run that query, we'll notice that we don't get any results.
03:10And that's because were still using the comparison against the decimal value
03:14that's in the original table.
03:15So in order to do this I would actually need to use the decimal version,
03:18>1.5, even though we are formatting it as a percent.
03:22So if I say >1.5 and then run it, we're filtering up all that Markup percentages
03:27that are >1.5, but then we're formatting it as a percentage, so we'll see that
03:31these are all greater than 150%.
03:32Let's go back into our Design View, and so far we talked about the properties
03:37for our fields down here. We also have the ability to adjust the properties for
03:41entire query by clicking in the upper pane, and if I click there you'll notice
03:45that it changes to Query Properties.
03:46We're going to go ahead and take a look at Query Properties in the next movie.
Collapse this transcript
Understanding query level properties
00:01In the last movie, we took a look at how we can use the field properties in
00:03our query to organize and clean up some of the results that are returned with our query.
00:08We also have some additional properties that we can adjust for the query itself.
00:11So let's go ahead and take a look at what we have available there.
00:12So, let me go ahead and open up our Products query and we can see that our
00:16numbers are cleaned up by using the field properties from the previous movie.
00:19We'll switch in the Design View and this time we're going to open up the
00:22properties for our entire query by clicking in the upper pane of our window.
00:27If it's not already open, we can open up our Property Sheet in a
00:29couple different ways.
00:30We've got the toggle button up on the ribbon.
00:32We can press the shortcut keys Alt+Enter,
00:34we can press F4, or we can right-click in the open area and select properties
00:39from the list there.
00:40So, let's go ahead and ensure that we're taking a look at the query properties
00:44by looking at the selection type up here in the Property Sheet.
00:47The first property for our query is a Description.
00:49This particular cell doesn't have a whole lot of room for typing.
00:52So, what I'm going to do is right-click in the cell and choose Zoom to bring up the Zoom box.
00:56Now this allows me to type in a useful description for our query, so we can make
00:59sure that we understand how it relates to some of the other objects, tables, and
01:02forms within our database.
01:04So, I can type in a description, such as This query pulls data from the form
01:09"frm_ProductsSearch" and feeds it into the report "rpt_ProductSearch".
01:13So this is an example of how we can use the description for our query to make
01:17sure that we understand how the query results relate to some of the other
01:20objects within our database.
01:21So, if we're pulling information from a form and feeding into our report,
01:24this is a good way that you can make sure that we remember how those
01:26relationships have been set up.
01:28Now, let me point out one thing about the Zoom box.
01:30We have a scrolling slider here and that seems to indicate to me that we can
01:33keep typing a bunch of text in here.
01:35Now I've got some dummy text saved to my clipboard that I'm going to paste in.
01:40We can go ahead and type in a description that seems to be as long as we want.
01:43Once we press OK though, we're going to get an error message here and Access
01:47is telling us that the string returned is too long and the results will be truncated.
01:50At this point we don't have any other option, but to say OK and let Access chop it off.
01:55So, we're going to say OK and if I go back to view the description after it's
01:58been edited, we'll go ahead and zoom again, we'll see that Access chops it off
02:02at about 255 characters.
02:04So, don't go crazy in here. Just get to the point to make sure that we
02:07understand how this query can relate to some of the other objects in our database.
02:10So, we could go ahead and delete all the dummy text, and just leave it with
02:14that simple description.
02:15I'm going to go ahead and say OK.
02:16So, I've got a couple of other properties that I want to take a look at.
02:20We've got our Default View and currently that's set to Datasheet.
02:23If we're working with PivotTables or PivotCharts, we can change to that setting here.
02:27That way when we double-click on our query in the navigation pane on the left,
02:30it'll open up directly into those views instead.
02:32We're just working with Data Tables, so I'm going to leave it on Datasheet.
02:36We've got another set of properties here called Output All Fields and the
02:39default value is No.
02:41Using the drop-down menu, we see that our options are both No and Yes.
02:44If we change it to Yes, then Access is going to ignore the status of our Show
02:47boxes down here in our Query Designer.
02:49Basically, Access allows us to include a field and sort or filter based off of
02:53that field, but we don't necessarily need to show it in our query results.
02:56So basically, we can use a field and a criteria to filter out our results, but
03:00we don't need to show it in the return.
03:01If we have these checkboxes turned off, and we set our property to Output All
03:05Fields to Yes, then Access is going to ignore that these are turned off and
03:07just return them anyway.
03:08Typically, you're going to want to use the Show boxes to control which fields
03:12are displayed in your query results.
03:13So, we're going to leave our default output fields to No, in which case Access
03:17is going to respect the settings that we have down here with our Show boxes.
03:20The next property we have to take a look at is called Top Values and
03:23currently that's set to All.
03:25Using the drop-down menu, we have some options here.
03:27We have 5, 25, 100, and then we have 5% and 25%.
03:31If we select 5, then Access is going to return the top five results.
03:35It's not going to return everything. Only the top five.
03:38We can also type in a number that we want.
03:39So, for instance, if I want it to return 10% of my records, I can change that
03:44here and I don't think it's necessary to select one from the list. I can
03:46type in whatever I like.
03:47You might have noticed that when I change this to 10% and press Enter, it's also
03:52changing the Return up here on our ribbon.
03:54So, these two values are actually linked.
03:55Return is the same as Top Values here.
03:57So, I can adjust those to return 100 records and you'll notice that the Top
04:01Values down here change to 100.
04:03Let me go ahead and reset this to All, so we're getting everything in our query results.
04:08The other property that I want to point out is called Recordset Type and the
04:11default value is called Dynaset.
04:13Basically, when I run a query, Access is returning what's called the Dynaset.
04:18This is basically a live link between our data in the query return and the
04:22data in our data table.
04:23So, if I were to make an edit here in the query, let's write the word EDIT after
04:26the name of my product and go down the cell to save those changes, if I then
04:30move into my Products table, we take a look and we'll notice that that edit has
04:33actually been saved to my data table.
04:35Now, depending on the task that you're trying to accomplish, this could be
04:38either really useful or it could be really dangerous.
04:40So, what we can do here is let's close our Products table and I'm going to get
04:45rid of this edit that I made so it's not saving it.
04:46Let's switch back into Design View.
04:50Back in the Query Properties, by clicking up here in the open area, I'm going to
04:54change my Recordset Type to Snapshot.
04:57So, Snapshot allows us to view an up-to- date and current look at our data table,
05:01but we won't be able to edit any of the data within it.
05:03Let's go back into Design View and switch to the Query Properties again.
05:07Now, there's a bunch of other options in here, and these are typically not
05:11things that you're going to ever need to deal with.
05:12If you've noticed as I've been clicking through here that you can click on an
05:15option and Access gives us a little bit of text down here to describe what
05:18that property does.
05:19But typically these are things that you're not going to want to adjust in your query.
05:22So, the Query Properties gives us a lot of options that we can do to adjust how
05:25our queries operate, how they're documented using the Description field, or how
05:29the fields themselves are formatted in the query results.
Collapse this transcript
Working with joins and primary keys
00:00When creating queries that include multiple tables, it's important to understand
00:03how those tables are related to each other in order to get meaningful results.
00:07Now typically, Access is going to pull information from the Relationships window
00:10that we've set up between our tables, so that it'll understand how the tables
00:12relate when we create our queries.
00:13But sometimes we're going to need to go into our query and modify those
00:16relationships manually.
00:17Let's take a look at how we can do that.
00:19So, I'm going to go ahead and open up our JuneSales query and I'm going to
00:22right-click on it and say open that directly into Design View.
00:25So, our JuneSales query has three tables.
00:27We've got our Employees table, our Orders table, and our Products table.
00:31Each of our products is represented in the Products table one time and we can
00:35have multiple orders for the same product.
00:37So, we can say that this is a one-to-many relationship.
00:39Same way, we've got our Employees table and each of our employees are only
00:42represented here one time, but they can be represented on multiple orders.
00:46So again, this is a one-to-many relationship.
00:49The query that we have set up right now has employee IDs from the Employee table.
00:53We've got our FirstName from the Employees table.
00:55We also have a calculation that takes Price and we're finding the sum total of
00:59all the prices and we're going to call that June Sales with an alias.
01:02Then we have our OrderDate, and we see that we have a criteria set on our date.
01:06Now, we'll go ahead and talk about dates in a future movie, but for now just
01:09know that we're using this criteria to filter out only the records that occur
01:11between June 1st 2010 and June 30th 2010.
01:15So the question that this query has set up to answer is how did our Sales team
01:18do during the month of June?
01:19Who sold the most products and who sold the least?
01:22So, let's go ahead and run this query and see what the results look like.
01:25We can see that we have Rose in the lead.
01:26She sold a total of $110 worth of product in June.
01:29If we scroll to the bottom, we can see that Patrick sold the least at $10.57.
01:33I'm going to go ahead and maximize this window so we can see the very bottom,
01:37and taking a look at the number of records that were returned, we have 36.
01:40Now I know a little bit about the data tables that we have in this database,
01:43and I recognize that we have a problem here, because we should have 84
01:47salespeople on our team.
01:48So, where did everybody else go?
01:50Let's go back into the Design View.
01:52The answer to our problem can be found in the way these two tables are
01:54related to each other.
01:55So, I'm going to go ahead and double- click on the joining line between the two
01:58tables and we can bring up the join properties for that.
02:01The default option is called an inner join and we can see that it's only going
02:05to include rows where the joined fields from both tables are equal.
02:07Let's go ahead and get this out of the way so I can see this a little bit.
02:10Basically, what we're saying is that we're only going to return records where
02:12the employees are represented in the Orders table and the orders are
02:16represented in the Employees table.
02:18Basically, if an employee didn't have any sales, then it's not going to be returned.
02:21What we want is this option number two.
02:24We want to include all the records from the table Employees, and only those
02:27records from table Orders where the joined fields are equal.
02:29Basically, we want every employee, whether they had sales or not, and then for
02:33the people that did have sales, we're going to return that information for them.
02:35So, let's go ahead and say OK to this.
02:37Now we're going to try and run our query.
02:40Access throws up a little warning message here.
02:42In dealing with Access, I've learned that the warning messages that we get are
02:45actually really useful if we take a moment to read what they say.
02:48So, this one says the SQL statement cannot be executed because it contains
02:51ambiguous outer joins.
02:53Basically, since we've adjusted the join properties between the Employees table
02:56and the Orders table, Access is getting a little bit confused about how all
02:59three of these tables are supposed to relate to each other.
03:01The warning message goes on to give us a hint as to how we can fix this.
03:04To force one of the outer joins to be performed first, create a separate query
03:08that performs the first join, and include that query in your SQL statement.
03:11So, the answer is that in order to perform this task, we actually need to
03:14create two queries.
03:15So, let's go ahead and say OK to this message and I'm going to reset our join
03:18back to the way it was.
03:19So again, double-click on that, select option 1, and say OK.
03:22Let me go ahead and close this query and Access asks if we want to save it.
03:26Let's go ahead and save our query.
03:28The next step is to create a new query.
03:30We're going to go to Create and then Query Design.
03:32We're going to choose our Employee table.
03:34We're also going to switch the tab to Queries, and we're going to include the
03:37query that we just created.
03:39Go ahead and close this window.
03:41We see that Access is already recognizing that the employee ID in the Employees
03:45table should match the employee ID from the JuneSales table.
03:47Let's go ahead and populate some data into our query.
03:50We want employee ID, FirstName, and then we want the calculated June Sales value
03:54from the JuneSales query.
03:56So let's double-click to move that down.
03:58Next, we want to adjust the join properties between these two tables.
04:00Let's double-click on that line and now we can go ahead and select option 2 to
04:04include all the records from the employees table and only records from JuneSales
04:07where the join fields are equal.
04:08We'll go ahead and say OK, and we'll run our query.
04:11So, after a bit of processing, Access returns 200 records.
04:15Now, that's not the 84 that we're expecting in the number of salespeople that we have.
04:19So, we've got one more step that we need to do.
04:20One more time, let's go back in the Design View.
04:24Now, since we're returning all of our employees, we're actually getting
04:27everybody in the whole company, regardless of whether they work in the Sales
04:29department or HR or Marketing or Advertising.
04:32So, what we want to do is filter down to just the people in the Sales department.
04:35So, I'm going to expand this window a little bit and we're going to add a
04:38department filter in here.
04:40Under criteria for departments, I'm going to write sales.
04:43Press Enter to accept it, and now when we run it, we'll see that Access returns
04:48the 84 records that we were expecting.
04:49Let's go ahead and sort this from largest to smallest on the sales.
04:52We can see that once again we've got Rose at the top of the $110.
04:55In the middle, we've got Patrick with $10.57 for sales, and then we've got
04:59everybody else in the Sales department that didn't have any sales for the month.
05:02So, understanding how your data tables relate to one another is a critical
05:05component to creating well- structured queries that give the meaningful
05:08results that you expect.
Collapse this transcript
Printing query results
00:01Often the results of your queries are going to be incorporated into other Access
00:04objects such as Forms or Reports.
00:07But occasionally you want to get a printout of the Datasheet View of your query
00:09just to pass on to a colleague or supervisor.
00:11If you've ever printed a Data Table within Excel, you'll be happy to know that
00:15many of the same formatting options are available to you within Access.
00:18So let's go ahead and take a look at how we can print our query results.
00:21So let me go ahead and open up our qry_ Products, and we can see that this is the
00:24one we've spent some time on earlier, formatting it through properties.
00:27So we've got our headers all named appropriately and we've got our
00:30formatting all set up.
00:31Let's go ahead and go up to Office button, go down to Print, and we'll
00:35choose Print Preview.
00:36Now this is the same Print Preview interface that's common throughout the
00:40entire Office suite.
00:41We've got our option to send it to the printer.
00:43We've got some options for page layout, so we can change it in size, whether
00:47it's portrait or landscape in some of the margins.
00:49We've got some options for how we're displaying the print preview of screens, so
00:53whether we want to zoom in, view it as a one-page or two-page spread.
00:56The data section gives us some export control, so we can export to Excel, we
01:00can export to a PDF file, a Word file, or we've got some additional file types
01:04we can export to in the More menu.
01:06Finally, we've got the Close Preview button.
01:08Let's go ahead and expand this out a little bit and I am going to zoom in here
01:11to the top and we can see that we've got currently a name at the top of our page
01:14and we've also got the date that we were printing this.
01:17In the Page Setup here, in the Page Layout section, I can click on that.
01:20And, we can see that we've got the same controls for margins and another Page
01:24tab for layout landscape or portrait.
01:27But under Print options I also have an option to Print Headings.
01:29And, if I turn that off and say OK, we'll notice that we lose the title and the dates.
01:33I am going to go ahead and go back to the Page Setup and turn my Print Headings back on.
01:38Finally, we can just go to our Print menu and we can choose any available
01:41printer within our system. Let me go ahead and Cancel out of that.
01:45Now depending on what additional extensions or software you have on your system,
01:49we might have some additional options available to us.
01:51On this particular computer I've got Acrobat Professional installed and as
01:54part of that Installation Package, it installs say this additional toolbar into the Office suite.
02:00Otherwise we can go into the Microsoft Office button and if we go down to Save As,
02:03you might see an option to export to a PDF or XPS file.
02:07If you don't have either these options available to you, we can actually
02:09download a free extension from Microsoft that will add that ability into
02:12the full Office suite.
02:13So let's take a look at where we can get that from.
02:16You can visit the Microsoft download Center at www.microsoft.com/downloads.
02:22Once here, we can do a search and we are going to do a search for Office 2007, Save As PDF.
02:31In the search results we'll find an option to install an add-in for Microsoft
02:34Office 2007 Save As PDF or XPS.
02:38If you click on the Download link, it'll download an executable file to your
02:40computer, and after running that you'll have the ability to export PDF files
02:44from any of the Office programs.
02:45Now back in Access, we actually already have that extension installed on our
02:49system and if you didn't have it, you'll notice that you've got a couple of new buttons here.
02:52We've got a PDF or XPS button on our ribbon.
02:55We also could go up to Microsoft Office button > Save As, and if you didn't have
02:59it installed, you know this options is there as well.
03:01So let's go ahead and close the print preview.
03:05So that was a way that we can quickly generate a printed report that shows the
03:08results of our query.
03:09Let's go into Design View for moment.
03:11So let's say that we are putting together a scientific report or a grant
03:14proposal where not only do we need to document the results of our query, we
03:18also need to document the process that led to those results.
03:20So basically we need to a have report that shows all the fields that we have in our query.
03:24All the criteria that we were filtering by and any calculations we might have
03:27used to derive those results.
03:29From the Design View, you would think that you could probably go up to the
03:31Office button and select Print, but we'll notice that it's actually grayed out.
03:35And if you have the PDF extension installed, if you go to Save As, you'll also
03:38notice that PDF is grayed out as well.
03:40So how do we document our process?
03:42There are a couple of options that we have available to us.
03:45One option is to switch our view into SQL mode and basically this is the text
03:49that's created behind the scenes as we edit our query in the QBE environment.
03:53So we can simply select this, copy it and paste it into an email or a text file.
03:57So that's one way that we can document our process.
04:00Another way is to go up to our Database Tools menu and we can use a tool called
04:04Database Documenter.
04:05Let's go ahead and click on that.
04:07And we'll see that the Database Documenter brings up a list of all the objects
04:10within our database.
04:11I can switch to the Queries tab, if it's not selected, and choose to document our qry_Products.
04:17I've got some options available to us, so let's take a look at those.
04:20We get a lot of information that we can include in our report.
04:24We can include information about the properties for our query, the SQL
04:28statement, any parameters that we might have saved, the relationships between
04:32the data tables that we were using and any user permissions that might be
04:35associated with our query.
04:37For the fields we can choose to document Nothing.
04:39Or we can show Names, Data Types and Sizes, or we can include Names, Data Types,
04:44Sizes and Properties for our fields.
04:46We can also include information about indexes, but we haven't been using
04:49indexes and our queries.
04:50So let's go ahead and just accept these default settings and say OK.
04:54And if I press OK one more time, it's going to ask us to close our query first
04:58and we can go ahead say OK.
05:00And Access will jump us back into our Print Preview mode and we can take a look
05:04at the report that was generated by zooming in.
05:06And we can see that we have a detailed report about how this query was created.
05:10We've got information about our properties, including the date and time that it was created.
05:12We've got our SQL statement and we can see that we have some information about
05:17the fields and all of the properties we have associated with those fields within our query.
05:21We can also page through our reports, and we've got several pages here, to see all
05:25the details that go in the creation of our query.
05:28So using the Print options within Access is an easy way that we can document,
05:31both the process and the results of our queries, in order to include a hardcopy
05:35printed report or to shares with our viewers.
Collapse this transcript
3. Creating Parameter Queries
Understanding parameter queries
00:01So far we've seen lots of ways that you as the query designer can control the
00:04records that are displayed in the query results.
00:07But what if we wanted to set up a framework and allow the end user of our query to
00:10supply some of the input?
00:11Lets go ahead and take look at this qry_EmployeesAZ, and I am going to
00:15right-click and go into Design View.
00:17And you can see that this query has a criteria of Arizona, or AZ.
00:20If I run this query, it's going to return all the employees that live in
00:24the state of Arizona.
00:25Now the results might change over time as we gain or lose employees within
00:29our organization, but the underlying structure of the query is always going to be the same.
00:32It's always going to return the employees that all live in Arizona.
00:35Let's take a look at the Design View again and we'll see how we can make this a
00:38little bit more flexible.
00:40So back in the Design View, I am going to get rid of the criteria AZ.
00:44We can use what's called a parameter query in order to prompt the end user for a
00:48little bit of extra information when the query is run.
00:51We define a parameter value by using the square brackets.
00:54So instead of hard coding this query to always return results for Arizona,
00:58I want to ask the end user which state they are interested.
01:00I am going to input a parameter query by opening it with a square bracket and
01:05then I am going to ask the end user, What state are you interested in?
01:07I am going to end that with a closing square bracket.
01:11Now when I run this query, I am going to get a dialog box that says What state
01:14are you interested in?
01:16Let's say I am interested in the state of Florida, so I'll type in FL and say Run.
01:20And now we're getting all the employees that live in state of Florida.
01:23Switch back into Design View and run the query again.
01:25Now I am interested in Alabama, so I'll type AL. Go ahead and say OK.
01:30Now the exactly same query is returning just the results from Alabama.
01:33So you can see how including a parameter request into your query can quickly
01:37make a query more flexible.
01:39Let's go ahead and go back into our Design View and take a look at some other
01:41options that we have.
01:42In a previous movie we looked at the comparison operators greater than or less
01:45than or greater than an equal to, etcetera.
01:47Let's take a look at how we can combine the comparison operator with a parameter request.
01:51I am going to go ahead and delete this request for State, I am just going to
01:54highlight the whole thing and press Delete.
01:56I am going to also add a couple of more tables to our query, so I am going to
01:59right-click and say Show Table.
02:00I am going to add our Orders and our Products table.
02:03Go ahead and close this.
02:08And I am going to add Cost from the Products table to our query.
02:11So, for our Cost criteria, let's say we want to set up a query that prompts
02:15for a maximum price.
02:16I am going to use the operator <= and then I am going to prompt for our parameter.
02:22So I am going to open up the square bracket, I am going to type in Enter Maximum Cost,
02:26and I am going to close it with a square bracket.
02:29Now Access is going to ask me for the maximum cost that I am interested in.
02:32This time I am going to type in $10 and I'll say OK.
02:35And we'll see that Access returns only the products that are $10 or less.
02:37Let's go back into our Design View and take a look at another example.
02:41Let's go ahead and get rid of this parameter.
02:44Okay, now I want to set up a query where we can look up employees based off to
02:50the first letter of their last name.
02:52In the criteria section for LastName, we are going to use a like operator
02:55that we've seen before.
02:56I am going to enter in the parameter, First letter of Last Name.
03:03I am going to finish that with a square bracket.
03:06And I am going to use the asterisk wildcard that we've seen before.
03:09In order to do that I need to use an ampersand to say that we're linking these together.
03:13And then we're going to add the asterisk.
03:13We'll take a look at the SYANTAX a little bit later in the course when we
03:16look at text expressions.
03:18Now we'll go ahead and run this query now.
03:21And we are going to get a parameter request that says What is the first
03:23letter of Last Name?
03:24Let's say I am interested in all of our employees that have a last name that
03:27starts with W. I am going to go ahead and say OK.
03:29You'll see that Access returns those results.
03:32If I go back and run it again, now I am interested in F and Access
03:36returns those results
03:38Letting your end users define their own questions can be a pretty powerful tool.
03:42It adds flexibility to your database and could be a way to streamline your work
03:45while providing some level of future-proofing.
03:48Parameter variables will allow your queries to serve answers to questions that
03:52you might not have even considered during the design process.
03:55The Enter Parameter Value dialog box isn't the only way that your end users can
03:58define variables in a query.
03:59In the next movie we will take a look at how we can use data entry forms and
04:03buttons to hook into the variables that we've just seen.
Collapse this transcript
Obtaining parameters from other Access objects
00:00I want to start this movie by passing on a fundamental concept that I think
00:04might change the way you think about Access.
00:07Every object and every form, every button, checkbox, label, text box has a name
00:12or a value associated with it within the database.
00:15In every name or value it can be used as an input somewhere else in your database.
00:19And really understanding this concept will set you on the path to becoming a
00:22true Access rock star and here is why.
00:24Access objects, that is the tables, the forms, the queries, the reports,
00:28etcetera, they are all presented in the interface as individual components of your database.
00:32But in reality, it's the interaction and interconnections between the objects
00:36that allow your database to do some pretty amazing things.
00:39In the previous movie, we looked at how we can provide our end users with a
00:42popup dialog box to request some additional bit of information when the query is run.
00:46Let's take that a step further and we'll see how we can define those same
00:49parameters using a button or a form control.
00:52Right now, I am going to open up our EmployeeLookup form here and it's
00:55called frm_EmployeeLookup.
00:58This is pretty well formatted.
00:59It's going to ask me to Show me all employees in and I've got dropdown menu.
01:03If I use the dropdown menu, you can select any of the states.
01:06I am going to go ahead and select Colorado and I have got a button that says Run Query.
01:10If I click on that, it runs a query called EmployeesLookup and filters down to all
01:14the employees that live in the state Colorado.
01:16Now, functionally this is working the same exact way as a parameter query that
01:19we were looking at in last movie.
01:21If we switch into Design View, we will see they we were simply asking for a
01:24parameter here in the Criteria section.
01:26So, we can see that we are obtaining the value of this parameter from the State
01:29object within the EmployeeLookup form.
01:32Let's go ahead and take a look at our EmployeeLookup form and see where that's coming from.
01:35Let's go back into our form here and I am going to switch in the Design View for our form.
01:40I am also going to open up our Properties box and I am going to use the F4 key to do that.
01:45If we take a look at the properties for this dropdown menu, we'll see that the
01:48name of this menu is called State.
01:50So if we go back to our query one more time.
01:52We can see that the State value is the same as that dropdown box.
01:55So, whatever is selected in the dropdown box when this query is run is what gets
01:58populated as the criteria.
02:01Let's take a look at another example.
02:02Let me go ahead and close the Property Sheet here and close the
02:05EmployeeLookup query.
02:06I am also going to go ahead and close the EmployeeLookup form.
02:08I've got another form here called SalesRange and when I run that, we are going
02:12to get asked to supply a couple of values.
02:14The Total Lifetime Sales to each Customer.
02:16We want to find the customer that has spent between $150 and $200.
02:21If I run the query, Access returns the result that we've expected.
02:26Take a look at the data sheet behind this to see how this is working.
02:29If I expand this to open a little more, we can see that we have got some
02:32comparison operators here.
02:33So we have got comparison operator that says greater than or equal to whatever
02:37the value of the low field in the SalesRange form is and less than or equal to
02:43whatever the high value is in the SalesRange form.
02:46So, if we take a look at the Design View of our SalesRange form, we should be
02:49able to find the low and high values in these two text boxes.
02:52Let me click on this text box and we'll open up our properties again.
02:56This time I'll use the button on the Ribbon.
02:59We can see that the name of this field is low and the name of the right field is high.
03:03So, whatever the end users typed into these two boxes when you run the query is
03:07what gets populated as the criteria and that's what gets run as the query.
03:10Now this wasn't meant to be an all- inclusive look at creating amazing forms, but
03:15I hope these examples have given you a little taste of the types of user
03:18interface tools that are at your disposal within Access.
03:21By combining what we already know about creating robust queries with some
03:25additional insight into the larger framework of Microsoft Access databases
03:28and specifically forms, my goal is to get you thinking about your own projects
03:32and hopefully get you a little bit excited about applying these tools to your needs.
Collapse this transcript
4. Creating Calculated Fields
Using Totals to calculate values
00:01Access databases are really good at collecting and storing a large amount of raw data.
00:05However, when it comes to understanding what all that data is telling us, it can
00:08quickly get overwhelming.
00:10Most of the time you want to summarize or aggregate data points together in
00:13order to get a better understanding of some of the trends or rankings that might
00:16otherwise be buried within your data.
00:18So, let's create a new query where we can apply this concept.
00:20I am going to go up to the Create menu, and this time I am going to create a new
00:24query in our Design View.
00:25I am going to include our DirectCustomers table.
00:27I also want to get some information about their products they have ordered.
00:30Now, if we think back to our relationships window, you might recognize that
00:34these two tables aren't directly linked together.
00:36We also need to include our Orders table that provides a relationship between the three.
00:40Go ahead and close your Show Table window.
00:42I am going to include our FirstName and LastName from the DirectCustomers table.
00:47I also want to get the Price from the Products table.
00:51I'm going to use a criterion here so we can filter out only a few records.
00:55So it's a little bit easier to see what's going on.
00:57I am going to use our friend Sebastian Rich.
00:59He is our top customer.
01:00So let's go ahead and run this query, and we are going to see how much Sebastian
01:03has spent for each of his orders.
01:05He has placed a total of six orders with the company.
01:07Let's go ahead and switch back into Design View.
01:09I am going to go ahead and toggle on our Totals row by using the button up
01:12here on our Ribbon. I'll turn that on.
01:15You will see that we've got a new row here called Total, and we've got some
01:18default values that apply to each of our fields.
01:20So we've got a Group By class for each of these fields.
01:22Under Price I am going to open up the menu and I see that we have a lot of
01:25options to choose from.
01:26I am going to go down the list here. I am going to Sum first.
01:29And if I run this query again, we'll see that we have a new total here and
01:33Access gives it a new title called SumOfPrice.
01:35This is going to be the sum total of all the orders that Sebastian has spent with us.
01:39So, we can easily see that he has spent $260.24 with the company.
01:42Let's go ahead and go back to the Design View and take a look at some of these
01:45other total options.
01:46Let's take a look at average.
01:48If I run it again, and now I am going to have to expand this out a little bit so
01:52I can get a better look at the number.
01:53Access again has changed the title.
01:55So now where it says AvgOfPrice, I can see that the average order has been
01:5843 dollars and about 37 cents.
01:59We go into our Design View again.
02:03Let's change this to minimum. Run it again.
02:07We now have the minimum order that he spent and the smallest order was $16.
02:11Back up in Design View, and I think you get the idea.
02:14We can also get the maximum order that he spent, we can get the number of orders
02:17that he has ordered with us, we can also get some statistical information such
02:20as the standard deviation or the variance in his orders.
02:23We can also return the first or the last order that he spent with us.
02:26But for now, let's go ahead and change this to... let's say Average.
02:30I am going to get rid of our criteria here.
02:32So I am going to delete Sebastian Rich from our criteria.
02:35Now, when I run this query again, we've got all of our customers sorted out and
02:38we can find the average total for every order that they have placed.
02:41So, aggregate operations will give you the answers to some of the most common
02:44questions that you might have about your data.
02:46They provide a mechanism for all kinds of summaries, trend analysis, or rankings.
02:50They're a great way to move beyond simply reviewing your data to understand
02:53what it actually means.
Collapse this transcript
Introducing mathematical operators
00:01One of the key factors in creating a clean and robust database is knowing
00:04what data you need to store and what data you can derive or create from other records.
00:09For instance, if we think about how a payroll database might work, the number
00:12we're after is how much the employee earned at the end of a two-week pay period.
00:16But will that dollar amount appear in any of your tables? And I would say probably not.
00:21More than likely, a payroll database will simply include a record for the
00:24employee's hourly wage
00:25and will have another table that describes the number of hours they worked during each shift.
00:30At the end of the pay period, when they are cutting the checks, the payroll
00:32department will simply add up the number of hours worked, they'll multiply that
00:36by the hourly wage and you will get the total amount for the check.
00:39By using some simple mathematical operations, in this case addition and
00:43multiplication, the Payroll department is able to derive the value of the check
00:47even though that value doesn't actually appear in the database.
00:49The Design View in Access allows us to easily add some of these types of
00:52mathematical operations to our queries.
00:55So let's see how we can apply this.
00:56We are going to go up to the Create tab and I am going to create a new query in Design View.
01:00I am going to go ahead and add the Products table for good measure, even though
01:04I am not going to actually use any of the data from the Products table.
01:07Now, in the fields, we can actually just type a mathematical expression right in here.
01:11We could type in things like addition so I could type in 5+3.
01:13we could do subtraction, 5-3.
01:17I could do multiplication, so 5*3.
01:20We can do division, 5/3.
01:23We can even do exponentiation, so we could do 5 raised to the third power.
01:28Here I am using the up caret, which is Shift+6 on your keyword.
01:30If I go ahead and run this query, you 'll see that Access does the math and
01:34returns the results.
01:35So we see Expression 5 was our exponent, so 5^3 is 125.
01:415/3 is 1 and two-thirds.
01:43Now, we are returning a total of 90 records here because if we go back into
01:46Design View, I have the Products table to sit up here.
01:49Access requires one table to be linked every query.
01:51So that's why this is up here, even though we are not using it.
01:54So let me go ahead and get rid of all these math expressions.
01:57I'll right-click and say Cut.
01:59Now, Access actually also provide another two options for division and I want
02:02to tell about the differences between the two.
02:04So let's say that we are packaging up bottle of olive oil and we are going to
02:07ship them out to our retail locations.
02:09We have got a thousand bottles and we are putting them in the cases of 12 bottles each.
02:12Normally, you would say 1000/12 to give you the number of cases that you have.
02:16If I go ahead run this expression, we'll see that Access returns 83 and
02:21one-third cases full.
02:22Now, this is a little bit hard to deal with.
02:24So let me take you through the other two options that we have for division to
02:27see how they'll help us out here.
02:28So, we'll go ahead and return to our Design view and the first option is
02:32called integer division and that's starting with a backslash instead of a forward slash.
02:36So let me give this field an alias here and we are going to call Full Cases.
02:39We are going to use integer division. So 1000/12.
02:47The second form of division is called modulo.
02:48I am going to write an alias that says Remaining Bottles.
02:53The mathematical operation for this is going to be 1000 Mod 12.
02:57That's how we would write a modulo expression.
02:59Now when I run this, let's take a look at what the results are.
03:01We'll see that the original division returned 83 and one-third.
03:05That means 83 and a third cases can be filled with our thousand bottles.
03:10The integer division only returns the full cases that I have returned.
03:13So we can get 83 cases.
03:15The modulo division only returns the number of bottles that are left over at
03:18the end of the day.
03:19So we have four bottles left over.
03:21So these two numbers,
03:22the integer division and modulo division, are much cleaner in this particular
03:26examples, depending on the particular scenario that you're working with.
03:29Now that we have seen that how these all mathematical expressions work within
03:32Access, we are going to take a look at how we can apply them using our data
03:35within the data tables in the next movie.
Collapse this transcript
Using mathematical operators
00:01In the last movie, we took a look at the seven mathematical operators that
00:03Access has at our disposal.
00:05Let's see that how we can use these actual data.
00:07We'll go up to our Create tab and we will create a new query in Design view.
00:11Then I am going to add our Products table to our query and we are done with the
00:13Show table window, so I can close that.
00:16Now, for this query, we are interested in finding out the price per ounce of all
00:20of the products that we carry.
00:21That may include the ProductName, the Price, and the size, which is in the Ounces field.
00:27Now we are going to create the mathematical expression that will give us the
00:30price per ounce of each product.
00:32Let's go ahead and right click and say Zoom to bring up the Zoom window.
00:35So, the expression is going to look something like this: price/size.
00:37Now, this isn't actually the correct syntax that we need.
00:42We need to be more specific so that Access knows exactly where these values are coming from.
00:46So I am going to go ahead and rename price, and I am going to tell it that
00:50It's coming from the table products, tbl_products, and it is coming from the
00:57field named price.
00:59And this is the syntax that Access requires.
01:01We need to wrap the table name in square brackets, and then we separate each by a period.
01:06I am going to do the same thing for our size field.
01:08So the size is coming from the table products and is coming from the ounces field.
01:14So once again, we are taking the ounces field from the table products.
01:18So our full expression reads "take the price in the products table divided by
01:22the size, which is in ounces, from the products table."
01:24We'll go ahead and say OK and Access gives it a Expression 1 alias and we can
01:31go ahead and rename this.
01:32We want to call this dollars per ounce.
01:37If I run this, we will see that we have got a calculated field that gives us the
01:41price per ounce of each of our products.
01:42If I were to sort base off of this, I can click on the downward arrow and say
01:47Sort Largest to Smallest.
01:48We will see the highest price per ounce product that we carry.
01:52So by using some other mathematical operators to combine the data that's in
01:55our data table, we can come up with new values that might give us some insight into our data.
01:59It should be noted these mathematical operators will only work with numerical data.
02:03Access does offer some similar functionality that applies to text and we'll take
02:06a look at those in the next movie.
Collapse this transcript
Using text
00:01As with the mathematical operations that we looked at in the previous movie,
00:04Access provides us with a few ways that we can modify and combine our text based data as well.
00:10For instance, we can extract a person's initials from a table of first and last names.
00:13We can do this using what Access calls string fFunctions.
00:17The key here is to think about your text based data as a simple string of
00:20characters, so individual letters, and we will use those positions to help us
00:24extract the pieces that we need.
00:25Let's go ahead and create a new query. Create and then the Query Designer.
00:30This query is going to be all about our employees.
00:32So we are going to add that table and close the Show Table window.
00:35I want to add the first name and last name to our query just for reference and
00:40then in the third field, I am going to right-click and say zoom.
00:42The first function that I want to talk about is called Concatenates and that's
00:45represented by the ampersand or the And symbol.
00:48And we can use that to join two separate fields together, and this is how that will work.
00:52So, I am going to add the field FirstName from the table Employees.
00:56So I will start tbl_Employees, period, and then the firstname field.
01:00I'm going to use Concatenate to add that to the LastName field and then we're
01:09going to go ahead and say OK.
01:11Access is automatically going to give it a name of Expression1 and we can change
01:15and we can change this alias if we wanted to, to say Full Name.
01:19Now, if I run our query, we will see that we get our FirstName column and the
01:22LastName column directly from our table.
01:24We also have this new calculated field that combines the first and last names together.
01:29Now currently we don't have any spaces in between and so we can go back into our
01:32query and fix that up a little bit.
01:34So back into the Design View, right-click again and say Zoom.
01:37Access is actually putting these together exactly as it's written, so we are
01:40joining the FirstName directly to the LastName with nothing in between.
01:44Let's change that around a little bit by typing in a quote, and then a space,
01:48and then a closing quote and then another ampersand.
01:51So now this is reading, we are going to join the first name to a space and join
01:55all of that to the last name.
01:56Let's go ahead and say OK, run it again, and we see that cleaned up a little bit.
02:01Let's go back into our Design View and take a look at another example.
02:04In the fourth field, again right-click and say Zoom.
02:08For this field, we want to extract just the employee's initials and we can do
02:11that using a function called Left.
02:13We will type in the word Left and then open a parenthesis.
02:16Now left enquires two elements. First we need to know which field we want to
02:20extract the data from and then we need to know how many characters to extract.
02:23So we are going to feed it the first name from our Employees table,
02:27tbl_Employees, and then the FirstName field from that table.
02:31Then we are going to add a comma and we are going to say how many characters to extract.
02:36In this case, we just want the first letter from the first name.
02:39Go ahead and close our parenthesis and we will use Concatenate to add a period
02:42to the end of this.
02:44So and, and then put a period and quotation marks.
02:47So we are literally adding a period to the end of that.
02:49Let's go ahead and say OK and if we say run, we will notice that we get the
02:54first initial of the person's first name.
02:55One more time, we can go back into our expression, we are going to
02:59right-click, and go to Zoom.
03:02We could join this whole thing to their last name by typing in tbl_Employees.
03:10And we are going to choose the LastName field from the table.
03:13So now we have an expression and while I am actually here in the Zoom box I can
03:17change the alias right here.
03:19So this is going to be the initials and so that's going to be the First
03:23Initial and LastName.
03:27So I've got a statement that reads like this.
03:29From the FirstName in the table Employees, we are going to extract the first
03:32character using the Left function.
03:34We are going to join that to a period and then we are going to join that to the
03:38employee's LastName.
03:40Now I am actually going to add this as a period and a space and go ahead and say
03:43OK to this, and if I run it now, we have got a new field that combines the
03:48first initial of the person's first name with the last name.
03:52By combining some of these functions into our queries, we can come up with some
03:54interesting new ways to present our results.
03:56When feeding our query results into a report or simply reviewing the data sheet,
04:01the appropriate and creative use of string functions can increase the
04:04legibility of our data.
Collapse this transcript
5. Using the Expression Builder
The Expression Builder interface
00:00In the last chapter, we took a look at how we can use mathematical and some text
00:04based expressions, in order to build functionalities into our queries.
00:07Now you might have noticed that that included a lot of typing where we had to
00:09type out the field and table names that the titles were coming from.
00:12We can use what's called the Expression Builder to minimize some of that typing.
00:15Let's go up into our Create tab and again we are going to go to Query Design and
00:19we are going to go ahead and use the Expression Builder to recreate the query
00:21from the last movie.
00:22We are going to add our Employees table and close the Show Table window.
00:26I am going to right-click in the first field and this time instead of going to
00:29Zoom, I am going to go to Build.
00:30Now there is another way we can invoke the Expression Builder and that's using
00:33the Build button up here on the Ribbon.
00:35Either way, go ahead and start up the Expression Builder.
00:38Now the Expression Builder window has several panes to it and the first one
00:42up here at the top, functions just like the Zoom box that we have been working with.
00:45We could type in our functions here and Access will put that into the field down here.
00:49We also have some options for the mathematical operators that we looked at.
00:53We've got Concatenate represented here with the ampersand.
00:55We also have our comparison operators and our logical operators.
00:58In the bottom half of the window, we have access to go into all of our tables,
01:02queries, forms, reports and we also have some built-in functions that we will
01:06look out in the next movie.
01:07For now, let's just drill into the Tables folder and you will notice that we
01:10have a list of all the tables that are currently in our database.
01:13I am going to go into our Employees table and the second pane populates with all
01:17of the fields from the Employees table.
01:18So let's rebuild the expression that we built in the last movie.
01:21I am going to choose our FirstName and we are going to use Concatenate to join
01:26that to our LastName.
01:26So I simply double-click on FirstName, click on the Concatenate ampersand symbol,
01:31and add that to our LastName.
01:31Now as we learned in the last movie, this is going to join the FirstName
01:34directly to the LastName and we want a space in between.
01:37So we are going to add our space in there, using quote space quote to indicate
01:41that we are literally adding a space in between those two, and we are going to
01:44join that to our last name using a second ampersand symbol.
01:47Once we say OK, Access drops that down into our field and it gives it an
01:51Expression 1 as the alias. We can name this again Full Name and now when we run
01:57our query, we get the full name of our employee all put together in one field.
02:02As you get more experience writing out the expressions that you want to use, you
02:05might find it quicker just to write them out by hand directly in the QBE grid.
02:08But for learning the proper syntax, troubleshooting or exploring and discovering
02:12how new functions work, the Expression Builder is a fantastic tool.
Collapse this transcript
Exploring some built-in functions
00:00Using the Expression Builder, not only do you have an easy way to reference
00:03every record of every table, form or query, you also have a list of all the
00:07built-in functions within Access.
00:08Let's take a look at some of these.
00:09We're going to go ahead and create a new query and we are going to go into
00:12Design View again and we will choose the Products table.
00:15Let's go ahead and close Show Table.
00:17I am going to right-click on the first field and choose Build, open up
00:20the Expression Builder.
00:21We have seen how we can go into the Tables folder and select from all of the
00:24tables within our database.
00:25We can also go into this Functions folder down here and when I open that up,
00:29I've got two options. One of them is Built-In functions.
00:31When I open up that folder, I get a list of all the function groups
00:34available within Access.
00:35We can take a look at some of these.
00:36We have got functions that deal with conversions, so we can move from one data
00:39type to another or reformat some of our data.
00:42We have got some functions that deal with Date and Time and we are going to have
00:44a whole chapter on Date and Time functions coming up.
00:46We also have a group being called Financial and this is a list of Financial
00:49Calculations such as calculating rates of return.
00:52Now, at anytime if we want to learn how one of these functions we can simply
00:55select it from the list and then click on Help.
00:57Now the Microsoft help documents have gotten much better over the years.
01:00When you click on Help in the Expression Builder,
01:02it actually jumps right to the help document for that specific expression.
01:05So I can click on Rate and we get some help here that shows that the Rate
01:08functions has five arguments and the help document also defines the variables.
01:13We have some information whether they are required or not, what kind of data
01:16type that they expect, and we also have some remarks about how we can use that function.
01:20Let's go ahead and close the help document.
01:22We also have some mathematical functions and we can use some of these, for
01:25instance, we can use this to round our data.
01:28The Program Flow group contains some functions that allow us to automate some of
01:32our processes and we will look at those in an upcoming chapter.
01:34We also have some aggregate functions that we can use and this should look
01:37familiar to you. These are the same functions that are available through the Totals field.
01:41So let's go ahead and see how we can use this in our Expression Builder.
01:43I am going to go ahead and select Average from the list by double-clicking on it
01:47and we can see that Access populates the Expression Builder statement with the
01:50syntax that's required.
01:51So we've got Average and we have got an expression that's wrapped in parenthesis.
01:54So Access is helping us out here a little bit.
01:56It's telling us that it expects us to provide an expression that it is going to
01:59go ahead and Average.
02:00So I can go ahead and click on expression and it highlights this bit of text,
02:04and now we can go into one of our tables, let's say our Products table, and
02:07choose price and if I double-click on price, it will replace that bit of text with
02:12link to the price field in our Products table.
02:15I can go ahead and say OK and it populates that expression down into our field.
02:19Now it also gives us an alias here. We can replace this with Average Price and
02:25now when we run our query, we get the Average Price for all of our products.
02:28So not only is the Expression Builder great tool for helping you construct valid
02:32expressions, it's a fantastic resource for expanding your understanding of all
02:36the functions that Access brings with it.
02:38The Expression Builder will quickly let you browse the list and learn how and
02:41why to use every function at your disposal.
02:43It's well worth the time investment to peruse the list of available functions
02:46and think about how they can be used within your own workflow.
Collapse this transcript
6. Working with Dates in Queries
Understanding dates as serial numbers
00:00You would think that storing dates in your database would be a fairly
00:02straightforward and routine operation.
00:04On the surface, for you and I it is, but behind-the-scenes Access does a lot of
00:08work to make sure that your date- specific records are kept in order.
00:11Understanding how Access stores, retrieves and formats date and time-based data
00:14can really help down the road, and there are a lot of functions built in that
00:17deals specifically with dates and times.
00:19So let's take a look at how Access understands a date field versus a field that
00:22includes both the date and the time.
00:24So we have got a simple table here and we want find out how Access
00:27understands the relationship between these three dates, May 11, May 12, and
00:30May 13, as they relate to May 12.
00:33May 11, Access understands that is less than May 12 or before May 12.
00:36Access has also understood that May 12 is equal to May 12 or that May 13 is
00:41greater than or after May 12.
00:42So there are no real big surprises there.
00:45Now let's see what happens when we include a field that includes both dates and times.
00:48So right now it's May 11, 11:59 and 59 seconds, or one second before midnight.
00:53Access understands that this is less than or before May 12.
00:57At the stroke of midnight Access says that we are equal to or exactly at May 12,
01:01and one second later, so one second after midnight on May 12, Access thinks that
01:05this is greater than May 12.
01:06So there is a slight discrepancy between the way that Access understands dates,
01:10versus dates and times in relation to each other.
01:12Let's go into Access and take a look at some data tables to see why this is.
01:15I have got a couple of sample queries setup.
01:18I am going to go ahead and open up this query qry_DateSerial.
01:19So what this query is doing is taking the dates from our Dates table and it's
01:25also reformatting them into the serial number that Access understands and
01:28stores in its database.
01:29You can see that at the top of our list, all the date serial numbers are
01:33positive and they get smaller as we go back in time.
01:35When we get into the early 1900s we're down to single digits.
01:38We will see that December 31st, 1899 is actually the serial number of one.
01:43December 30th, 1899 is represented by the serial number zero and anything before that
01:47gets increasingly negative.
01:49So the serial number representation of a date is a whole digit, whether
01:52positive or negative.
01:53Let's go ahead and take a look at how we represent times as a serial number.
01:56I have got a query setup here called qry_TimeSerial.
02:00And I am going to go ahead and open that up and I've got a list of times here,
02:03ranging from 12 midnight, which is represented by the serial number .0.
02:07Halfway through the day we are at 12 noon and that's represented by the
02:10decimal .5. Six hours later at 6 p.m. we are three quarters of the way through the day.
02:14So it's represented by .75, and all the way at the end of the day we have got
02:1811:59 and 59 seconds, which is one second before midnight and that's represented
02:22by the serial number .999988.
02:25So if you think about times as fractions of a day, this seems to make sense.
02:29So we get a whole number that represents the day and the decimal that
02:32represents the time.
02:33Now for looking at these serial numbers, we see that the zero is at the very
02:36beginning here, and we just saw a moment ago that the zero represents
02:38December 30th, 1899.
02:40I have got another query here that extracts the date for the time-based values
02:43and if I run that we can see indeed that we're getting the date of 12/30/1899.
02:48Now obviously you are never going to extract a day from the time, but if I take
02:51a look at how this relates to our dates serials,
02:53for instance, if I try and extract the time from our date, this will make
02:56a little more sense.
02:57So Access is understanding that the serial number is a whole number, which
03:01is actually .0, which gives us the hour zero, and the minute zero or exactly 12 midnight.
03:06So this is why we have a slight discrepancy when we are sorting our dates.
03:09If we have just a date value, it's understanding that as 12 midnight and if
03:12we are including a time value, that's understanding that as a fraction of the
03:15day past the date.
03:17Now as a best practice, it's always a good idea to keep your dates and times
03:20separated into two separate fields.
03:22Because the date is represented by a whole number and the time is represented by
03:26a fraction, it's easy to add them together again when needed.
Collapse this transcript
Specifying a range of dates or times
00:00Because Access stores dates as serial numbers, it becomes very easy to perform
00:03date comparisons or to define ranges of dates.
00:06We have seen these comparison operators before, but I thought it would be
00:09valuable to review them and see how they can be applied specifically to
00:12working with dates.
00:13I am going to go ahead and create a new query in Design View.
00:16I am going to go ahead and add our Orders table and we are also going to add
00:20our Customers table.
00:21Let's go ahead and close the Show Table window and I want to populate our
00:25query with the first and last name of the customer and the date that they
00:29placed their order.
00:30In the Criteria for the date, I want to specify a specific date that I am interested in.
00:33For instance, I'm interested in January 9th, 2010.
00:37Now when I press Enter to accept this criteria, Access adds a little bit of code.
00:40So just like we are entering a text criteria and Access adds a quotation marks
00:43around it, when you are entering a date,
00:45Access is going to wrap it in pound symbols.
00:48Let's go ahead and say Run, and we will see the five customers that placed
00:51orders on January 9/20/2010.
00:53Let's go ahead and go back to our Design View.
00:54We can also use comparison operators with our Date Criteria. So let's go ahead
00:58and take a look ahead at all the orders that are placed before 1/13/2005, I was
01:00going to write less than 1/13/2005 and say Run.
01:05We have got a total of five orders that were placed before 13th and we notice
01:09that our results don't return the 13th.
01:10Let's go ahead and return to our Design View.
01:14So let's change this up, and we are going to make it greater than or
01:16after January 13th, 2005.
01:17I run the query again and we will see all the customers that have placed orders after the 13th.
01:23But notice again that we actually don't return any orders that were placed on the 13th.
01:25Let's go back into our Design View. Because it's often a matter of convenience
01:29to include the date that we are specifically interested in our criteria, one the
01:33things we can do is change this up to a greater than or equals to statement.
01:37That way when we run it, the date that we are actually asking for also appears
01:40in the query results.
01:41Let's take a look at another example of how we can use dates.
01:45Back in Design View, I am going to get rid of this and we can specify a range
01:49of dates by using a between and statement. So I can write between, the first
01:53date that I am interested, let's say January 1st, 2010, and then the last date
01:59that I am interesting in, 1-31-2010.
02:02So essentially the entire month of January in 2010. Let's go ahead and run
02:06this query, and you will notice that when we use "between and," not only do we
02:10get the dates between the two that we specify, but we'll also get the dates
02:13that we actually asked for.
02:14So January 1st. If we scroll to the bottom, we also get January 31st.
02:18So it's a little bit different than the results that we would have gotten if
02:20we'd used our comparison operators to specify dates greater than the 1st and less than the 31st.
02:25Using dates and date ranges in a query's Criteria field is a very common way to
02:29return the most significant data or to limit the returns to only the most recent
02:33and relevant time periods.
Collapse this transcript
Using the Format function
00:00There are thousands of ways that dates can be valuable in your database and
00:03there are just as many variations on how they can be formatted or displayed.
00:07They can be written longhand for use on the business letter, or they can be
00:10abbreviated for use on a payroll check.
00:12They can be incorporated in the serial, or production, or batch ID numbers, or
00:16they can be used for quarterly financial reporting.
00:18Because Access stores dates as serial numbers, as we saw in a previous movie,
00:22we have an infinite number of ways that we can display that data.
00:25One of the ways that we can do this is using the Format function.
00:27So we'll go ahead and create a new query in our Design View again, we'll choose
00:32the Orders table, and close the Show Table window.
00:34So we're going to add OrderDate to our query, and we're also going to add OrderID.
00:38In the third column, I'm going to right -click, and we're going to open up the
00:41Zoom window, so we have more room to type.
00:42I'm also going to change the font size up a little bit.
00:45Okay, the first thing I want to do is type in an alias for this new field, and
00:48we're going to call this Formatted Date.
00:50I'm going to end that with a colon, so that Access knows that this is the name of the field.
00:55We're going to start writing our Format function.
00:57We're going to write the word Format and an open parenthesis.
01:00Now Format requires two arguments.
01:02First you need to know what we want to format.
01:04We're going to go ahead and format the data that's down in our OrderDate
01:07field of the tbl_Orders.
01:09So let's go ahead and add that to our expression.
01:11We're gong to open with a square bracket, tbl_Orders, and the field name is OrderDate.
01:19Now as we continue typing, the Zoom box isn't wide enough to hold all this.
01:24So it's going to wrap the text around.
01:25But this is okay because everything is going to actually you put on the single line.
01:29So once we tell Access what we are formatting, we need to tell Access how to format it.
01:33So since we're done with the first argument, we're going to put a comma in and
01:36then the next part is how to format it.
01:38Now Access uses a couple of variables in order to define the formatting.
01:41We're going to use the lowercase letter m to define how we format a month.
01:45If we use a single m, Access is going to format the month with a single or
01:48double digit number.
01:49so 1 for January or 10 for October.
01:52If we use two lowercase Ms together, Access is going to format the month with
01:55a two digit number.
01:56So 01 for January, and 10 for October.
01:59If we use three lower case Ms together, Access is going to abbreviate our month
02:03with the three letter abbreviation for the month name and if we use four Ms
02:06together, Access is going to format the month with the entire month name.
02:10So I'm going to end this with a quotation mark and the closing parentheses to
02:13finish off our Format function.
02:14I'm going to say OK.
02:15We'll just take a look at how Access deals with this.
02:17So we see in our Formatted Date field, we can have the month formatted as
02:21four different ways.
02:22We've got a single digit, we've got the double-digit, we got the three letter
02:25abbreviation, and we have the full name.
02:27Let's go ahead and go back into our Design View and go back into our Zoom box.
02:31Now we can replace these Ms with the letter D and D indicates how we format our days.
02:37We've got the same options here.
02:39We can either use a one-digit day, we can use a two-digit day, we can use a
02:43three letter abbreviation for the day, and we can use the full name of the
02:46day by using four Ds.
02:47Let's go ahead and say OK and run that again.
02:50We can see that we have the date formatted in different way.
02:52We have a one or two-digit dates, the two-digit date,
02:55so if we scroll down, we can find some that are having 1 or 01, three-letter
02:59abbreviation, or the full name.
03:01So let's go back into the Design View and make this actually represent real data.
03:04Let's go ahead and do a Zoom.
03:06So I'm going to put this all together, and we can format our days in a couple of different ways.
03:11We can be really concise and a little bit trendy.
03:14We can format our day with m.d.yy, to represent a two-letter year.
03:20Let's go ahead and say OK and Run.
03:22We can see we have a concise formatting on our date with a period used as a separator.
03:26One more time, we'll go into the Zoom box and we'll change our day a little bit differently.
03:30We'll see that Access understood what we meant to type in and it changed our
03:33code a little bit to match its syntax standards here.
03:36So that's where the slashes come from.
03:38Let's go ahead and format our date a little bit differently.
03:40This time we can make it into a standard that's a little bit more common in Europe.
03:44We're going to put the day first.
03:45We could say dd mmm yyyy.
03:50Now this is going to return a two- digit date, a three-letter abbreviation for
03:53the month, and then a four-digit year.
03:55We can run that, and see we have now 10 Jan 2005, for example.
04:00One more example here. Right-click, go back into Zoom.
04:03Now let's be as verbose as possible.
04:05Let's go ahead and return a date that looks like this.
04:08Four Ds to represent a full day, comma, four Ms to represent the full month, two more
04:15Ds to represent the day in numbers, comma, and four Ys to represent a four-digit year.
04:20Let's go ahead and run that.
04:22We can see we've got a date formatted as Monday, January 10th, 2005.
04:26So as you can see, the Format function can be really flexible in how you display
04:30your dates and it can satisfy almost any reporting need that you may require.
Collapse this transcript
Using other Date/Time functions
00:00Typically, dates and time store within your database are going to be very specific.
00:05Dates are usually specific to a particular day and times are typically
00:08precise to the minute.
00:09But when it comes to evaluating or aggregating your data, you might want to look
00:13at it a little less granularly.
00:14When it comes time to query your data by month and year, Access provides a few
00:18simple ways to extract just the level of precision that you need from your
00:20date and time fields.
00:21So we'll go ahead and create a new query in Design View.
00:23I'm going to add our Orders table, and I'll go ahead and close the Show Table window.
00:26Now we're going to add two instances of our OrderDate.
00:29I'm also going to turn on our Totals row and I'm going to change the
00:33first Total to Count.
00:35If I run this query, we're going to get a list of the number of orders that
00:39were placed on each day.
00:40We can see that some days had multiple orders and some days only had one order.
00:43Let's go ahead and go back into our Design View.
00:46So if I wanted to organize my database off of a different level of detail,
00:49for instance, I want to group everything by a specific month, I can use a
00:52function to tell Access to do this.
00:54Let's go ahead and right-click on our second OrderDate column and open up the Zoom box.
00:58At the very beginning, I am going to type it a new alias and I'm going to
01:01call this Grouping.
01:02I'm going to use a function for Month.
01:04I'm going to wrap our OrderDate field in square brackets to tell Access
01:08that this is the data that we're going to be using.
01:10So now we're going to be extracting just the month from the OrderDate.
01:14Go ahead and say OK and run our query again.
01:16You can see that we get a numeric code that represents the month that we're in.
01:19So for instance, in the month of January we sold a total of 199 products.
01:23For the month of December we sold 151.
01:26Now this is grouping all January's together.
01:28So it doesn't matter if it's in January 2005 or January 2010. All the January
01:32dates are getting looped together in this view.
01:34We can go back into our Design View and do the same thing for year.
01:37This time we're going to type it directly into the field here.
01:39So I'll expand this open a little bit.
01:40We're going to put a new alias here.
01:42We're going to call this one Group by Year.
01:43We're going to use the Year function.
01:46I'm going to pull the year out of our OrderDate, finishing with a square
01:50bracket, and a closing parenthesis.
01:52I'm going to go back to our first one here and specify that this is Group by Month.
01:56So we can tell the difference between the two.
02:00Now when I run our query, we'll see that now we have all of our January
02:032005s grouped together.
02:05We can see that we have a total of 29 orders in January 05.
02:07But let's go ahead and sort this little bit differently.
02:10I'm going to sort the Month from smallest to largest.
02:13Then we'll sort the Year from smallest to largest.
02:16Now we can get a better look at our data, so we have everything in order from
02:19January to December 2005, and then January to December 2006 and so on.
02:22So this is how we would use the Month and Year functions to format our dates.
02:26We have similar functions for day, so that will be a Day and then parenthesis, or
02:30Weekday and then parenthesis.
02:33There are also functions that do the same thing for times, for hour, minute, and
02:37second, and they will work exactly the same way.
02:39So in order to maintain flexibility, it's always best to store data based on the
02:42finest level of detail that you can ever conceivably require.
02:46Typically, this is going to be a date specific to the day and a time
02:48specific to the minute.
02:49Using queries, it's easy enough to strip away all the fluff based upon the
02:52specific task that you're working with and when you don't need that level of detail.
02:56With these Date and Time functions, Access makes it easy to see exactly what you
02:59need and nothing that you don't.
Collapse this transcript
Defining today's date
00:00So far we've taken a look at how we can work with dates relative to each other,
00:03or relative to a specific and static date range.
00:06Access provides us with a couple of additional functions that will allow us to
00:10define dates relative to today, or relative to right now.
00:13This is useful for all queries that require a date range such as within the last
00:1730 days, or two weeks from today. Dates with a specific relationship to the
00:21current date and time.
00:23The first part of this requires us to accurately define today's date and time.
00:26In the next movie, we'll take a look at how we can combine this with some
00:28additional functions to perform some calculations to define a timespan relative to today.
00:33Once again, we're going to go ahead and create a new query. Design View.
00:37I'm going to add our Orders table, and close the Show Table window.
00:40Now the first function I want to show you is called Date.
00:43If we just write the word Date, an open parenthesis, and then immediately close
00:47it with a close parenthesis, and then run this query, Access is going to
00:51return today's date.
00:52You can see that I'm recording this movie on July 19, 2010.
00:56So let's go ahead and go back to our Design View.
00:58I'm going to change the Expression 1 alias to Today.
01:01We also have a function called Time and that's going to return the current time.
01:05So I'm going to write Time as an alias, and then I'm going to write the Time
01:08function, which is followed by an open and then a closed parenthesis.
01:11Finally, we have a function that will return both the date and the time.
01:15I'm going to call this one Now as an alias and the function is actually
01:19called Now as well.
01:20So I'll write Now, open and then close the parenthesis.
01:22Let's go ahead and run these again.
01:25We'll see that, a little more specifically, not only is it July 19, 2010, but
01:30it's currently 1:10 and 50 seconds PM. We can see the Now column combines both of
01:34these fields into one.
01:35Let's go ahead and switch into Design View and wait a couple of seconds.
01:39And then if we switch back, we'll see that has been updated.
01:42It's now 1:11 and 12 seconds.
01:44Now if we wait a long enough, we would also see the date roll over, but that
01:47would make a really long movie.
01:48So you are just going to have to trust me on this one.
01:50Let's go ahead and switch back into Design View.
01:53Now we can combine these three functions with some of the formatting options
01:57that we saw previously.
01:58I'm going to go ahead and delete these two fields from my query.
02:00I'm going to highlight that and this time I'll press Delete Columns.
02:03I'm going to combine our Format function with today's current date.
02:07We can do that by writing Format, open parenthesis, and let me expand this out
02:11so we can see what we're doing.
02:13If you remember, the Format function requires two arguments.
02:15First we need to know what we're formatting.
02:17So right now, we're going to format today's date.
02:19So I'm going to follow that with a comma.
02:20The second thing we need to provide is to tell Access how we're going to format this date.
02:24So let's say I want to find out today's day.
02:27I will write four Ds to give us a full name of the day, wrap that in quotation
02:30marks, and then finish my Format function with a closing parenthesis.
02:34Let's go ahead and run that query.
02:35We'll see that today is in fact Monday.
02:37So we can combine formatting with our Date, Time, and Now functions in order to
02:42give us specific information about today's date and time.
02:44Now that we can accurately define the current time, we can use this
02:47functionality to help us define date ranges relative to today.
02:51We'll take a look at that in the next movie.
Collapse this transcript
Working with time intervals
00:00Now that we know how to tell Access how to return today's date and current time,
00:03we can use that information to help obtain query results that are relative to today.
00:07We have two different functions that will return slightly different results
00:09depending on the type of question that we're going to ask.
00:12The first one is DateAdd.
00:14This will allow us to specify a time frame such as the past 30 days, or
00:17within the last year.
00:18It will return records within that time frame.
00:20Let's go ahead and take a look at how we can use DateAdd.
00:23We're going to start a new query in Design View.
00:25I'm going to add our Orders table and we're going to go ahead and close the Show Table window.
00:30In the first field, I'm going to right-click and say Zoom.
00:32We're going to take a look at the DateAdd function.
00:35The first thing I'm going to do is add an alias to our field header.
00:37We're going to call this 30 Days Ago, followed by a colon.
00:44Now the DateAdd function has three required parts.
00:48The first part is called the interval and this is going to be the units that
00:51we're adding or subtracting from our date.
00:52So this is where we tell Access whether we want to add or subtract days, months, or years.
00:58The second part is called number and this is going to tell Access how many of
01:01the interval that we're going to add or subtract.
01:03So, how many days, months, or years?
01:05The final part is the Date.
01:07This is the date that we want to add or subtract the values from.
01:09Now we can supply this with a date from one of our tables,
01:12for instance, the OrderDate from the tbl_Orders, or we can specify today's date
01:15by using the Date function with an open and then close parenthesis.
01:17Now we need to tell Access where our interval is going to be.
01:21We're looking at 30 days ago, so our interval is going to be a d for day.
01:25The number that we want to add or subtract is 30.
01:27But since we're subtracting it, we would need to put a negative.
01:29Let's go ahead and say OK and run our query.
01:33Now earlier I told you that I'm recording this movie on July 19th, 2010.
01:37So a day 30 days ago will be June 19th, 2010.
01:39That's the result that we get.
01:41Let's go ahead and go back into our Design window.
01:43I'm going to right-click and go back to Zoom.
01:45Now there are a couple of different variables that we can use here to
01:49specify different units.
01:50We're seeing how we can use d for day.
01:52We can also use w if we wanted to add or subtract weeks.
01:55We could use m if we wanted to add or subtract months.
01:58If we wanted to add or subtract years, it's similar, but we need to put four Ys
02:02to tell Access that we're going to add or subtract a year.
02:04So I'm going to change this back to a d. I'm going to say OK.
02:07I'm going to expand this window open a little bit more so I can see what we're doing.
02:11I'm going to take this and I'm actually going to copy this out.
02:14So I'm going to highlight the whole thing and press Ctrl+C. Now I'm going to
02:18delete this field because we're going to do something slightly different.
02:20So I'm going to delete this column.
02:22Now I want to create a query that will select all of the orders that were placed
02:26within the last 30 days.
02:27I'm going to make this query using the OrderID and then an OrderDate field.
02:32For the Criteria of the OrderDate, I want to specify the same formula that we just created.
02:35So I'm going to say greater than, and then I'm going to paste in the
02:38formula that we created.
02:39Let me expand this out so we can read it.
02:42So I'm saying the Criteria for our OrderDate is going to be every day that's
02:46DateAdd 30 days ago.
02:49So my Criteria is going to be every OrderDate that's greater than and then this
02:52is the formula for 30 days ago.
02:54Let's go ahead and run this query and see what we get.
02:57We can see that we get a total of 19 orders that were placed within the last 30 days.
03:01So I want to remind you that this 30 days ago is relative to today, the day that
03:05I'm recording this movie.
03:06If you're following along with the Exercise Files, your results might differ
03:09slightly as you'll be running query in a different day.
03:11In the next movie, we'll take a look at a similar function called DateDiff.
Collapse this transcript
Using the DateDiff function
00:00In the last movie we took a look at how we can use the DateAdd function to allow
00:03us to specify a date in the past.
00:05I'm going to go ahead and open up the query that we are working on, and that is
00:07called to qry_RecentOrders.
00:08And if I go ahead and view it in Design View, I'll open up our field and we can
00:14take a look at the Criteria.
00:15So I specify that we want to view order dates that are greater than and then
00:19we're using or DateAdd function to specify the date 30 days ago.
00:22Now if I run this query again, I'll take a look at it, and I know that all of
00:25these orders were placed within the last 30 days, but I don't know exactly how
00:28many days ago that they were placed.
00:30I can use the DateDiff function to tell us that.
00:32Let's go back into Design View and we're going to create a new calculated field
00:35here and let's do that in the Zoom box.
00:38The name of this field is going to be Days Elapsed.
00:41And the function that we're going to using is DateDiff and this function
00:46is similar to DateAdd.
00:47It requires three arguments.
00:50The first one is the Interval and this will be same unit that we were using
00:53before, so whether we're looking at months, days, or years.
00:56The second argument is our first date, and our third argument is our second date.
01:00So basically the two dates that we want to compare to each other.
01:02Let's go ahead and plug in our numbers.
01:04Data here is going to be days, the first date is going to be coming from
01:10our orders table, so I can specify that as our tbl_Orders, and our field is OrderDate.
01:24The second date that we want to compare to is today, so go ahead and use our Date function.
01:27Let's go ahead say OK and we'll run our query again.
01:30You'll see that we now have a column that says Days Elapsed and we can tell
01:34exactly how many days have passed since this order was placed.
01:36So DateDiff and DateAdd are two of the functions that are available to us when
01:39we're working with date-based data.
01:41Working with dates is an integral part of making sense of all the data
01:44within your database.
01:45With a little bit of practice and persistence, mastering date and time-based
01:48data formats and using it to filter all the relevant data that you need will
01:52help make your queries that much more successful.
Collapse this transcript
7. Understanding Conditional Statements
Introduction to the conditional IIf function
00:01In this chapter we're going to take a look at creating a new expression called a
00:03conditional statement.
00:04Now, conditional statements are one of my favorite features of Access queries.
00:07They allow you create an automated decision-making process.
00:10They're really flexible and very powerful and can automate some complex tasks in
00:14a short amount of time.
00:15You can think of a conditional statement as an If, Then statement.
00:19So If A is true, then we'll return B. Otherwise we'll return C. We can rewrite
00:24this as If A, B, C, and this is implied to If A, then B, then C. Putting it
00:30into a format that Access will understand, we have the IIF statement, which is the conditional.
00:35So IIF and then we have three argument,s A, B, and C. The A is stands for our
00:39expression that we're going to evaluate, B stands for our true part, and C
00:44stands for the false part.
00:45We take an expression and evaluate it and an expression can be anything like
00:49today equals Monday or total sales is greater than $50.
00:54Anything that Access can say, yup, that's true or no that's false.
00:57So once we evaluate the expression, if it's true, Access will return the
01:00true part of our function; if it's false, Access will return the false part of our function.
01:05When creating the condition that we want to evaluate, we can use most of the
01:08comparison operators that we've seen in this course.
01:10So that will be less than, greater than, equal to, etcetera.
01:13You can also use some logical operators, AND, OR, and NOT.
01:16In our queries, this is that how we might type out our conditional statement.
01:20And let's take a look at how we can read this.
01:22The first part asks, is August sales over $100,000?
01:25If that's true, we'll return the value "met sales plan." If that statement evaluates
01:30to false, we'll say we're "below sales plan."
01:32So let's take a look at how this works in our data table.
01:35Here I've got five stores. I've got store number 101 and we can see that their
01:38August sales is $103,588.
01:41If we take that value and plug it into our equation, we get a statement that
01:45reads 103,588 is greater than 100,000, and that statement is true, so we'll
01:51return the true part of our expression, "met sales plan."
01:54For store 102, we've got an expression that reads 125,491 is greater than 100,000.
02:00Now that's also true.
02:03So we can say store number 102 met their sales plan as well.
02:06For store 103 we have an expression that reads 98,743 is greater than 100,000.
02:12Now that statement is actually false, so we're going to skip to the end and
02:15we're going to put in our false part expression, below sales plan.
02:18We can finish up the other two stores and see how they fared as well.
02:22Now, Access can process a conditional statement much faster that I can I can go
02:25through the data table.
02:26Now, I'm sure if you think about the data that's in your database, you can come
02:29up with a lot of instances where you have to go through your data, review it,
02:33take some sort of decision, and then put that into your data table.
02:36By using a conditional statement we can automate that process.
02:40In the next movie we'll go ahead and jump into Access and we'll see how we can
02:43use conditional statements in our queries. w
Collapse this transcript
Creating an IIf function with the Expression Builder
00:00In this video, we'll take a look at writing our own conditional statements using
00:03the Query Designer in Access.
00:04We're going to use the Expression Builder to help us generate proper syntax and
00:08we'll use some mathematical expressions to evaluate our data.
00:10Let's take a look at how we could do that.
00:11We'll go ahead and go to the Create tab, new Query Design and I'm going to add
00:15three tables: our Customers table, our Orders table, and our Products table.
00:20Go ahead and close the Show Table window.
00:22Now, from these tables I'm going to add a bunch of fields to our query.
00:24First, we're going to add the OrderID, the customer's first name and last name,
00:28the state the customer is from, and the price that they paid for their order.
00:32Next, we're going to create our conditional statement.
00:34In the next open field I'm going to right-click and say Build and that will open
00:38up our Expression Builder.
00:39Now, we're going to use this to build an expression to determine whether or not
00:42we need to charge sales tax to our customers.
00:44We can find the IIf conditional statement under the Functions folder, the
00:48Built-In Functions, in the Program Flow group. Double-click on IIf and it will
00:53populate into the Expression Builder.
00:54Now, you can see that the IIf requires three parts.
00:57We have the expression, the true part, and the false part.
01:00For expression we need to obtain the state that the customer is from.
01:02We can find that in the Tables, Customers folder, and in the State field.
01:08So I'll double-click on that and that will add it to our expression.
01:10Now, we don't need to charge sales tax to everybody.
01:12So let's say that the Two Trees Olive Oil company operates in California.
01:15We have a web-based business and all of our customers order over the Internet.
01:19We only need to charge sales tax to the customers that actually live in
01:21California where we're based.
01:23So for the table state we're going to evaluate the expression =CA, and I'm going
01:28to wrap that in quotation marks.
01:30So as we're evaluating our IIf statement if the customer's state is California
01:34then we're going to return the true part.
01:36The part that we want to return is the sales tax rate that we're going to charge.
01:39In IIf we're going to say it's 8.25%.
01:39If the customers that not live in California we're going to return sales tax rate of zero.
01:45So let's go ahead and say OK.
01:46And Access actually populates an Expression 1 as our field header and we can go ahead
01:51and rename this to Tax Rate.
01:52Let's go ahead and run the query.
01:55Now, we can see for all of our customers who live in California we're getting a
01:58value of eight and a quarter, and for everybody else we're getting a value of 0.
02:01Let's go into our Design View and we'll do one more.
02:03Right-click in the next open field and say Build.
02:07We'll go down into our Functions group, Built-In Functions, Program Flow and IIf again.
02:13This time let's say we have a bonus program going where if you place an order
02:16for more than $50 you get free shipping.
02:19Our expression will look something like this.
02:20We go into our Tables, Products and grab the Price, and then we're going to say
02:25it evaluates to >50.
02:28So if price is greater than 50 we'll return the true part and we're going to put
02:31up a message that says "Free Shipping."
02:35If the price of less than $50 we don't want to return anything, we're just want
02:38a blank cell, and we can use the word NULL to return a blank value.
02:42Go ahead and say OK and Run.
02:45And I'm going to sort this by Tax Rate and we can see that most of the people
02:49that live in California were ordering less than $50.
02:50Let's scroll down and find this person, Hunter Foster who lives in California,
02:55so they will be paying the 8.25 sales tax, but they also ordered over $80 worth
02:58of products, so they get free shipping.
03:00So there are our first two conditional statements.
03:02We made use of the Expression Builder to help us format our statement properly
03:05and to help reduce errors.
03:07And we were also able to verify that it returned the expected result.
03:09I hope at this point that you are beginning to see why I like
03:12conditional statements so much.
03:13Being able to instruct Access how to make decisions on its own I think is pretty cool,
03:17but more importantly I want you to take a moment before moving onto the
03:19next movie and think about all the ways that your workflow could benefit from
03:23using conditional statements.
03:24I promise that once you start looking you're going to find a ton of ways where
03:27you can find them useful.
03:28In the next movie, we'll take a look at one of the other Program Flow
03:31functions called Switch.
Collapse this transcript
Using the Switch function
00:01Within the Program Flow category of functions within the Expression Builder is
00:04another function called Switch.
00:06The Switch function is similar to IF in that it allows Access to return various
00:10responses depending on the results of an evaluated expression.
00:14The difference between IF and Switch is that Switch decision tree only branches
00:18in the false direction.
00:19But you can have as many options as you want.
00:22In other words, Access reads through the function and returns the first true
00:25condition it happens upon.
00:27If the condition evaluates to false, Access proceeds to the next condition and
00:31keeps going until it gets to the end.
00:32So let's take a look at how we would write a Switch expression.
00:35You start with the word Switch and then in parenthesis we have pairs of
00:39expressions and values.
00:40We have an expression A and then a value A, expression B, value B, expression C, value C, etcetera.
00:47The decision tree would look something like this.
00:49We have expression A. If that's true then Access will return value A. If it's
00:53false, we'll move on to the next expression.
00:55We have expression B, if that's true, we return value B. If expression B is false
01:01we move on down the line.
01:02If we get all the way to the end and get to expression C and if expression C is
01:06false, Access will return a blank cell or the word NULL.
01:09So let's go ahead and apply this in a query inside of Access.
01:13I've got a query here called qry_Switch.
01:16I am going to right-click on that and open it up in the Design View.
01:19I've got a query built here that's actually built off of another query.
01:23I've got Employee Name, FirstName and the TotalSales as a calculated value in this query.
01:28I also have a switch statement set up here and actually instead of opening it up,
01:32I'm going to right-click and say Zoom.
01:34And this is how our switch statement is constructed.
01:37The field name is going to be Rating, and this is part of a rewards and
01:40recognition program that we have set up for employees.
01:42We are going to take a look at our employee's total sales throughout their
01:45entire lifetime with the company and evaluate them based on their ranking.
01:49So we're going to use the Switch() function here, and we're going to
01:52evaluate total sales.
01:53If the total sales is greater than $1000 then we'll consider that employee as
01:56part of a Platinum group.
01:58If the total sales is greater than $900, then we'll consider those employees as
02:01part of our Gold group.
02:02If the total sales is greater than $800, we'll consider them part of the Silver
02:06group and if the sales are greater than 700, they are part of our Bronze group.
02:10So let's go ahead and say OK and we'll run our query.
02:12I'm going to apply a sort to our total sales and we'll go from largest to smallest.
02:17We will see that we have several employees that are above $1000 for the company,
02:20and they'll get a rating of Platinum.
02:23Below them we have a group that's above $900, and they'll get a rating of Gold.
02:27We can go ahead and switch back to our Design View and I'm going to go into our
02:31Zoom box one more time.
02:32So what's happening is that for each employee, we're going to take their total
02:36sales and plug it into the formula everywhere you see the world TotalSales.
02:39Then we're going to go through and evaluate the expression.
02:42So if their total sales is greater than a thousand, Access will return the value
02:45to Platinum and then move on to the next employee.
02:49If the total sales is less than a thousand, then we'll move to the next statement.
02:52If the total sales is also less then 900, we move to the next statement.
02:55If the employee's total sales is 850 for example, we'll plug that in here.
02:59850 is greater than a thousand. That's not true.
03:02So move to the next one.
03:03850 is greater than 900, also not true.
03:07We move to the next one.
03:08850 is greater than 800. That's true.
03:11So that employee is part of our Silver group.
03:14So that's how the Switch function works.
03:16When used appropriately, both IIF and Switch can be really valuable tools to
03:20have in your pocket.
03:21Anytime you can automate some routine tasks and allow Access to make decisions
03:25for you could be a huge benefit to increase your productivity.
03:28But beyond simply taking decision- making processes off your plate, learning to
03:32use some of the Program Flow functions increases the reliability and
03:35consistency of your database.
03:37One of the biggest benefits is that they can ultimately prevent data
03:39inconsistencies due to the introduction of human error.
Collapse this transcript
8. Using Alternate Query Types
Using crosstab queries
00:00All the queries that we've been working with throughout this course have been
00:03examples of select queries.
00:05Basically queries that select and manipulate your data from a data tables and
00:08present the results in a new but temporary table format.
00:12Access also has a couple of other query types that we can work with.
00:15And the first one I want to look at is called the crosstab query.
00:18Like the select queries, the crosstab query pulls information from our
00:21tables and reorganizes it.
00:23But unlike a select query, the crosstab query is presented more like a standard
00:27spreadsheet with column headers and row headers.
00:29We can take a look at this easily if we go into our Create tab and then to Query Wizard.
00:33You can see the second option down as Crosstab Query Wizard and I'm going to go ahead and say OK.
00:37Now this first window says which table the query contains the fields you want
00:41for the crosstab query results.
00:42We're going to build a crosstab query that deals with our employee information.
00:45Let's go ahead and say Next.
00:48The next screen asks for which fields you want to use as our row headings and
00:51we're going to go ahead and choose State.
00:53When I move that over, you'll notice that Access updates the sample down here
00:56so, we're seeing the State is going to be our left-hand column of our table.
00:58Let's go ahead and say Next.
01:01This screen asks for which values we want to use as our column headings and
01:04we're going to choose Departments.
01:06Select that and you'll see again those samples updating.
01:09Go ahead and say Next.
01:11Finally, we need to supply the information that's going to go into the tabulated
01:14area down here and by default, it's selecting the first field and it's choosing
01:17average of employee ID.
01:19What I really want to do is get our count of our employees.
01:21So let's go ahead and select Count and we'll see that it updates here.
01:24I also want to include a Summary column, so I'm going to leave this checkbox on
01:27and I'll point out what that does in a moment.
01:29Go ahead and say Next.
01:30We are going to save our query as qry_DepartmentsByState.
01:31We'll leave this option on to view our query and go ahead and say Finish.
01:39And you'll see that we get a crosstab query set up where we have States in
01:42the left-hand column.
01:43We've got our Departments across the top.
01:45And we have got a grid that counts the number of employees in each department
01:47and lists them by state.
01:49We also have a Summary column here called Total Of Employee ID and this is the
01:52total number of employees.
01:54So for instance in Alaska, we've got a total of five employees, two of them are
01:58in the Customer Service department, one is in Human Resources, and we've got
02:01two people in Sales.
02:02I've also got another query set up already called AverageWageCrosstab, and if I
02:07open that up, you'll notice that it's in a similar format with states on the
02:09left and our departments on the right.
02:12The difference is that in the middle, we are calculating the average wage by
02:15Department and by comparing these two tables together we can perform some
02:18interesting analysis work.
02:19We'll go ahead and close both of these down and I'll go ahead and save the
02:23changes the query we just saved.
02:24I've got one more query set up right now and I'll go ahead and open that up
02:29and the name of it is qry_Crosstab, but is currently not displaying in the crosstab format.
02:33I can tell this is a select query right now based on the icon and you can see
02:37that this is the icon for a crosstab query.
02:40This query is pulling out our OilName and sizes and we're getting a count of the
02:43number of times that each has been ordered.
02:45You'll see that there is a lot of duplication here.
02:47We've got the word Gallon represented 18 times because we have 18 different
02:50oils and we've got each of our oils listed five times because we have five
02:53different sizes of each.
02:54Let's go ahead and switch in to our Design View and we're going to go to the
02:57Home tab and then press Design.
02:59We can update this to be a crosstab query, which will be a much more efficient way
03:02to display that data.
03:03If I switch to Crosstab, you will notice that we've got a new row down here and
03:06we can tell Access which fields put into which areas.
03:09So the count of our Product ID is going to be the value in the center of the table.
03:13Our size is going to be our Column Headings and our OilName is going to be the Row Headings.
03:18Now if I run this query again to see the results, we'll get it in a much
03:20more efficient table.
03:21Now I can look up and down these rows here and see that our Light Oil is the
03:24bestseller in the Gallon size.
03:26Now, it would be convenient if we could actually sort base out for these column
03:29headers, for instance Largest to Smallest.
03:30But Access actually throws up an error and says that we can't sort when it's in
03:33the crosstab format.
03:34What we could do though is export this into Excel and we can take a look at
03:38some of the data there.
03:38We can go to the External Data tab and press Excel.
03:42I am going to go ahead and browse, and I'll save this in my desktop,
03:46our Exercise Files folder and then Chapter 8.
03:48Let's go ahead and say Save and I want to pay attention to this checkbox right
03:53here that says Export data with formatting and layout.
03:55While this is not checked, Access is going to export the data table as if it was
03:59just a simple select query.
04:00So basically those three columns that we saw earlier.
04:02If you want to export this in the crosstab format we need to make sure this is turned on.
04:06Let's go ahead and say OK.
04:07And I don't necessarily need to save the export steps, so we can go ahead and just say Close.
04:11Let's go into Excel and we'll see what this data table looks like.
04:14I am going to browse out to my Exercise 8 folder and open up the Excel file that we just made.
04:19Now in Excel I can go ahead and click on Gallon and I'm going to apply a filter
04:23here, sort largest to smallest.
04:25We'll see that the Light oil comes up to the top in the gallon size.
04:28So that's our best-selling gallon product. And sort largest to smallest,
04:31we'll see that the Virgin oil comes to the top.
04:33So the Virgin oil and the small sizes are bestseller.
04:36So depending on the types of information, you're collecting, the crosstab query
04:39can be a great way to visualize a large amount of aggregated data and provide
04:43further insight into your database.
Collapse this transcript
Finding duplicate queries
00:01In the Query Wizard, we've seen that there are options beyond the simple select
00:03query. One of those options is called the find duplicates query.
00:07Now, the find duplicates query is a specific query type that will locate
00:10potential duplicate records within your database.
00:12This can be useful for cleaning up your data tables or reviewing records
00:15with multiple entries.
00:17We can get to that by going up to the Create tab and constructing a new
00:19query with the wizard.
00:20We're going to select the Find Duplicates Query option and say OK.
00:24Now we're going to take a look at our Orders table to find out if we have any
00:27potential duplicate orders that are in our database.
00:29Let's go head and say Next.
00:31The next window that comes up asks us which fields might contain
00:34duplicate information?
00:35Now, because of the way the table is constructed, I know that OrderID is a unique value.
00:38It's the primary key for the table.
00:40So there is no possible way that we could have two orders with the same ID.
00:43But we could have two orders of the same order date.
00:46In fact, it's pretty likely that we'll have multiple orders with the same order date.
00:48We could also potentially have two orders with the same customer ID, two orders
00:53for the same product, or two orders from the same salesperson.
00:56But the likelihood that we have two orders that are placed on the same day,
00:59by the same customer, for the same product, and with the same salesperson is exceedingly low.
01:04So those are the ones that we want to look for.
01:06Let's go ahead and say Next.
01:08You can also include the OrderID field as a reference, but it won't be used to
01:11determine whether an order is a duplicate or not.
01:13Let's move that over and go ahead and say OK.
01:15The next screen gives us an opportunity to save our query.
01:19I'm going to call this qry_DuplicateOrders.
01:23We're going to choose the option to view the results and we'll say Finish.
01:26Access goes through our Orders table and it finds two orders that are likely
01:29candidates to be duplicates.
01:31Now, this doesn't necessarily indicate that we have a problem.
01:33We could have a situation here where we have an order that was accidentally put
01:36into our Orders table twice, in which case we have a duplicate entry.
01:39But there could be a perfectly valid reason why we have two orders that look similar.
01:43I can see that our order date is a couple of days before Mother's Day, and there
01:47could be a situation where we have a single customer who is placing an order
01:49for a specific bottle of olive oil for his mom, and placing a second order to
01:53go to his grandmother.
01:54So determining whether we have a problem with our database might involve some
01:57additional investigation.
01:59Sorting out data entry errors that can often arise with importing tables from
02:02external sources is a perfect use for Find Duplicates.
02:05However, finding potential duplicate records in your database doesn't
02:08necessarily indicate that there is a problem.
Collapse this transcript
Finding unmatched queries
00:01Over time, your database will grow and change.
00:03Records will get added and removed.
00:05Throughout these changes, it's possible that records in some tables may lose
00:08connection to related records in other tables.
00:10If we take a look at our Relationships window by going to Database Tools and
00:13then clicking Relationships, we'll see that the most common join type between
00:16tables is the one-to-many relationship.
00:18That means that each product in our Products table will only be in there once,
00:21but we can have each product in our Orders table multiple times.
00:25The next query type that I want to take a look at is called Find Unmatched.
00:28That will review two tables together.
00:30It will find all the records in one that aren't in the other.
00:32For instance, all the products in our Products table that haven't had an order
00:35placed for them, or going the other way, all of the orders placed for products
00:39that don't exist in our Products table.
00:41Now, if we review the relationship between these two tables by double-clicking
00:44on the join line, we'll see that we're Enforcing Referential Integrity
00:47between these two tables.
00:48What that means is that it's actually not possible to have an order for a
00:51product that doesn't exist in our Products table.
00:53So we won't find any unmatched records that direction.
00:56But it is perfectly likely that we might have a product in our Products table
00:59that nobody has yet placed an order for.
01:01So let's go ahead and take a look at our unmatched query to see if this is the case.
01:04I'm going to go ahead and close our Relationships window.
01:06Go to the Create tab.
01:08We're going to create a new query with the Wizard.
01:10We're going to select the Find Unmatched Query, and go ahead and say OK.
01:13Now, the first screen asks us Which table or query contains the records we
01:17want in our results?
01:18We want to find all the products that aren't in the Orders table, so
01:21we'll select Products.
01:22Go ahead and say Next.
01:24The next screen asks which table or query contains the related records?
01:27Here we're going to go ahead and select Orders.
01:28One more time say Next.
01:31Now, Access has already identified that our ProductID occurs in both tables. So that's good.
01:35We can go ahead and say Next.
01:37For our query, we want to include a ProductID and a ProductName.
01:40Let's go ahead and say Next, and save our query as qry_UnmatchedProducts, and say Finish.
01:47Now, we'll see that Access has identified a single product in our Products table
01:51that nobody has yet placed an order for.
01:53That's our Virgin Oil, the new 640 ounce size, which is intended for
01:56the restaurant market.
01:58Just like the Find Duplicates query, finding unmatched records doesn't
02:01necessarily mean that we have a problem.
02:03The Find Unmatched query can be a simple way to identify potential problem areas
02:07and indicate avenues of further investigation.
Collapse this transcript
Working with action queries and backups
00:00There are four additional types of queries within Access and they are
00:03collectively called action queries.
00:05We'll take a look at them over the next few movies, starting with the update query.
00:08Now, an action query is different from everything else that we have worked on so
00:10far in one key area; they actually modify your data tables.
00:14In case it's not perfectly clear, all of the queries that we've run up to
00:17now simply presented your data, rearranged and reformatted, in temporary
00:21tables called record sets.
00:22All of the actual data tables within your database have remained unchanged.
00:26Action queries will throw that behavior out of the window;
00:28they will permanently modify your data.
00:30This can be really powerful when used appropriately or it can make a really
00:33big mess really fast.
00:34So before we jump in, I want you to take a moment to let that sink in and I
00:37also want to walk you through the critically important steps of creating a
00:40backup of your database.
00:41Luckily, Access makes this really easy, and we can go up to the Office button,
00:45down to the Manage button, and select Backup Database.
00:48Access is going to ask us where we want to save it, and I am going to put this
00:50in our Chapter 8 folder, but if you have access to an off-site backup location
00:54or remote server, you could also put it there.
00:55You can also see that Access has appended today's date to the end of our file name.
00:59Go ahead and say Save.
01:00And that's it. You've got a backup of your database.
01:03Let's go browse out to our Chapter 8 folder and we see that our action query's
01:06backup is right here.
01:07You can also take a look at the file size and see that Access has actually
01:09compacted our database quite a bit, but don't worry about that, because all of
01:12our data is still intact.
01:14Action queries are both destructive and irreversible, but I don't want that to
01:17prevent you from making use of them.
01:19Now that we're protected from any unfortunate mishaps, we can move forward.
Collapse this transcript
Updating queries
00:00The update query is one of four action queries in Access.
00:03Using it we can create a selection of our records that all require a specific
00:06update or a modification to their data and then update all those records
00:09throughout the entire database.
00:11The update query not only breezes through the task, but it will also prevent
00:14any data entry errors that typically crop up when you're manually modifying
00:17large amounts of records.
00:18Before we begin, if you haven't already done so, please take a moment to review
00:22the previous movie on creating a backup of your database.
00:25So let's say that we're thrilled with the recent performance of all of our sales
00:27people and we want to give everybody in the Sales department a $5 raise.
00:31Let's go ahead and create an update query to do that.
00:33We'll go the Create tab and then a New Query Design window.
00:36Let's go ahead and add our employees table and from that, we're going to go
00:41ahead and add Employee ID, First and Last Name.
00:44Let's scroll down and select their HourlyPay and their Department.
00:48For our Department criteria, we're going to specify Sales, to get everybody in
00:52the Sales department.
00:53Now let's go ahead and take a look at our Datasheet View and we'll see that we
00:56have our employee IDs, first and last name, the current rate of pay, and we can
01:01verify that everybody is in the Sales department.
01:03So let's take a look at Inga Burns here.
01:04Currently she is making $12.97.
01:07Keep that number in mind, because we are going to review that in a moment after
01:09we have run our update query.
01:10Let's go back into Design View, and we'll switch this to an update query.
01:15And when we do that, you'll notice that we get a new line down here that says Update To.
01:18So we're going to update our HourlyPay to the HourlyPay+5, and we put that in
01:23with square brackets and then our mathematical expression +5. So throughout this
01:29course I have been using the View button and the Run button interchangeably.
01:32That's because we have been dealing exclusively with select queries.
01:34Writing a select query merely shows the Datasheet View, so these buttons are the same.
01:38But when we're dealing with action queries, such as make table, append, update,
01:42and delete, Run will actually perform those operations.
01:45We can use the Datasheet View of our update query to review the records that
01:48are going to be modified and we can double-check our work.
01:50So we're going to be modifying only the HourlyPay records.
01:53Let's switch back into Design View and go ahead and run our query.
01:56Now you should get a message that says you are about to update 84 row(s).
02:00Go ahead and click Yes to this and Access actually doesn't give us any
02:03indication that the operation was performed, but if we change our query back to
02:07a select query and then take a look to our Datasheet View again, we can see
02:11that everybody's pay went up by $5. Now, if nothing happened when you tried to
02:14run your update query, that might be because you have to adjust some of your
02:17security permissions within Access.
02:19And you can do that by going to the Office button, Access Options, going to the
02:23Trust Center, and then taking a look at our Trust Center Options.
02:27If you go to Trusted Locations, you might have to add your Exercise folder as a
02:30Trusted Location, by going to Add new location, and then browsing to the
02:33location of your Exercise folder.
02:35I've already done that, so I'm going to go ahead and say Cancel and back out of this.
02:38Your data might need to be updated for any number of reasons.
02:41You might change vendors and have to update product ID numbers, or your business
02:45may move, change names, or get merged with another company, and all your
02:48addresses or phone numbers or email addresses might need to get altered.
02:52Whatever the reasons, you'll be able to quickly and easily keep your database
02:54up-to-date with an update query.
Collapse this transcript
Making tables and appending queries
00:00We can use a make table query to permanently save the results of the query as a new table.
00:05And we can use the append query to take records from one table and append
00:08them to another table.
00:09Let's how we can use these to action queries together to create a master email
00:13list for a marketing campaign.
00:14First let's go up to the Create tab and create a new query in Design View.
00:17I am going to add our Customers table to the query and we'll go ahead and close
00:21the Show Table window.
00:22Now we want to add our FirstName and LastName and our customers' email addresses.
00:27Now since all of our customers haven't signed up to receive newsletters from us,
00:30I want to make sure that I include the Newsletter field as well, and we are
00:33going to apply a criteria on that, so you can filter out just the customers who
00:36have agreed to receive our newsletters.
00:38I can go ahead and view the results and I can see that we have a total of 555
00:42customers who've signed up for our newsletter.
00:44Let's go ahead and switch back into our Design View and now we're going to
00:46convert our query into a make table query.
00:48Access is going to ask is for the new table name that we want to save and we'll
00:52call this tbl_EmailCampaign.
00:53Let's go ahead and say OK.
00:56Now for the new table I want to include the first name, the last name and
01:00the email addresses of all our customers that have signed up for our
01:02newsletter, but we don't necessary need to have the Newsletter column in the new table.
01:06So let me go ahead and uncheck the Show box and then I'll click Run.
01:09Access tells us we are going to paste 555 rows into a new table, and that's
01:13exactly the operation that we want to do, so go ahead and say Yes.
01:15And we'll see that we have a new table in our database now, and I can open
01:19it up and we can see that it has just the first and last name and the list
01:22of email addresses.
01:23Let's go ahead and close that table and we're done with this query, so we can go
01:26ahead and close it, and I don't need to save it.
01:28Now the next thing I want to do is take all of our employees and add their
01:31addresses to the EmailCampaign table as well. We can do that with an append query.
01:36Once again we'll go to the Create tab > Query Design. This time I am going to
01:40select our Employees table.
01:41And from there we're going to take FirstName, LastName and Email address.
01:47We can switch to Datasheet View to see that we're getting the results that
01:49we expect, and we can see that we are getting 200 of our employees that all
01:52have email addresses.
01:53One more time we'll go to Design View.
01:55This time we're going to switch it to an append query. Access is going to ask us
01:59which table we want to append to?
02:01And we can either type in the name or use the dropdown list here and
02:03select EmailCampaign.
02:05Go ahead and say OK.
02:07And when we run our query, Access is going to tell us that we are about
02:10to append 200 rows.
02:11That's exactly what we want to do, so go ahead and say Yes.
02:14And I am going to close this query without saving it as well.
02:17Now if we open up our EmailCampaign table, you can see that we now have a total of 755 records.
02:22So that's 555 from our Customers table and 200 from our Employees table.
02:27We have a master list of all the email addresses for the people within our company.
02:30So that was just a couple of examples of the ways that you can use a make table
02:33and an append query to modify data within your database.
Collapse this transcript
Deleting queries
00:00The delete query is obviously the most destructive of the four action queries.
00:04While the other three action query types modify and move records around,
00:08the delete query can actually remove large quantities of data from your database as
00:12if they had never existed.
00:13I'll repeat my word of warning here again.
00:15If you haven't backed up your database, make sure that you do that before
00:18performing any delete queries.
00:19While it's probably the rare exception where you want to permanently remove
00:22records from your database, it does happen.
00:24One of the most common reasons is when you're exporting out data from and rarely access data
00:28from your database to a second archive database.
00:30This way, you can keep only the most relevant and current data within your
00:33working database to keep it running smooth and fast.
00:36The archive database will contain all of your past records that you might need
00:39to review periodically, but not often enough to justify slowing down your
00:43everyday work with unnecessarily large data tables.
00:46So, let's take a look at how we can use the make table and delete queries in
00:49conjunction with each other to create an archive database.
00:51First, I'm going to go up to the Create tab, and a new query in Design View.
00:55Right now, we want to export all of our old orders to an archive database.
00:58So I'm going to add our Orders table to the query and close the Show Table window.
01:01I'm going to add all of the fields from our Orders table.
01:04I can do that by selecting the first one, Shift+Clicking on the last one, and
01:07then drag that whole thing down to the first field.
01:09Access will populate these across.
01:11Now I want to filter it down to just our older orders.
01:14I want to say every order that was placed before 12/31/2006.
01:18So I'll say <=12/31/2006.
01:24I'll switch over to Datasheet View to see that we're getting the results that I expect.
01:27I see that I've got 819 records that are being selected.
01:30Let's go back into our Design View.
01:32We can switch this to a make table query.
01:35Access is going to ask us for the table name.
01:37I'm going to call this table old orders, tbl_OldOrders.
01:44I also have an option to either create this table on the current database like
01:46we did in the previous movie, or I can put it in another database.
01:50I'm going to select that and browse to my Exercise folder.
01:53For me, that's on the Desktop/Exercise Files.
01:56I'll go into Chapter 8.
01:58Here, I've already got an Archive database set up.
02:00So I'm going to select that one and say OK. Select OK again.
02:03Then we'll run our query.
02:05Access is telling us that it's about to paste 819 rows into a new table.
02:09That's what we want.
02:10So I'll go ahead and say OK.
02:11Now we want to get the old orders out of our current database.
02:14That's pretty easy.
02:15We just need to switch to a delete query.
02:16We're going to keep the same parameters, and run the query again.
02:19Now, Access is telling us that we're about to delete 819 rows.
02:23That's what we want again.
02:24So, we'll go ahead and say OK.
02:25I'm going to close our query without saving it.
02:28Now if I take a look at our Orders table, I'll see that I've got 1381 records,
02:32instead of the 2200 that we started with.
02:34Let's go ahead and close our current working database, and review what
02:37happened in the archive.
02:38We'll open that one up.
02:40We've got one table in here called tbl_OldOrders.
02:42If I open that, I find that I have the 819 records that we just exported.
02:47So, that's how you can use a make table action query and combine that with the
02:50delete action query in order to create an archive database.
Collapse this transcript
Conclusion
Goodbye
00:00In this course we covered several of the techniques that are available to you
00:03when using queries to gain better insight into your data.
00:06I hope that you find this information useful as you apply it to your own Access projects.
00:09It's been a pleasure exploring queries in-depth with you.
00:12Have a great day!
Collapse this transcript


Suggested courses to watch next:

Access 2007 Power Shortcuts (3h 25m)
Alicia Katz Pollock


Access 2010 New Features (36m 35s)
Alicia Katz Pollock

Access 2010 Essential Training (3h 30m)
Alicia Katz Pollock


Are you sure you want to delete this bookmark?

cancel

Bookmark this Tutorial

Name

Description

{0} characters left

Tags

Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading
cancel

bookmark this course

{0} characters left Separate tags with a space. Use quotes around multi-word tags. Suggested Tags:
loading

Error:

go to playlists »

Create new playlist

name:
description:
save cancel

You must be a lynda.com member to watch this video.

Every course in the lynda.com library contains free videos that let you assess the quality of our tutorials before you subscribe—just click on the blue links to watch them. Become a member to access all 104,141 instructional videos.

get started learn more

If you are already an active lynda.com member, please log in to access the lynda.com library.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Get access to all lynda.com videos

You are currently signed into your admin account, which doesn't let you view lynda.com videos. For full access to the lynda.com library, log in through iplogin.lynda.com, or sign in through your organization's portal. You may also request a user account by calling 1 1 (888) 335-9632 or emailing us at cs@lynda.com.

Access to lynda.com videos

Your organization has a limited access membership to the lynda.com library that allows access to only a specific, limited selection of courses.

You don't have access to this video.

You're logged in as an account administrator, but your membership is not active.

Contact a Training Solutions Advisor at 1 (888) 335-9632.

How to access this video.

If this course is one of your five classes, then your class currently isn't in session.

If you want to watch this video and it is not part of your class, upgrade your membership for unlimited access to the full library of 2,025 courses anytime, anywhere.

learn more upgrade

You can always watch the free content included in every course.

Questions? Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com.

You don't have access to this video.

You're logged in as an account administrator, but your membership is no longer active. You can still access reports and account information.

To reactivate your account, contact a Training Solutions Advisor at 1 1 (888) 335-9632.

Need help accessing this video?

You can't access this video from your master administrator account.

Call Customer Service at 1 1 (888) 335-9632 or email cs@lynda.com for help accessing this video.

preview image of new course page

Try our new course pages

Explore our redesigned course pages, and tell us about your experience.

If you want to switch back to the old view, change your site preferences from the my account menu.

Try the new pages No, thanks

site feedback

Thanks for signing up.

We’ll send you a confirmation email shortly.


By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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.

By signing up, you’ll receive about four emails per month, including

We’ll only use your email address to send you these mailings.

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

   
submit Lightbox submit clicked