Creating Queries - Different Types K 18

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

INFORMATION TECHNOLOGY

TYPES OF QUERIES ( Access 2007+)

These include Select, Totals or Summary, Make Table, Update, Append and Delete queries.

a) A Select query allows you to select fields/ columns from 1 or more tables and show and save the results as a
separate object. Criteria and new calculated fields with formulas may be added. The results come out in table form.

b) A Summary or Totals query. is used to finds summary information for 1 or more columns. Eg the sum, average
or maximum of a column. It is a select query with the Totals icon ∑ turned on. The results will show up as 1
row table only.

c) A Make Table query allows you to select columns from 1 or more tables and create a brand new table.
d) An Update Query makes changes to an existing table. Can be used to add data to an empty column.
e) An Append query adds the contents of another table with similar structure to the selected table. Whole rows or just
specified fields from these rows may be added. All or some rows may be appended.
f) A Delete query deletes rows that meet the criteria you specified.

CREATING QUERIES

To create any query in Design View, you click on the Create Tab, then click the Query Design icon. The Show
table appears. Select the tables you want to use ( 1 by 1) then click Add. Close Show Table when through. The
Design tab ribbon, seen below, shows up, allowing you to choose a query type. . (ie Totals, Make Table, Append,
Update, Delete and Crosstab query). The default is Select Query

When the query is saved the name in the query list has the query type icon next to it. So a query name with a cross and
an exclamation mark next to it represents an Append query.

1) Select Queries – These are used to extract information from tables. You select the desired fields then add criteria
and formulas if required. The results are shown in a separate file or listing. They do not affect the original table.
The query design below will show all students from the table who are not 12 years old. Students will be listed in
ascending order of Age.
Field Name Age
Table Student Student
Sort   Ascending
Show √ √
Criteri
a   <>12
USING FORMULAS TO GET CALCULATED FIELDS.
In the 1st cell of the first empty column of the design grid, type a name for your new field. Type a colon. Type
the formula needed. If you use field names you must put each one in its own pair of square brackets.

Field Name Salary Tax: [Salary] * 10/100 Net Salary: [Salary] – [Tax]
Table Staff Staff
Criteri
a    
Show √ √ √ √

2) Summary Select Query


This is a select query with the si gma symbol (), turned on in the toolbar. (In picture above , see far right.)
This adds a row called Total to your design query. This row can be used to find the Sum, Average, Max, Min,
Count, First, Last of any column using all the records in a table or calculating by groups of records( if Group By
is selected first ).

In addition to Sum, Average etc, this row has a Where option, used to add criteria to a field which already has a
function .

Field Name Donation


Total Count Sum
Criteri
a    
Query 1
Query 2
The first query design counts all names and sums all their donations.
Query 2 groups the data by Gender (Male and female). For each group it counts and sums up the donations of
persons who gave $100 or more. Notice Donation has the Sum function so another Donation column is needed to
add criteria. However the show box for the Where column must be left blank.

The results will show up on 1 row table only. If the data in a column is grouped then the results will have 1 row per
group. See tables below.

CountOfName SumOfDonation
18 $ 54,600.00

NOTE ON ERRORS.

 If you get no results for your query check your criteria or links. It could be that no rows match the criteria, or the
criteria has a spelling error. Or the tables may be linked by the wrong fields or by more than one field. A field
involved may have no data or the correctly linked fields do not have matching data. Eg the IDNo field in 1 table
has different id numbers to those in the IDNO field of the other table.
 If you get repeated answers it is because you did not link your tables in the query design. Show only tables you
need.
 If you get Enter Parameter Value it means a field name in your formula or criteria is spelt wrong or the square
brackets are not placed correctly.
 If you get a message saying that a field name can refer to more than one table, it means you entered a table
twice in the top of the design grid. Scroll left or right to see all your tables (aka field list). Delete any extra or
duplicates.
 If you get an extra field called something like Expr1001, it means you selected to a column twice and this is the
repeated column.

3) Make Table Query


Allows you to select columns from 1 or more tables to create a new and separate table. In a new query design
select the columns you want to be in the new table. Then click the Make Table button. A box opens up for you to
type the name for the new table. Click the view button . It shows what the table looks like but you must hit the
RUN button to create the table. See pic below.

ACTION QUERIES - These are Update, Delete and Append queries. These queries change the contents of a table
rather than give a separate result. It is therefore advisable to make a backup copy of your original table before doing
these queries, should there be any error. The changes are sometimes irreversible.

Where as the View button will show results for select queries it will not do so for Action queries. It will show what
will change but not the change. You must click the RUN icon to get results from an Action query. You will have to
open the original table to see the changes. If the table is already open you will have to close it and reopen it

Access 2007 and up, tend to block you from running those type of queries. (See the security warning in pic above.
(Certain content in the database has been disabled) ). Before you run your 1st action query, click Options next to the
warning and click Enable this content.
When you run a query you will see a message box saying:

You are about to ______ number of records …. Click OK.

4) Update Queries
These are used to make changes to columns or rows in the original table. No extra listing will be saved. When
you click the Update button an Update To row is added to the query grid.
Select the field to update. In the Update To section type the formula, word or number you want applied. Column
names used in the Update To box must be inside square brackets eg [Price ]. Choose also all columns which need
criteria and complete them.
The design below shows that the Maths marks will increase by 20 for all students who are females and the word
yes will be placed in the Finalised field for each of them. Be careful. If you put +20 in the Maths update section,
all female students marks will change to 20 and their original scores lost forever unless you have a backup table.

You may also get a type mismatch error if the criteria or the Update To contents is not the same type as what’s
in the column. Eg. If in the table you forgot to change Maths from type text to number then [Maths] + 20 will give
this error.

Field Maths Gender Finalised


Table Student List Student List Student List
Update To [Maths] + 20   “Yes”
Criteria   Female
Or    

5) Delete Query
This is used to delete whole rows fitting a certain criteria. Click the Delete button in the toolbar or ribbon. Select
the fields for the criteria by double clicking the field names. Where automatically appears below the table name.
Add criteria . Eg. In the design grid below, rows with both address as Micoud and Gender as Male will be
deleted. Both Micoud and Male must be present in the same row for that row to be deleted.

Deleting rows may pose a problem if you have a relationship connecting that table with another. You may
sometimes have to first delete the link in the relationship then run the delete query. Reattach the link in the
relationship after.

Field Address Gender


Table Personal Info Personal Info
Delete Where Where
Criteria "Micoud" Male

6) Append Query. To Join 2 tables


When you have a table you want to add to another, you can use an Append Query to
a) join the whole table to the other table, or to
b) join specific rows or fields of the table to the other table depending on the criteria specified.

The corresponding fields need not have the same titles but must have the same type of data and the field size must
not exceed that of the receiving table.
Eg. The field entitled Student from 1 table can be added to the field entitled Name in a similar table.

Method
The table to come in must be open. Create a query design. Add the incoming table, then close Show Table
Double click the fields you want to add. Add criteria if necessary.

To add all fields (ie. whole records) double click the table’s asterisk or choose the tablename.* from the drop
down list in the first column of the design grid. If you do this, then you can add other columns for criteria but
make sure these show boxes are left blank and the asterisk one is ticked.

Click the Append Button. A box comes up from which you select the receiving table ( ie. table to append to).

Note above . the IDNo, Names and Address from the table Pers info to add will be appended to the table Personal
info.
Running the query will copy the information to Personal Info. Save the query. Note that Join query listed on the left in
the picture is an append query. The icon next to it tells you. (a plus sign with an exclamation mark)

Relationships
To avoid linking tables every time you perform a query with 2 or more tables you should create a relationship. This
will also enable you to create a report with 2 or more tables when you need to. Click the Database Tools tab then
click Relationships. Click Show Table if not open and add all the tables needed. Link them correctly. Click Create
after each link. Click Save & Close.

Note: The fields chosen to link 2 tables must have the same kind of data even if the column names are not the same.
Eg Worker can link with Names if both columns have names of workers. Staff ID may be a short code for each
employee and cannot link with Names or Worker . There can only be 1 link between 2 tables though several tables may
link to 1 table. Primary key in at least one table is

Criteria-
Criteria must be placed in the field column which describe it. Eg. A query to “show the Names and Ages of
all children living at the Morne” must have a column which Morne can be part of.  an Address column
must be included in the design. Morne cannot go under Name or Age.

Criteria / Or rows –
Criteria row only - 2 or more criteria on the Criteria line means all must be true for a record to be part of
the result.
Criteria on the Or line means either all those on the criteria line must be true OR all those on the OR line
must be true. All on both lines may be true.

Two or more criteria on the same field – Use 2 columns or more but with only one show box ticked or use
1 column but separate the criteria with the words AND or OR
For OR you can also use the OR line below the criteria line. See Address below.
In both tables the child must be older than 14 but less than 17 and must live either on the Morne or at La
Toc.

Table 1 Table 2
Field Name Address Age Age Age Address
Table Class2B Class2B Class2B Class2B Class2B Class2B
Sort Ascending
Show     
Criteria “Morne” >14 <17 >14 AND <17 “Morne” OR “La Toc”
Or “La Toc ”

Trouble Shooting Queries


Error message – if you’re in design view and you get an error message that does not go away click OK then
UNDO.

If a query gives you lots of duplicated records then you may have more tables than you need or your tables
are not joined. Join them in the correct order.

SQL errors – If you get an SQL error about ambiguous joins then you need to delete the joins in your design
view and rejoin them in the correct order.

Table cannot be opened for modification OR Table in use by another user. Make sure all other queries,
forms or reports are closed especially those using the table.

NB. In some cases a query may not run unless the table is closed.
Enter Parameter – You spelt a table or fieldname wrong.
Type mismatch – you entered text criteria in a number field or a formula in a text field or
the fields you joined in tables are not the same type or size. Correct this in the tables’ Design View.

You might also like