Sie sind auf Seite 1von 9

F -X C h a n ge F -X C h a n ge

PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

Previous Contents Next

Lesson 7 for Tuesday, January 05, 2010

Creating simple Queries

For more information and spec ial deals


related to any of the issues on this page,
place your cursor over the double-
underlined links.
All information supplied by Kontera.c om.

Finding answers to questions

Now, let's say we've created the Movies table and, using the appropriate Form, we've input the movies
that we c arry.

Hopefully we have thousands of entries in that table to give our customers the greatest choice possible.

And, if you look at the example below you will note that we usually have several copies of each movie in
stoc k. Eac h VHS tape or DVD c arries a different item number but the rest of the information has simply
been copied and pasted from one to the other.

www.profsr.com/access/accless07.htm 1/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

We'll use the Movies table to look at Queries.

A query is a question, a search, an interrogation. In Access it is an object of the


database. A query is a stored question. Not the answer, mind you, just the question.

If a customer comes in and wants to know what we have starring Tom Cruise or direc ted
by Spielberg or released in 1995, all those are queries. As long as the information is stored
in the database we can get it.

We will also have queries that are repeated all the time and we will have to take care of
those also:
"What were the sales figures for last month, for last year?"
"How many customers do we have and what is their average age? Are there more men or
women?"

The simplest query would return a list of a whole table. But we rarely need to see all the
columns in a table.

The first thing we do when designing a query is to determine what columns or fields we
want to see. Usually just enough to desc ribe eac h record that will display.

"Show me all the directors we have."


I could show just the director's name but that wouldn't be all that useful. I decide that I'll
also show the name of the movie and the year it was made.

www.profsr.com/access/accless07.htm 2/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

www.profsr.com/access/accless07.htm 3/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

The example above brings up an interesting problem - if I have 25 copies of a movie by a


director, whenever I use a query on director, or star, or title for that matter, the same
movie will come up 25 times. What I actually meant was: show me all the unique movies
by a director, not the tapes or DVD's.

There is a simple way to correct that and that's to use the Properties of the Query, as
shown below:

www.profsr.com/access/accless07.htm 4/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

I c an now save the Query so that if the question ever c omes up again I only have to go
to the Query objects and open the query that I had saved.

I'll give it a name that will remind me of what it does, not just Query1.

Just to remind you again: the query results are not saved here. Only the structure of the
query is saved. If we add a bunch of new movies before we run the query again, all those
movies, if they apply, will show up next time.

Specifying criteria
You may have noticed in all the pics above that the title bar always refers to "Select
Query". That means that we are selec ting something. In fact, we usually select some
records from the table. We rarely have to see all the movies, or all the customers.

To select some records we will use the Criteria line in the query to specify a condition
for a record to be included in the result.

For example: Show me all Tom Cruise movies in stock. Note: in all these I will use the
Unique Values property so that I don't get 4 or 5 repetitions of the same movie.

www.profsr.com/access/accless07.htm 5/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

To define criteria we use comparison operators. These are the signs most people are
familiar with from programming, and there are a few new ones.

= equal

> greater than

< less than

greater or
>=
equal

<= less or equal

<> not equal

reverse
not
condition

like

between

www.profsr.com/access/accless07.htm 6/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
Examples:
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k
All movies with a value of $25 or more.
m_Cost >= 25

All movies produced before 1990.


m_YearProduced < "1990"

All movies released after 1995.


m_VideoRelease > #1995-12-31#

Access is very flexible. However you should get into the habit of using punctuation
correctly.
Usually, text fields, or strings, are enclosed in quotes: "Tom Cruise", dates are enclosed in
number signs: #2000-01-01# and numbers are enc losed in nothing.

The between operator is used like it's pronounced:


m_YearProduced between 1960 and 1970
it is inclusive in that 1960 and 1970 both appear in the result

The like operator is very useful and is used extensively when working with text fields.
It is used in combination with the * , the wildcard character.

Show all stars with a name beginning with Tom.


m_Star like "Tom*"
* stands for "any string", so that the question becomes "Tom followed by something"

Show all movie titles containing the word "wind".


m_Title like "*wind*"
m_Title is "any string, wind, any string"
Of course this will return "Gone with the Wind" along with any title containing "window" or
"winding", etc.

Show all movies starring somebody not named Tom.


m_Star not like "tom*"

If there are criteria in 2 columns, both have to be true. This is an AND condition.

What movies have Spielberg and Tom Hanks done together?

www.profsr.com/access/accless07.htm 7/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

To get an OR condition, use the OR line after criteria.

Show movies starring Vivien Leigh or Tom Cruise.

www.profsr.com/access/accless07.htm 8/9
F -X C h a n ge F -X C h a n ge
PD PD
1/5/2010 Develop Business applications with Micr…

!
W

W
O

O
N

N
y

y
bu

bu
to

to
k

k
lic

lic
Next week: More complex Queries. See you then!
C

C
w

w
m

m
w w
w

w
o

o
.d o .c .d o .c
c u -tr a c k c u -tr a c k

www.profsr.com/access/accless07.htm 9/9

Das könnte Ihnen auch gefallen