0% found this document useful (0 votes)
75 views

2 Access2010 Queries

MS Access Queries
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views

2 Access2010 Queries

MS Access Queries
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Using Microsoft Access

2 - Queries

Using Microsoft Access


USING MICROSOFT ACCESS
Queries Exercise 1. Setting up a Query Exercise 2. Selecting Fields for Query Output Exercise 3. Saving a Query Query Criteria Exercise 4. Adding Criteria to a Select Query Using Multiple Criteria Exercise 5. Using And Criteria Exercise 6. Using Or Criteria Ranges in Criteria Exercise 7. Using an Upper or Lower Limit Exercise 8. Using an Upper and Lower Limit Keywords and Wildcards in a Query Exercise 9. Using Wildcards and Keywords Exercise 10. Criteria Challenge

1
2 3 4 5 6 6 7 7 8 9 9 9 10 10 10

Steve ONeil 2011

Page 1 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Queries
After Tables, Queries are perhaps the most important component in a database. Queries are used to retrieve information from a database. Once again, a telephone directory can be used for an example of a query. When you look up a phone number in a phone directory, you use query techniques. You might begin by looking for names that begin with J. Then you might narrow it down to listings with the last name Johnson. The criteria for your query is Last Name = Johnson. If there are still too many results, you may add additional criteria to narrow down the results. E.g. Initials, Suburb, street address. Eventually, your criteria will narrow down the results until you have the result you are after. When you query a database on a computer, you use the same techniques. A query is a set of rules for finding information in a database. Access queries that retrieve data are called Select Queries. Access also provides other types of queries for automating things like adding, modifying and deleting data. These are referred to as Action Queries. Queries in a database use the Structured Query Language (SQL often pronounced sequel). In Access, however, it is not necessary to learn SQL to create queries, as queries can be created using the much simpler Query by Example (QBE) window. The most common variety of query is the Select query, where the query is used to select certain information from the database according to criteria you specify. Creating a standard select query involves the following steps: 1. Choose which table(s) the information will be coming from (information can also come from existing queries). 2. Choose which fields will be included in the query output. 3. Specify the criteria for the type of query. 4. Run the query / view the results. We are going to use the Student List database created in the previous exercises to practice creating queries so make sure it is open before you commence the following exercises.

Steve ONeil 2011

Page 2 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Exercise 1.

Setting up a Query

1. Click the Create tab on the Ribbon. 2. Click the Query Design icon. 3. Make sure the STUDENTS table is selected in the Show Table window. You can use this window to select more than one table to base your query on as we will do later on but we only have one table in this database.

4. Click Add to add the selected table to the Query window (you can also double-click the name of the table). 5. Close the Show Table window so that you are viewing the main query window.

The top section of the window shows a list of fields in the source table(s). The bottom half, referred to as the QBE Grid (Query by Example), is where you can select the fields and criteria essential to the query results. 6. In the top part of the window, resize the STUDENTS table box so that you can see all of the fields. Depending on your screen resolution, you may also need to adjust how much room the top area takes up by dragging the line between the top and bottom area.

Steve ONeil 2011

Page 3 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Exercise 2.

Selecting Fields for Query Output

Selecting fields to be included in the query results can be done in several different ways. In the following exercises you will learn different ways of doing it. When you become experienced with Access you may decide on a method you prefer to stick with. The bottom half of the query design window is where information about the currently selected fields will be kept. 1. Make sure the first column in the QBE Grid is selected. for the fields names and a drop-down arrow will appear next to selected box. 2. Click the arrow and a list of fields from the STUDENTS table will appear. 3. Select Last Name from the list. The Query results will now show the Last Name field. 4. Click the Datasheet View icon to view the results of the current query. The first row is

You will see a list of all students (since we havent yet specified any criteria) with only the Last Name field showing. 5. Click the Design View icon to return to the Query Design view. Fields can also be dragged to the QBE Grid from the field list in the top section of the window. 6. Click on First Name in the Field List at the top and drag it down to the column next to where you selected the Last Name field (you can also drag it on to a column that already contains a field to insert it before that column). 7. Click the Datasheet View icon to view the results of the current query. You will now see all the students listed with their first name and last name showing. 8. Click the Design View icon to return to the Query Design view. You can select multiple fields from the Field List using the [Shift] and [Ctrl] keys the same way you can select multiple files in Windows Explorer. 9. Click on Date of Birth in the Field List. 10. Hold down [Shift] and click State in the Field list. All the fields in between will be selected. 11. Hold down [Ctrl] and click Mark in the Field List. This field will be added to the selection. 12. Drag any one of the selected fields to the next available column in the bottom section and they will all be added to the QBE Grid. 13. Click the Datasheet View icon to view the results of the current query.

Steve ONeil 2011

Page 4 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access 14. Click the Design View icon to return to the Query Design view.

2 - Queries

The last and perhaps quickest method of selecting fields is double-clicking the field names. First we will clear the existing selection of fields so we can have a fresh start. One way we could do that is to simply close the query without saving and then start a new one. Another way is to do the following. 15. Click the Home tab on the Ribbon. 16. In the Sort & Filter group click Advanced. 17. From the list which appears, click . The QBE Grid at the bottom will now be blank. 18. Double-click on Last Name in the Field List. It will appear in the first available column of the QBE Grid. 19. Double-click to add the following fields. First Name, Date of birth, Address, Suburb, Postcode, State, Phone, Gender, Mark. These fields will be used in the queries that will follow.
Note when you add several fields by double-clicking, you might not have room to see all of them without scrolling. They are being added though so dont double -click again on the same field or that field will appear twice in the query results.

Exercise 3.

Saving a Query

Sometimes a query is used as a temporary way of finding records in your database and wont need to be saved. Other times, Queries may be used on a regular basis, such as a query which will show whatever sales records were recorded in the past week. Also, Queries need to be saved if they are to be used with Forms or Reports (covered later). Saving a Query is much the same as saving a table. 1. Click the Save icon on the toolbar. (or press [Ctrl] [S] )

2. For the query name enter test query as shown above and click OK.
Note In the previous section it was advised to name Tables and Queries differently so it is easy to tell which is which when they are listed together. We are naming Tables in UPPERCASE letters and Queries in lowercase letters. Another common method is to use naming prefixes such as tblCustomers and qrySeptemberSales. Also, it is important to give your queries meaningful names so you know which is which, especially when there are a lot of queries in a database or a lot of people using it. E.g. a query that shows overdue payments should be called something obvious like overdue payments listing.

Steve ONeil 2011

Page 5 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Query Criteria
The next key step in creating a query is to specify what kind of information you want to be included in the query results. The rules you set up for determining what will be included in the results are referred to as Criteria. If you look in the QBE Grid, you will notice that there is a criteria row. To use criteria, you can type an example of your intended result in the criteria row below your intended field (which is why its called Query by Example).

Exercise 4.

Adding Criteria to a Select Query

In this exercise we will use criteria so that the query results will only show records for students where the suburb is Dianella. Any records with a different suburb will not appear in the results. Your test query from the last exercise should still be open. 1. Click in the Criteria row in the Suburb column. 2. Enter Dianella as shown to the right. It doesnt matter if you use uppercase letter for the criteria or not. It makes no difference. 3. Click the Datasheet View icon to view the results of the current query. Your results will only show records matching the criteria. I.e. all records that have Dianella in the Suburb field. 4. Click the Design View icon to return to the Query Design view. You will notice that Access has placed quotation marks around your criteria. Access uses quotation marks to specify that the criteria being used is a text value. When dates are used as criteria the date will be surrounded by a # on each side as you will see later. Numbers are left alone. When entering criteria you dont need to enter these symbols yourself but be careful when editing. If you del ete quotation marks on one side and not the other, you will get an error when you try to view the query results. 5. Change the criteria from Dianella to Yokine. 6. Click the Datasheet View icon to view the results of query again. Now you will only see students from the suburb of Yokine. 7. Click the Design View icon to return to the Query Design view. 8. Delete the criteria in the Suburb field and click to save the query.
Note You can view the results of a select query by clicking either the Datasheet View icon or the Run icon. However, it is best to get in to the habit of using the Datasheet View icon. Firstly because the Design View icon is in exactly the same place so you can easily move between Datasheet and Design view without moving your mouse. Secondly because the Run icon has a very different purpose in action queries and should be used with caution as it can actually change data in those queries.

Steve ONeil 2011

Page 6 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Using Multiple Criteria


You can use more than one criterion in a query to either narrow down the results or expand on the results. You can narrow down the results using and criteria and increase the results by using or criteria.

Exercise 5.

Using And Criteria

And criteria work by using more than one criterion in different fields. This has the effect of reducing the number of results. In the last page, you used criteria in the Suburb field to limit the results to only six records. If you decided to also specify that you only wanted results for female students you would reduce the results even further. I.e. you would only see results where the record is in a certain suburb and a certain gender. 1. In the Suburb field, enter Dianella for the criteria. 2. Scroll to the right until you can see the Gender field. 3. Enter Female for the criteria in the Gender field.

4. Click the Datasheet View icon to view the results of query. You will only see records where the record has Dianella for the Suburb field and Female for the Gender field. The additional criterion has reduced the number of results from six records to two records. Return to Design View when done.

Steve ONeil 2011

Page 7 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Exercise 6.

Using Or Criteria

Or criterion allow you to increase the results by having more than one criterion for the same field. For example, if you wanted to see all of the records where the suburb was Yokine, Bedford or Morley, you would get more results than if you only had one of those suburbs as the criteria. In other words, the results would show all records that are Yokine or Bedford or Morley. There are two ways of using or criteria. The first is to use the or operator as part of your criteria as in the following steps. 1. Delete any existing criterion in your query. 2. In the criteria row for the Suburb field enter Yokine or Bedford or Morley. 3. Click the Datasheet View icon to view the results of query. You will see records that have any one of those suburbs in the Suburb field. Return to Design View when done. You can also use Or Criterion by placing each criterion on a different row. You will notice that under the Criteria row is an Or row which works as an or operator. All of the ones below it are also rows used for Or criteria so you can have as many criteria as you like in a single field. 4. Delete the criteria in the Suburb field. 5. Enter the same suburbs one under the other as shown below.

6. Click the Datasheet View icon to view the results of query. You should get the same results as before. Return to Design View when done. Although the results were the same in this instance that is not always the case, particularly if there are criteria in other fields. Consider the following two examples.

In the first example, the results will show records for Male students in Yokine or Bedford. In the second example, the results will show Male students in Yokine and all students from Bedford. This is because in the second example, the Gender field criteria only applies to the suburb on the same line. If you wanted both Suburbs to only show Male students you would need to modify the query as shown below.

Steve ONeil 2011

Page 8 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Ranges in Criteria
Instead of using specific criteria, you can specify that you want the results to find all records within a certain range.

Exercise 7.

Using an Upper or Lower Limit

Logical Operators can be used to set a logical limit on the range or results. If you want to specify a Less than criteria you can use the < symbol. To specify a Greater than criteria, use the >

symbol. 1. Clear any existing criteria from your query. 2. Click in the Date of Birth criteria row. 3. For the criteria, enter <1/7/95. 4. Click the Datasheet View icon to view the results of query. You will see all students with birth dates before 1/7/95. Return to the Design view. 5. Delete the criteria in the Date Of Birth field. 6. Scroll right to the Mark field and enter >70 for the criteria. 7. Click the Datasheet View icon to view the results of query. You will see all students with a mark higher than 70. Return to the Design View. 8. Delete the criteria in the Mark field. 9. Scroll left to the Last Name field and enter <M for the criteria. 10. Click the Datasheet View icon to view the results of query. You will see all students a last name staring with a letter that comes before M in the alphabet. 11. Return to the Design View. You can see from these examples that range criterion work equally well with numbers, dates and text. 12. Try the following criteria in your query. Remember to clear each one before trying the next one.
Field Date Mark Mark First Name Criteria >=1/9/95 <54 <=54 >=M Expected Result Dates greater than or equal to 1/9/95 Marks less than but not including 54 Marks less than and including 54 First names beginning with the letter M or later in the alphabet.

Exercise 8.

Using an Upper and Lower Limit

You can use range criteria to specify an upper and lower range so that records between those values will appear in the results. 1. 2. 3. 4. 5. Clear any existing criteria from your query. In the Mark field, enter >=50 And <80 for the criteria and view the results of the query. Your results will show all records for students with a mark that is 50 or more, but less than 80. Clear the Mark field criteria. In the Date of Birth field, enter >=1/3/95 and <=30/9/95 for the criteria and view the results of the query. 6. Your results will show all students with birthdates between the dates specified (and including those dates).

Steve ONeil 2011

Page 9 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

Keywords and Wildcards in a Query


There are several shortcuts that can be used in query criteria. The first of these are Wildcards. You may be familiar with wildcards from other computer applications. They take their name from Wildcards used in card games where a certain card can take on the value of any other card. The first wildcard is the ? symbol. This can be used as a substitute for any letter in a criteria (similar to the blank piece in a game of Scrabble). The second and more commonly used wildcard is the * symbol. This can be used in place of any number of letters. The following examples show how it might be used. Sm* *ville *smith* All values starting with Sm E.g. Smith or Smyth All values ending with ville E.g. Smallville or Townseville All values containing smith E.g. Johnsmith, Smith or Smithson

Certain keywords can also be useful in query criteria. The Not keyword can be used with other criteria to specify that you want all records that dont match the criteria. The Null keyword can be used to specify that you want all records that are blank in the specified field. The following examples show how these might be used. Not Bedford Null Not Null All entries except ones for Bedford All entries that have nothing in the field All entries except ones that have nothing in the field

Exercise 9.

Using Wildcards and Keywords

1. Try the following criteria in your query. Remember to clear each one before trying the next one (Hint - you will need to add the Comment field to the QBE Grid first).
Field Last Name Comment Comment Suburb Criteria C* Null Not Null Not dianella Expected Result Last names beginning with the letter C All students without a comment All students with a comment All students who dont live in Dianella

Exercise 10.

Criteria Challenge

1. See if you can create criteria to get the following results. Some of them are a bit tricky. All male students with a mark of at least 50. All students that live on a road (such as Walter Rd). All students with a last name ending in ns. Students with a birthdate from 1st May 1995 to the end of that year. Female students who got a mark of less than 60. All students not born in Winter 1995.

2. When you are done, clear all criteria and close the query without saving changes.

Steve ONeil 2011

Page 10 of 11

http://www.oneil.com.au/pc/

Using Microsoft Access

2 - Queries

If you get stuck, check the following for ideas. Some of them have more than one way of getting the correct result.

Notice you can widen the columns if needed. Try it out.

Tip

have a hard time remembering which symbol is greater than and which one is less than? Less than starts with the letter L, which is what the < symbol looks a little bit like if you look at it on an angle.

Steve ONeil 2011

Page 11 of 11

http://www.oneil.com.au/pc/

You might also like