Vous êtes sur la page 1sur 59

Lesson 1 Introduction

Basic concepts
This tutorial is on Application Development with Microsoft Access. Let's define what we will be doing: Application: a series of programs or computer codes that execute tasks that a user wants done. Maintaining a list of your friends' addresses and phone numbers is a personal application. Producing the weekly payroll for 1 employees of a business is a commercial application.

!unning a computer model to forecast tomorrow's weather is a scientific application. "e will look at commercial applications only. "e'll lea#e the scientific stuff to the people at M.$.T.

Development:

design%

create%

make%

build

all

the

parts

of

the

application.

&naly'e: talk to the client (the user) to find*out what he wants. $f you don't ha#e a user handy% use your imagination and prior knowledge to guess what a user would want. +esign: create a model of the system. & model is like a blueprint to a builder. $t is a drawing or a description of some kind of what the system will look like when finished. "hen you determine how things will work and how problems will be sol#ed. ,ou do that before you start to write computer code. -reate: write the application using the software * &ccess% in this case. &nd while you're creating you're also testing and debugging to make sure that what you create works the way it's supposed to work. Microsoft Access: &ccess is part of Microsoft .ffice . /owe#er% it is not included in the basic suite with "ord and 0xcel. ,ou need to get Microsoft Office Professional dition to ha#e &ccess. .b#iously% if you intend to do this tutorial we ha#e to assume that you ha#e access to &ccess. $t really is impossible to do otherwise. "e are using &ccess 1 . ,ou could do 2ust as well with &ccess 34. &ny application created in &ccess 1 can be con#erted to the pre#ious #ersion of &ccess and any application created in &ccess 34 can be used in &ccess 1 . The sample applications are all in &ccess 1 .

Lesson ! for

"aturda#$ April !%$ !&&'

Desi(nin( the application

)he *+ideo "tore*


To illustrate how to use &ccess in a commercial application we'll use a business that almost e#eryone is somewhat familiar with: the local #ideo rental store. Let's say that the store% Mi,e-s +ideo% is going to open for business in a few weeks and the owner% Mike% wants to ha#e a database application ready to go for opening day. ,ou% the Anal#st% will sit down with Mike and you will 5uestion him on what he wants to get from the computer application. Then you will draw the plans for the application% which we call the model% and you will check with him again to make sure you ha#en't forgotten anything. .nly then will you actually start to write the application in &ccess.

Definin( the application


"hy does Mike want a database in the first place6

There are actually two main reasons. 1. This is rather ob#ious: he is going to be renting thousands of mo#ies to thousands of customers. There has to be a system in place to track who has what mo#ie% when it was rented% when it was returned% if it was late% if it was lost% who to call to get it back% etc. 1. To succeed in business you ha#e to anal#.e your business: "ho are your customers * men6 women6 old6 young6 "hat are they renting6 "hat's selling and what isn't6 "hat do you ha#e on the shel#es that is gathering dust6 "hat are they asking for6 7o% a well/desi(ned database application will meet both those re5uirements. $t will do the routine sales management and% it will allow the user to do all the sales analysis he needs to do to make the business prosper. ,ou ha#e to keep both of those basic needs in mind when you work on the design.

0ommercial re1uirements desi(n


This is the part where we identify what has to be done to make the application perform all the commercial functions it has to ha#e. 8irst% a word of warning: 7ince you're 2ust beginning with this% we'll keep the exercice simple. "e know that there are many functions that you

can do in the #ideo store: rent +9+s% rent 9/7 mo#ies% rent games% buy pre#iously*#iewed mo#ies% buy popcorn% chips and cola% rent machines% etc. "e won't co#er all of those. "hich is what you should be doing when you do it for real: design the core application and get it working then% add other functions to it. .ur core application is to track the rentals of mo#ies% +9+s and games. "e'll lea#e the popcorn and Pepsi for another session. The first thing you will disco#er is that there are 1 entities that you are working with. &n entit# is something you keep data on% an ob2ect that acts on other ob2ects. $n this application they are: 0ustomers and Movies. "e'll use the term :Mo#ies: to describe our products e#en if they are +9+s or games or whate#er. ;ow% take out your pencil and paper and make a list of all the data% we call them fields% that you ha#e to keep for each entity. ,ou should get something like

this:

"hy do you need the customer's +.<6 $f you want to analy'e your sales by a(e you ha#e to ha#e it. &lso% maybe you'll want to send your best customers a card on their birthday. $t would be a nice touch= 7ame with sex. ,ou need it to analy'e by gender and maybe orient your publicity towards certain target groups. This assumes that you can get the information. "hen you ask the customer originally to fill*in a membership form you will ask for that information. $f the customer refuses to gi#e it% that's .>. ,ou don't make a federal case out of it. <ut 3?@ of people will gi#e you what you ask for on the form. 8or the mo#ies% you keep what might be important if it's not too much trouble to obtain it. Maybe customers will want to know what mo#ies starring :Tom -ruise: you ha#e. .r 7pielberg mo#ies. Maybe there is a 8ilm 7tudies program at a college nearby and they will want to know which /itchcock mo#ies from the 13A s you carry. The idea of "ales Anal#sis is that if you know your customers% you know their needs and you gi#e them what they want% they'll come back as customers. The more customers you ha#e% the more money you make. 7imple% isn't it= 7o% the database is designed to store as much description of the customer and of the product as possible. "e can then use that information to build customer profiles and to track daily or weekly

or monthly sales and identify patterns. &s soon as something is starting to go off*track% the manager can take correcti#e action. This is 2ust the beginning in your career path to bigger and better databases. !ight now on the market there are databases containing many terab#tes of information (a terabyte B 1% gigs). There are applications called Data warehousin( and Data minin( that dig through those mountains of data looking for shopping trends% customer buying patterns% etc. This is going to be <$C=

)echnical re1uirements desi(n


;ow we look at what we should put in so that the application works as smoothly as possible with the computer * speed% error*checking% flexibility for future growth% standard design practices.

/ere are the things that you ha#e to identify: 1. The primar# ,e# for each table: the field that will identify each indi#idual in a uni5ue way. -ustomers: 8irst name6 ;o * there are thousands with the same 8irst name. 7ame with Last ;ame. &nd -ity% and 7tate% etc. Phone number might be a candidate but% you may ha#e 1 memberships in one family with the same phone. 7o we'll create a new field. "e'll assign a uni5ue $+ to each customer when he registers. 7ince the $+ doesn't ha#e to be anything in particular we'll make it autonumber% meaning that the first customer will automatically get $+B1% the second will be $+B1% etc. Then we do the same for Mo#ies.

The prime directive for database design:

ver# table must have a primar# ,e#.

1. Lists: any field that contains a limited list of items. .b#iously not 8irst name or Last name. <ut "tate contains a list of ? items. $dentifying it as a List will help us down the line. $t will a#oid errors: with a list you select from the list instead of keying*in the stateD if you want Maine% you select :M0: and you don't risk entering :M&: by mistake. &nd% if items are added to the list% it can be done easily. That probably won't happen too often with 7tate (maybe -anada% eh=) but it will be more fre5uent with other fields. .ther List fields will include: 7ex% -redit card name% Mo#ie -ategory% !ating% Language. The rule is: an# field that can be a list$ should be. E. Default values: the most common #alue for a field. $f you know that 4 @ of customers are from 7pringfield% make the default for -ity B :7pringfield:. "hen you come to enter -ity for the customer% 4 @ of the time you will 2ust do 0nter to accept the default #alue. $t helps cut down on mistakes.

F. 2amin( convention: a standard format for field names in your application. 8or example% $ use the first letter of the table name as a prefix for field name: cGcust$+ for customer $+% cG8name for customer first name. The reason is that e#entually% when you get into many tables% you will run into duplicate field names. $f you create a 7upplier table% the 7upplier may also ha#e a 8irst name. "hen you program the application it will be a lot easier if you can tell one from another at a glance: c34name will be -ustomerG8irstGname and s34name will be 7upplierG8irstGname. /ere% all -ustomers fields start with c3 and all Mo#ies fields start with m3.

Lesson 5 for

"aturda#$ April !%$ !&&'

0reatin( the database

8or more information and special deals related to any of the issues on this page% place your cursor o#er the double*underlined links. &ll information supplied by >ontera.com.

)he new *Mi,e+ideo* database


To your keyboards. ")A6) A00 ""7

+o you want to open an existing database or create a new one. $f this is your first session% you want to create a new database. &nd% because you want to learn something from the ground up% you won-t use the wi.ards$ pages and pro2ects which are the database*creation templates. .nce you'#e mastered the concept% then you can use the shortcuts and let &ccess guide you through the process.

&nd

again%

to

make

it

interesting%

2ust

choose

Ceneral

+atabase.

-hoose

name

for

the

database

and

store

it

in

folder.

$n &ccess% a database% no matter how many tables or forms or reports it contains% is stored in a single file. That file has the extension: .mdb (for Microsoft database). "hen you want to mo#e or copy or delete the database% all you ha#e to do is work with the one file.

7o The first

far% ob2ect

you you create

ha#e to define

an the

empty database is the

database. table.

& table is the description of an entity. $t is used to store the data. "hen you create the table you first describe its structure. & table in &ccess is sort of like a spreadsheet in 0xcel: it consists of rows and columns. .nly% it is more organi'ed than a spreadsheet and that organi'ation will later allow you to retrie#e the data in all kinds of ways.

The

desi(n

view

of

the

table

is

meant

to

define

the

structure.

,ou name all the columns (in technical 2argon they would be called attributes of the entit#). !emember to use the prefix for the table% in this case c3 because it's the 0ustomers table.

Then%

choose

Data

)#pe

for

each

column.

The +ata Type defines what kind of data will be contained: text% numbers% H!L% dates% etc. This lets the system know how much memory to reser#e and also% what you are allowed to do to the data. 8or example% if you specify that a column is a :;umber: type% you won't be allowed to put :I,J: in it. To see the data types a#ailable% click on the +ata Type column and do 81 (the /elp function). /ere's what you should get:

&nd here are the basic rules on how to use them: "hen you define a primar# ,e# field and it doesn't matter what the format is% use Auto2umber. $f the identifier is formatted% like &3*11EF% use )e8t. "hen the data is a date of any kind% +ate of birth% +ate hired% $n#oice date% etc.% you ha#e to use the Date9)ime data type. Most fields will be )e8t. 0#en a phone number (it contains a dash)% or department number that happens to be 1 1. Hse 2umber only for fields that will be used in calculations (K * L M ) like 5uantities or salaries. $f it does not ha#e to be calculated% use )e8t. 0#en if the +epartmentGnumber is 1 1% define it as text: it's not the number one hundred and one% it's the characters :1:: ::1:. <elie#e me% it will make your life easier. Hse Memo when the Text field may be too big. $f you are doing a Patients table for a +octor% the Dia(nosis field should be Memo. "hene#er a field has only a yesMno answer% use the :es92o type. 8or example: Paid6% Member6% &cti#e6

<efore starting to enter data% you should define the data formats in the ;indows 0ontrol Panel. &ccess gets its formatting information from "indows. $f you want dates to automatically display as ,,,,*MM*++% as you should% you set that property in "indows. The same for currency and number formats. +ifferent countries ha#e different ways of displaying those types so% you set them for your country in the "indows en#ironment.

The Properties at the bottom will usually be acceptable for the data type you selected. <ut% to simplify things later there is one property you should indicate: 0aption.

The -aption is the name that will show for the column on forms and reports and so on. $f there is no caption% the 8ield ;ame is used. 7o% if you want forms to show :-ustomer $+: instead of cGcust$+% define the caption.

;ow% complete the Table% as defined in the model from Lesson 1% adding all the fields re5uired. <efore you sa#e the Table% make sure the Primar# ,e# is identified. To do that% click on the grey button to the left of cG-ust$+ and that should select the whole row. Then click on the ,e# in the toolbar. That should put a little key symbol on the grey button% identifying that field as the Primary key. 7a#e the table and% if this is the first 7a#e% it will ask you to name it (don't keep Table1 as a name=).

8inally% repeat the whole process with the Movies table and you will be well on your way.

Lesson < for

"aturda#$ April !%$ !&&'

0reatin( relationships

Identif#in( the tables


&fter Lesson E% these are the tables you should ha#e.

;ote that there are se#eral fields in both tables that refer to Links to other table. The reason for that is that !elationships are #ery powerful in database design. Let's look at the case of credit cards in the -ustomer table. $f you ha#e to keep a record of the customer's credit card name% number and expiry date you will create E fields in the table. .b#iously% card number and date will be indi#idual but% you may ha#e 1% customers with 9isa. "hen you enter the customer's record you could 2ust type :9isa: for the card name. <ut what if the person keying the data keys :9i'a: instead% or : 9isa:% or :9issa:% or ...... $f% later on you ha#e to ha#e a list of all customers with a :9isa: card% all those entered incorrectly won't show*up. &nd what if 9isa decided to change its name to 9i'a-ard6 ,ou would ha#e to go through the records and change 1% customers.

The solution to those problems is: don't key the card name into the record% select the name from a list% and the list will come from another table. $n this case% we create a -redit-ard table and it will contain only a list of credit cards that we will accept. The customer record won't actually contain the credit card name% it will contain a pointer to the -redit-ard table. $f e#er a card company changes its name% we only need to change the name in the -redit-ard table and all customers who point to that name will automatically be changed.

Relationships and data types One crucial point you have to keep in mind when describing relationships: the link is always from the primary key of one table to a field in another table. The primary key can be any type field - in CreditCards it's a Text !"# field. The other end of the relationship must contain a field of the same type and size. $o% in the Customers table% c&Card'ame must also be a Text !"# field. (n the other tables we'll look at in a minute% the primary key is defined as an )uto'umber field. )uto'umber is not a type% it's a function. The data type is Number and the Length is Long integer. (n those relationships the other end of the link must also be defined as a 'umber field. (n all relationships involving an )uto'umber% the other field must be a Number. (n the *anguage table l&'o is )uto'umber so% m&*anguage in +ovies is 'umber *ong integer. ) common mistake is to try to link two )uto'umber fields together.

The -redit-ard table doesn't ha#e to be complicated% as you can see:

To create the !elationship% hit the !elationships button on the toolbar:

Referential integrity: this is important, it means that you can't enter a credit card that is not in the CreditCard table, this keeps the data honest, you almost always want to do this. Cascade update: if a card name changes% all customer records will be updated automatically to reflect the change, that's O- - usually you want this to happen. Cascade delete: if you delete a credit card name from the CreditCard table% all customer records holding that card will be deleted, you don't want that to happen, don't check this box.

;ow% apply the same idea to the Mo#ies table. The fields: -ategory% !ating% Language% 8ormat can all be stored in other tables and linked to Mo#ies. The same principles will apply. $f you add a new category% you add it only once to the -ategories table. $f a category name changes% you change it once% in the -ategories table. The only difference in those tables is that we'#e gi#en each item in the list a number: 1 B &ction% 1 B !omance% etc. The list will still work the same as the -redit-ard table. 8or example% the Language table:

/ere's what you will e#entually get when all the tables and relationships are set*up. +on't worry about all the extra tables% we'll get to those in the following lessons.

2e8t wee,:

nterin( data into tables. "ee #ou then7

Lesson % for

"aturda#$ April !%$ !&&'

nterin( data

;or,in( with the data (rid


"hen you ha#e a large 5uantity of data to input% the data grid is not a bad way to go. ,ou normally won't use this techni5ue for day*to*day operations * we will create forms for that * but it is useful at startup. ,ou will ha#e to modify some of the properties of the fields in the table to make the best use of all the functions. $t is recommended to test the changes on a few entries at first. ,ou get the data grid when you click Open on a Table in the +atabase window.

The data grid acts like a spreadsheet. 8or people used to working with 0xcel it should look 5uite familiar. 0#en the tools used to manipulate the data are similar to those used for the spreadsheet.

nsurin( the accurac# of data

Default values and 9alidation are #ery important. They ensure that the information that gets into the database is as exact as possible. $f there is a field where you see you will enter the same data more than ? @ of the time% use a +efault. $t will display automatically and you only ha#e to tab o#er it to accept it. $f you ha#e to change it% you 2ust enter a new #alue o#er the default. & +alidation rule is a check on the accuracy of the input. 7ex can only be :M: or :8:% for example. $f a field had to be less tha 1 you would code the #alidation rule as: N 1 . $f the operator makes a mistake on a #alidated field and there is no +alidation te8t% chances are the system will display a weird error message (:,ou commited a fatal error= ,ou will die=: can be somewhat intimidating.) The #alidation text is your way of telling the operator what he should ha#e done.

-hoosing the input from a list is a lot more exact than keying it. The table containing the list should already ha#e been created. Oust select the table.

$f the table for the list contains more than one column (Language% for example)% 0olumn count will be 1 and Bound column will be 1. <ound column is the position of the column that will be sa#ed in :Mo#ies:. The list will show :Language number: and :Language: but it is column 1% Language number that is sa#ed.

Lesson = for

"aturda#$ April !%$ !&&'

0reatin( a data entr# 4orm

;or,in( with the Auto4orm ;i.ard


$n the pre#ious lesson we used the data grid to get information into the tables. &lthough useful in some situations% it is not #ery practical for day*to*day operations because it is not a format that non*programming types are comfortable with. 0#erybody is familiar with forms. 7o% to display information from the tables or to input data into the tables we'll create a form for e#ery table in the database. "e'll do the -ustomers form first and then% all the other tables can be done the same way. There are se#eral ways to create forms. $t is possible to do it from scrtach but that is rarely necessary. The easiest is to use the "i'ard to get the basic design and then customi'e it.

&s soon as $ ha#e the form created% before $ go further% $ will "ave and 2ame the form. There is no problem in gi#ing the form the same name as the table * in fact it is usually easier to associate the two ob2ects when they ha#e the same name. &ccess keeps all the ob2ects separate so in fact there will not be a conflict if $ ha#e a 0ustomers table and a corresponding 0ustomers form. ;ow $ can customi'e the form.

&ll the ob2ects on the form% the boxes% lists% titles are called controls. -ontrols can be changed% mo#ed around% resi'ed% colored% etc. ,ou can select se#eral controls at once using the standard "indows methods: shift*click% control*click and change all the ones that are selected using the 0dit toolbar. $f you ha#e to go to the properties of the controls% you open the Properties window with a !ight*click on the control.

&s you do the changes% take a look at the results fre5uently and ad2ust the form accordingly. 0#ery time you go to 8orm mode% do a "ave at the same time.

.nce the layout is complete there is one last% #ery important task to do. $t is called the )ab order. $t is the order in which the cursor mo#es when you hit the NTabP key on the keyboard. "hen the form is first created% the Tab order will follow the order of the controls on the form. <ut as you mo#e the controls around% the Tab order does not change. 7o% if you mo#e a control from the end of the form to the beginning% it will still be last in the Tab order. Hsually% the user will na#igate through the form using the NTabP key. People don't work with the mouse to go from field to field on a form. $f the cursor 2umps from 8irst name to &ddress to Last name% etc when you hit NTabP% you will soon get #ery frustrated.

$t is ver# important to make sure that the Tab order follows the order of the controls.

To do other operations on the form% use the appropriate button in the toolbar. ,ou can delete an existing customer or add a new one.

$n the case where you ha#e ?% customers and you ha#e to find the record for a particular one% do a 4ind operation using the toolbar.

;ow% create a form for the Movies table using the same techni5ue.

Lesson > for

"aturda#$ April !%$ !&&'

0reatin( simple ?ueries

8or more information and special deals related to any of the issues on this page% place your cursor o#er the double*underlined links. &ll information supplied by >ontera.com.

4indin( answers to 1uestions


;ow% let's say we'#e created the Mo#ies table and% using the appropriate 8orm% we'#e input the mo#ies that we carry. /opefully we ha#e thousands of entries in that table to gi#e our customers the greatest choice possible. &nd% if you look at the example below you will note that we usually ha#e se#eral copies of each mo#ie in stock. 0ach 9/7 tape or +9+ carries a different item number but the rest of the information has simply been copied and pasted from one to the other.

"e'll

use

the

Mo#ies

table

to

look

at

?ueries.

& 5uery is a 5uestion% a search% an interrogation. $n &ccess it is an ob2ect of the database. & 5uery is a stored 5uestion. ;ot the answer% mind you% @ust the 1uestion. $f a customer comes in and wants to know what we ha#e starring Tom -ruise or directed by 7pielberg or released in 133?% all those are 5ueries. &s long as the information is stored in the database we can get it. "e will also ha#e 5ueries that are repeated all the time and we will ha#e to take care of those also: :"hat were the sales figures for last month% for last year6: :/ow many customers do we ha#e and what is their a#erage age6 &re there more men or women6:

The simplest 5uery would return a list of a whole table. <ut we rarely need to see all the columns in a table. The first thing we do when designing a 5uery is to determine what columns or fields we want to see. Hsually 2ust enough to describe each record that will display. :7how me all the directors we ha#e.: $ could show 2ust the director's name but that wouldn't be all that useful. $ decide that $'ll also show the name of the mo#ie and the year it was made.

The example abo#e brings up an interesting problem * if $ ha#e 1? copies of a mo#ie by a director% whene#er $ use a 5uery on director% or star% or title for that matter% the same mo#ie will come up 1? times. "hat $ actually meant was: show me all the uni1ue mo#ies by a director% not the tapes or +9+'s. There is a simple way to correct that and that's to use the Properties of the Query% as shown below:

$ can now sa#e the Query so that if the 5uestion e#er comes up again $ only ha#e to go to the Query ob2ects and open the 5uery that $ had sa#ed. $'ll gi#e it a name that will remind me of what it does% not 2ust Query1. Oust to remind you again: the 5uery results are not sa#ed here. .nly the structure of the 5uery is sa#ed. $f we add a bunch of new mo#ies before we run the 5uery again% all those mo#ies% if they apply% will show up next time.

"pecif#in( criteria
,ou may ha#e noticed in all the pics abo#e that the title bar always refers to *"elect ?uer#*. That means that we are selecting something. $n fact% we usually select some records from the table. "e rarely ha#e to see all the mo#ies% or all the customers. To select some records we will use the 0riteria line in the 5uery to specify a condition for a record to be included in the result. 8or example: 7how me all Tom -ruise mo#ies in stock. 2ote: in all these $ will use the Hni5ue 9alues property so that $ don't get F or ? repetitions of the same mo#ie.

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.

= > < >= <=

e.ual greater than less than greater or e.ual less or e.ual

<> not li e between

not e.ual reverse condition

8amples: &ll mo#ies with a #alue of R1? or more. mG-ost PB 1? &ll mo#ies produced before 133 . mG,earProduced N :133 : &ll mo#ies released after 133?. mG9ideo!elease P S133?*11*E1S &ccess is #ery flexible. /owe#er you should get into the habit of using punctuation correctly. Hsually% text fields% or strings% are enclosed in 5uotes: :Tom -ruise:% dates are enclosed in number signs: S1 * 1* 1S and numbers are enclosed in nothing. The between operator is used like it's pronounced: mG,earProduced between 13A and 134 it is inclusi#e in that 13A and 134 both appear in the result The li,e operator is #ery useful and is used extensi#ely when working with text fields. $t is used in combination with the A % the wildcard character. 7how all stars with a name beginning with Tom. m3"tar li,e *)omA* L stands for :any string:% so that the 5uestion becomes :Tom followed by something: 7how all mo#ie titles containing the word :wind:. m3)itle li,e *AwindA* mGTitle is :any string% wind% any string: .f course this will return :Cone with the "ind: along with any title containing :window: or :winding:% etc. 7how all mo#ies starring somebody not named Tom. m3"tar not li,e *tomA* $f there are criteria in 1 columns% both ha#e to be true. This is an A2D condition. "hat mo#ies ha#e 7pielberg and Tom /anks done together6

To get an .! condition% use the .! line after criteria. 7how mo#ies starring 9i#ien Leigh or Tom -ruise.

Lesson B for

"aturda#$ April !%$ !&&'

More comple8 ?ueries

8or more information and special deals related to any of the issues on this page% place your cursor o#er the double*underlined links. &ll information supplied by >ontera.com.

;or,in( with dates


"hene#er you de#elop a commercial application% there is absolutely no way that you can get by without using date fields. The 9ideo application has Mo#ie !ental date% !eturn date% +ate*of*birth% !eport dates. The +ating 7er#ice has dates for dates. The -redit card has an 0xpiry date. There are <irth dates% /ire dates% +eli#ery dates% .rder dates% and so on% and so on .... $n ancient times% like 1 years ago% dates were stored as strings and we all remember what that brought about in 1333. ;ow all +<M7s handle dates in a +ateMTime format% which makes our li#es a lot simpler% but we ha#e to be aware of the particular properties of +ate formats. <efore we begin writing 5ueries there is an aside we must make about formatting. &ccess is a Microsoft product% ob#iously. &s with all Microsoft products it works closely with "indows% ob#iously. "hen it comes to formatting for different countries &ccess gets its information from the "indows -ontrol panel. The "indows default #alues are basically &merican. <ut% if you li#e in 8rance for example% the standard date format is dd*mm*yy and not mm*dd*yy as you are used to. &nd some countries use the comma as a decimal separator and put the currency sign to the right of the amount and so on. Therefore% before starting with &ccess you should go to the -ontrol panel ***P !egional settings and ad2ust all the settings for your particular situation. To a#oid confusion with dates $ preach and preach to all who will hear that you should automatically adopt the &;7$ standard for dates and that is: ####/mm/dd. 8or example% Oanuary 1st 1 E should alwa#s be input into &ccess as: !&&5/&1/&1.

$n ...

&ccess%

date or time constant Cm3videoreleaseD

should

be

identified E

with

... S% as in: F!&&1/&1/&1FG

To begin with% know that you can do some calculations with dates as you do with numbers. F!&&5/&1/51F / F!&&5/&1/&1F will return 5&% the number of days between the 1 dates. F!&&5/&1/&1F H 5 will return F!&&1/&1/&<F because a numeric constant is always taken to mean days. "hen using the comparison operators% I Fdate1F is taken to mean later than or after and J Fdate1F is taken to mean earlier than or before. The smallest date #alue refers to the earliest or oldest date. The person with the smallest date*of* birth is the oldest. This sometimes comes*in handy because you can sort people by age without ha#ing to actually calculate age: to get a list in ascending age order% sort on date*of*birth descending. $n the criteria B ); inclusi#e. 2 Fdate1F A2D Fdate!F sets a date between date1 and date1%

To work with date fields we'll use the +ate and Time functions that &ccess supplies. There are do'ens of built*in functions that will allow us to manipulate dates and times in 2ust about any way that you can imagine. The function that will probably be used more than any other: 2owK L returns the current date from the system clock. $n &ccess and 7QL% one of the most useful functions is called: DateDiffK L DateDiffK-interval-$ Fdate1F$ Fdate!FL returns the time difference between date1 and date1% expressed in inter#al units which could be: days% months% years% weeks or hours. The inter#al is specified as: 'd' for days% 'w' for weeks% 'm' for months and 'yyyy' for years. 8or example:

DatediffK-d-$ F!&&5/&1/&1F$ nowKLL returns the number of days between Oanuary 1st% 1 E and today. DatediffK-m-$ p3"tartDate$ p3 ndDateL returns the number of months between start date and end date% in this case% for a pro2ect. $f the result displays too many numbers after the decimal% use the 6OM2DKnumber$ di(itsL function to display the number rounded to 'digits' positions after the decimal: 6OM2DKDatediffK-m-$ p3"tartDate$ p3 ndDateL$ !L. $n theory% DatediffK-####-$ c3BirthDate$ nowKLL returns the customer's age% expressed in years. $n practice howe#er% you will find that it works or doesn't work depending on whether the

person has had his birthday yet this year or not. Hsually it doesn'y work #ery well. To calculate the exact age% the following formula is 5uite accurate. There may be a small #ariation of a day or so once in a while. I2)KDatediffK-d-$ c3BirthDate$ nowKLL95=%.!%L

-alculate the number of days between birth and now and di#ide by the exact number of days in a year% which% as you know% is EA?.1? and not EA?. That takes leap years into account. The $nteger function% I2)K L % truncates the result so that 1?.3 becomes 1?% for exampleD the person is 1? years old until the day she turns 1AD after the age of ?% you rarely hear people say that they are 1? and a half years old.

?ueries that calculate


$n the pre#ious lesson we created se#eral 5ueries to get information from a table. <ut we always selected columns that had already been defined in the table. "e can also define a 5uery that will do other calculations using data from the table. &s we saw in the pre#ious section% we can calculate age if we know date*of*birth% as in the following example:

&s you can see% to put calculations into a 5uery: Pick +efine a heading * a an name followed by empty a colon * for example% column &ge:

"hen you use a column name in the calculation% it must be enclosed in s5uare brackets as% TcG+.<U &ll functions ha#e parentheses * make sure all parentheses are balanced% especially if you use se#eral functions .b#iously you can do more than calculate age with 5ueries. 0#entually you will use the techni5ue to calculate sales amounts% stock 5uantities% etc. 8or example% if you ha#e a 7ales table with a s3?uantit# column and a s3Amount column% you would do: Line)otal: Cs3?uantit#D A Cs3AmountD .ne of the fre5uent uses of this is to do concatenation% that is% stick two strings together. 8or example% instead of ha#ing Last name and 8irst name in separate boxes on a form% $ want to see 8irst name and Last name together. To get that $ ha#e to put it into a 5uery:

Mpdate and Delete 1ueries


3 @ of the time when you use a 5uery it will be a 7elect 5uery * one that displays information. .nce in a while you will ha#e to do maintenance operations on the tables: change a group of prices% delete all obsolete items% etc. 8or those operations you use a different type of 5uery called: an action 1uer#. "e'll look at two: the Hpdate and the +elete. 8irst% create a new 5uery and specify the table that will be affected% same as the 7elect 5uery. Then% choose the Query type from the toolbar.

8or the Hpdate 5uery% specify the operation on the update line: what you want to change and the criteria: which records will be changed.

8or the +elete 5uery% all you ha#e to specify is the -riteria. ,ou cannot ask to delete certain columns in certain recordsD you can only delete entire records. &fter choosing +elete 5uery% specify the -riteria to delete records:

Vous aimerez peut-être aussi