You are on page 1of 11

Page | 1

Access 5: Creating Advanced Queries and Enhancing Table Design


Review: Excel and Access serve two very different purposes: When working with numbers (budgets, calculations,
formulas) Excel is an excellent product. If you are working with non-numeric data (text), a database (Access) is a
much better choice. Even though you can use database functions within a spreadsheet, there is nothing more
efficient than a relational database to store, maintain and track data.

Remember: Excel for crunching numeric data. Access for organizing and analyzing text data

An Access database can contain multiple tables. Each table contains records (groups of related fields) and Fields
(groups of similar information). In addition to tables, a database will contain Queries, Forms, Reports and Macros.
The screenshot below shows a table in design view (the tables metadata is visible).

Database parts:

Tables consist of records (horizontal rows) and Fields (vertical


rows). A field will always contain the same type of data (first
names, last names, and city fields would all hold specific data). A
record is contains data from multiple fields (a person would have a
first name, last name and city etc)

Queries can be used to join related data from multiple tables,


modify data (update, add, delete, append) and answer questions (
filter, sort, etc)

Forms – used for data entry (easier to look at than table or query)

Reports- used for cleaning up and presenting data (easier to look at


than in table or query view)
Table in Datasheet view (selected record in blue, field names in yellow) Page | 2

Table in Design view (field names in yellow, metadata for selected data visible)
Page | 3

Database Naming Convention:


In this chapter, there are four basic types of database objects being used: Tables, Queries, Forms and Reports.
Figure 1 contains an example of the naming convention introduced in the text. In this example, all table objects
begin with a three letter prefix describing the nature of the object (tbl, qry, frm, rpt). The rest of the object name
consists of a brief description of the type of data contained within the object. It’s a good idea not to have spaces in
the object name (it’s easier in the long run so it’s a good habit to start!). If your description includes more than
one word, separate the words by using capital letters.

Here’s the short version:

1. Naming conventions help organize a database. The person maintaining the database (you) will know
where to look for stuff and more importantly, what you’re looking at (tables should begin with tbl.
Queries should begin with qry, Forms should begin with frm and Reports should begin with rpt.)
2. Names should be descriptive (use words!)
3. Don’t use spaces in database object names (use capital letters to help identify individual word.
Due: qryCustomersAndInvoice
Don’t: qry Customers and invoices Why? When referencing, prone to typos
Don’t qrycustomerandinvoice Why? Difficult to read
Why?

By using a logical and organized naming convention, it is quite easy to navigate through the various database
objects (it’s easy to find the query or table required for a specific form or report).

Advanced Pattern Match Queries: Up until now, most of the queries that we have worked with
(CCCL 100) have used the basic comparison operators (<,>,=). While these are good for exact matches or numeric
values, they don’t work very well with data that is in a text format.

The Like format is useful when the search involves a pattern. Suppose you have a database of pets with a field
titled “Animals”. If you wanted to filter the data for only dogs, you could use the like function with a dog filter: Like
“dog”. This would return every record that contain only dog. The problem with this statement is that it would
eliminate any field that had more than just the value of dog. For example: any field that had “big dog” or “little
dog” would not be included in the query (why? The like returns an exact match).

Access allows the use of wildcards to help with this type of situation. The wild card, when used with the like
function, really opens up your search options!

Wildcard Function Example


* Use both before and after the string of text being “*dog*” would return dog, small dog,
sought to accept any number of characters large dogs
before or after the string.
? Any single character “d?g” would return dog, dig, dug
# Any single digit” “200#” would return 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008,
Page | 4

2009
There are times when you’ll want to run a match for multiple criteria in a query (for example, you want to return
those customers living in Adams, North Adams and Savoy). While you could spread the criteria over three different
or lines, it would be easier to write a single line of code to perform the same function. In this example, right click in
the criteria field on the city field and select “Zoom” and a window will pop up that will allow you to enter in all of
your criteria at the same time.

The Zoom dialog allows you to easily view the selection


criteria.
Page | 5

List-of-Value queries use a list to define the query criteria - While the or criteria works when there are only two
items, the lists come in handy when there are more than two items. The List function is used in the screenshot
bellow.

Using the “Not” logical operator will exclude the the items from query results. In the screenshot below, all of the
results execpt for the items in the list.

Autofilter:
Remember using the Autofilter function in Excel? Access is no different. Enough said.
Page | 6

Assigning a conditional value to a Calculated field:


The power of Access is that you can set up the program to make decisions and do stuff for you (Nice!)

In the example they’re using in the book, they create a title field that will conditionally return a result that will
either contain the customer name (first and last name) or the company name (if the customer name doesn’t exist)

The only difference between the IF function in Excel and Access is the spelling (IIF in Access vs IF in Excel), all of
the other arguments are the same.

General format:

FieldName: IIF(conditional test, what to do if true, what to do if false)

Screenshot:

To the right, you can see the IIF statement in


the design view. Note: the first part
(customer) will be used as the field name in the
query results. In the Zoom screenshot, its’
easier to see the IIF statement.
Page | 7

Parameter Querys: The power of this type of query is that it prompts the user for the filter criteria when
the query is run (during run time). To set up a parameter query, use brackets to enclose the prompt to be
displayed. In the examples below, the parameter query as it appears in the design view of a query and as it
appears in the Parameter dialog

Crosstab Queries: Why? When somebody (your boss) what’s to see something grouped by . . . you will need
to use a crosstab (in Excel, it was the crosstab table, in Access, it starts with a crosstab query). For example, a
summary of sales by city would provide an aggregate total of sales by city.
Page | 8

The table (left) contains the various aggregate


functions that may be used when creating a
crosstab query.
Note: the first and the last field value are useful
for returning the last invoice paid (if you don’t
know the date and only want to look at the most
recent).

The screenshot (left) displays the three


parts of the crosstab query:
Row Heading: Items in this category will
be used to group the items down the
left hand side of the crosstab (in this
example the company name). Note: use
of iif statement in company name.
Column Headings: items in this
category will be used to group the items
across the top of the crosstab (in this
example: the status of the invoice)
Value: the item (only one) in this
category will appear as the aggregate
function (in this function: the total
amount paid or unpaid).
In a crosstab query, multiple items may
be used as row headings (although, if
more than two are used, the data can
be rather difficult to interpret the
results. On one Column heading and
one value category may be used in a
crosstab query. The following block
displays the query in an SQL format.
Page | 9

Finding duplicate queries: This is a great tool (select query wizard and select duplicate query from the
option menu). There are times when you may suspect that a set of data contains duplicate values (why? It
happens . . . ). If the dataset is small (20 names), it’s easy to spot the duplicates. If, on the other hand, the dataset
is quite large and complex, it can become quite difficult to find the duplicate records.
There are times when the field of a record will have the same value (just look in the phone book under the last
name of Smith or Jones). A duplicate record would have the same value in multiple fields (for example, first name,
last name and phone number).
When looking for duplicate records, make sure that the values from multiple fields are selected and compared!
Finding Unmatched values: The unmatched queries are useful for isolating inactive records. The query
wizard will walk you right through the process (select new query and click on the unmatched query option to bring
up the wizard, which will walk you right through the steps.)
Again, the value of this tool is based on the criteria used to build the results. Even using the wizard will allow you
to create quite complex unmatched queries.

The Find unmatched query wizard will guide you through the process:
Select the two tables to be compared
Idenfitied the common value between the tables
Select additional fields (provides context for results)
Click finish

Top value queries: This is a


filter that can be applied to a select
query that will return either a fixed
percentage or fixed count (top 10%
or top 10 values). This can be useful
when working with massive amounts
of data. To access this option from
design view, select the Return Option
in the Query Setup group on
Microsoft Ribbon.

Lookup Fields
Bottom line: people mess up data entry. It is amazing how many ways people can mess up a state name . . .
Solution: Lookup fields. With a lookup field, the user selects from a list of preset options –
Page | 10

In the scvreenshot to the right, is a table in


design view. To set up a lookup field, you can
either use the lookup wizzard (very easy) – or
you can set up the lookup value by working
with the metadata.

With the state field selected, the metadata


for the field may be seen in the lower part of
the screen.

Select the lookup tab, and for the sake of this


example,

Set the Display control to “Combo Box”

Set the Rource Source Type to “Value List”

Set the values of the look up field in the row


source (text values seperated by commas)

Lookup options
Page | 11

Inputmask wizard:
The person entering data into
the database will use the
input mask to provide
valuable feedback about the
type of data being entered
into a specific field. Again, the
tutorial in the book will walk
you through the process for
applying an input wizard.

Object dependencies: When


working with multiple tables in a
relational database, it is helpful to see the
ways in which the tables, queries, forms
and reports are linked. This resource may
be accessed by clicking on the object
dependencies icon in the database tools
tab. This will bring up a list of
dependencies for the selected object
(very handy!),

You might also like