P. 1
Giao Trinh Access Va Ung Dung

Giao Trinh Access Va Ung Dung

|Views: 194|Likes:
Published by loveinmagic

More info:

Published by: loveinmagic on Sep 09, 2010
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

07/05/2014

pdf

text

original

, A' ~

TS. HUYNH QUYET THANG

Giao trinh

ACCESS VA lING DUNG

(Sdch dung cho cac trucng dao UJ.o h~ Trung boc chuyen nghi~p)

NHA XUAT BAN GIAO cue

Nam 2002, V¥ Gido due Chuyen nghi~p - B9 Gi6.0 due va Dao tao aa pha;, hflp viii Nha xuat ban Gi6.0 due xudt bdn 21 gido trinh phl,/.c vI,/. cho aao tao ~ THCN. Cdc gido trinh tren. aa. dut;lc nhUu truimg sa d,!-ng va hoan nghenh. De' titp tl}c bO° sung ngudn giao trinh aang· con ihitu, VI! Guio due Chuyen. nghi~p pha, hflp cung Nha xudt ban Gido dlJC tie'p tl!C bien sQ9.n m9t sa'giao trinh, sdch tham khdo phuc vI,/. cho aao tao d cae nganh .' Di~n - Di~n til, Tin h9C, Khai thiic ca khi. Nhitng giao trinh nay truttc khi bien sQ9.n, VI,/. Gido due Chuyen. nghi¢p aa gm a€ cuang ve' tren 20 truiJng va t6 chuc h9i thdo, [fiy y kie'n dong gop ve' n9i dung M cuang cdc gido trtnh. n6i tren.Tren ca sa nghien CUu y kitn dOng gop

. ctta cdc truimg, nh6m tac gid da. aie'u chinh n9i dung cac gicio trinb cho phil hflp vcR, yeu cdu thuc tiin lum.

Viii kinh nghiem gidng dr:;t-y, kiln thuc tich. Iuj qua nhUu ruim, cae tac gid da. co g&ng de'nhitng n9i dung au(jC trinn bay Ia nhftng kie'n thuc ca ban nhat nhung vdn c~p nhQ,t aW!C vat nhitng ti€n bg cua khoa h9C kj th~t, vm thljc tt san xuat. N(ji dung cu.agido trinh eon. t~ sl/ lien thong til D(ty ngM len THCN.

Cdc guio trinh du(jC bien sor:;t-n theo huang mO', kien thl1c r9ng va co g&ng chi ra tinh. ang dl,/.ng cua n(ji dung ali?c trinh bay. Tren ca 80' d6 tao dilu ki¢n ae' cdc truiJng sa d'!-ng mgt cach phil hflp vm dilu ki?n ca sO' v~t chat phuc VI! thljC hanh, thl/c t~p va d{ic diim cua cdc nganh, chuyen nganh 000 tao.

De' vi?c d6i mdi phUdng phdp d{ly va h9C theo chi d"r:;t-o ctta Bg Gicio due va Dao too nMm nang caD cMt lur,Jng cU;&y va h9C, cdc truiJng can trang bi au sdch. cho thu vi¢n va t{W dilu ki?n de' gido vien va h9C sinh c6 du sach theo nganh 000 tao. Nhitng giao trinh nay ciing la tai l~u tham khdo tot cho h9C sinh da tot nghi?p cdn dao tao lai, nhan vren kj thu~t dang trllc titp san xuat.

Cdc gicio trinh eta xuat ban kh6ng the'tranh kIWi nhitng sai sot. Rat mong cdc thay, c6 gicio, br:;t-n a9c gop y de'lan xuat ban sau ttl/(jC tot han. M9i gop y xin gm ve' .' C6ng ty Gel phan sach DfLi h9C - DfLY ngh~ 25 Han Thuyen - Ha N(Ji.

V1) GIAo Dt,IC CHUytN NGHltp • NXB GIAO Dt,IC

3

Trang thbi dt;r.i ngay nay, may tinh dii co m{it trong moi linit v{tc xii h9i, d{ic bi~t no lit m9t c{mg ql ho tro kh/mg the'thietL troug cimg ttic qut'm Iy.

Man hoc Hf quim tri co sa dii li~ dii duoc dua vao giang d(lY Clla cac Khoa CNIT va Toan'tin tai cac Truong fJt;r.i hoc, Cao ddng. V6i mong muiin co mQt giao trinh ha tro cho cac em sinh vim, nhii'ng ban tre muml hoc su dung va l@p trinb vcYi H~ quan tri.co sa dii li~, chilng tOi viet Ciao trinh. nay nhlim trang bi cho dQc gia mot s6'kiett thuc co ban vi' khtii niem co sa dii li~ quail h~ va mQt so'ky nang trong Microsoft Access de'giiip cdc ban dQc gia co the' xdy d1;fllg nhauh ciic «ng d{tJtg nluJ, llhlim pht;lc Vt;l cong ttic quim Iy ho{ic hoc t6p rna eiic b(l1t duoc M't han.

Ciao trinn duoc chia lam 6 chuang v6i 1tQi dung XUye" SlLO't :

Chuong 1- Ci6i thiiu h~ quan tri co sa dii li~t Access Chuang II - Tao l~p co sa dii Ii~lL

Chuang III - Thiel: ke'bieu mdtt va bao cao Chuang IV - L~p trinh tren ACCESS

Chuang V - Su dt;lng dOi tUf;rizg trong Visual Basic for Access Chuang VI - Su dt;lng co sa dii li~ trong Visual Basic

Cnol mai chuang diu co cac ctiu hoi all t6p va bai ttjp. Cae b(l.11-dQc gia khi doc titng chuang c6 the' cai d,;tt va su d1;lng au: vi dt;l trinh bay trong cdc chuang de'thllc hanh. Nhiing bai t~p itt lam cuol mai chuang Ia nhiing bai kiem tra nho giup b(ln cUng co' kim thuc rna minh, cac b{fn khong nrn bo qua nhiing bai ttjp nay.

Chuc cac ban thanh congo

Rat mong nhtjn duoc y kim dong gop Clta cdc ban de'litn tdi ban salt duoc holm chinh han. MOi gop y xin duO'c gUi vi': Nhii XBCD - 81 r-s« Hung fJ(lO, iu NQi.

TACGIA

4

Chuong I ,

GIGI THIEU HE QUAN TRI co so' DO' LIEU ACCESS

. .'. .

1.1. KHAI NI~M V A. cAc TINH NANG CVA H~ QUAN TRl CO so mr LI~U QUAN H~

H~ quan tri Co sa dtt li~u (CSDL) quan h~ - DBMS la M cho phep luu trii va loc thong tin c6 cAu tnic til thiet bi hru trU. Vf du v~ cac M quan tri CSDLm~nh :

- Oracle

- Microsoft SQL Server

-IDMDB2

- Informix

Vi du v~ DBMS dB ban c6 : - Microsoft Access

- Microsoft Foxpro

- Borland dBase

1.2. GI(~n THI~U H~ QUAN TRl CO SO Oa LI~U ACCESS

Access la mot h~ quan tri CSDL c6 nhieu cac tfnh nang cftn thiet. Vi du, Access chua cac thanh phan sau :

- Mot h~ quan tri CSDL h6 tro hai ngon ngfr van tin chuan c6ng nghiep la SQL (Structured Query Language) va QBE (Query By Example).

- Ngon ngfr I~p trlnh thu tuc - v~ co ban la mQt t?P con cua Visual Basic.

- ¥6i tnrong trien khai irng dung nhanh co cac cong cu phat trien bao

bieu va bao cao tnrc quan.

- Hd tro l~p trlnh hu6ng d6i tirong.

- Cac thu~t sy (wizard va builder) tro giup thi~t k~.

5

Microsoft Access duoc sir dung de ~p hop, Iuu t:rIl va xu 19 nhieu Ideu thong tin. S1) da dang nay nhieu khi gay kho khan cho nguOi moi sir dung, vi v~y khi lam viec voi Access d.n xem xet dum nhtmg goc dQ khac nhau, vi du :

- Dum goc dQ CSDL quan h~, xem irng dung g6m cac bQ dte li~u.

- Dum g6c do I~p trinh thu tuc, .xem irng dung Ia nhirng I~nh thirc hi~n

tuan tu.

- Dum goc dQ huong d6i nrong, xem trng dung g6m cac d6i tuong co phuong thirc, thuec tfnh va nrong tac qua I~.

Vi~c lira chon phuong phap phat trien irng dung nao 13 tuy thuoc vao ngiroi su dung nhung t6t nhAt Ia hi~u h~t cac tinh nang cua Access d~ ap dung vao nrng vi~c sao cho phu hop nhat,

LQ"i Ich IOn nhAt khi dung Access U\ nguoi sit dung Hun quen diroc voi

cac khai niem cua cac h~ th6ng khac nhau :

- Oracle cho CSDL quan M.

- PowerBuilder d~ tri~n khai ung dung nhanh.

- SmallTalk cho l~p trlnh huang d6i nrong,

Tip CSDL cUa Access

- Khai niem CSDL thuong chi mQt ~p cac bang lien quan d~n nhau

nhung mot t~p CSDL cua Access bao g6m nhieu thanh phan hon :

+ Cac query de vdn tin.

+ Cac form d~ wong taco

+ Cac report d~ in bao cao.

+ Macro va don th~ VBA d~ l~p trlnh.

- Khai niern "co sa dte lieu" bao ham nhieu nghia khac nhau tuy thuoc vao h~ quan tri CSDL su dung, Vi du trong dBase N, mot CSDL la mot t~p (<ten t~p >.dbf) chua mot bang duy nhAt, cac bi~u m§:u (Form) va hao bi6u (Report) duoc luutren nrng t~p rieng le. Ngiroc lai, trong Oracle mot CSDL co the chua nhieu bang tit nhi~u dir ani irng dung khac nhau va ban than CSDL co th~ chia lam nhieu t~p (tham chi c6 the nb tren nhieu may).

6

Access nfun 6 vi trf can bAng 6 giii'a - tAt ca cac d6i nrong (bang, truy vAn. bieu mftu. bao bieu, ... ) cua mot du ani img dung duoc hru trong mt}t t~p. Tuy nhien, cling c6 the tach cac d6i tUQ11g thanh nhieu t~p, vi du, cac bang vao mot t~p. chuang trinh niim 6 t~p khac,

- DBn t~p CSDL : Mot tep CSDL cua Access c6 th6 phan manh va trc nen Ion han rAt nhi6u so v6i thirc t~. co- th~ vai megabyte cho mQt t~p chi co vai ban ghi. Dbn tep se loai bo phan manh va giam dang k6 kfch thiroc t~p.

- 86i ten CSDL : Access khong co lenh Save As cho mQt CSDL, d~ dbi ten ta su dung Explorer cua Windows ho~c khi compact mot t~p dang dong, Access cho kha nang luu t~p da dBn du6i mN t~n khac,

- Su dung bang lien k~t : Nhieu nha thiet k~ irng dung Access d~t cac bang trong mQt t~p CSDL va cac d6i tuong con lai nhu truy vein, bao bieu, chuang trinh tren met t~p khac nham d~ tach biet giea dfr li~u va chuang trinh.

Access cho phep su dung tfnh nang "linked table" d~ lien k~t bang til mot t~p nay sang rnt}t t~p khac. Tep chua tilt ca cac bang la tep dtr lieu (data) con t~p chua cac chuang trinh la t~p giao dien (interface).

Nho each tach rieng dtt li~u ta co tha d~ dang nang cdp irng dung rna kh6ng anh huong d~n dfr li~u. Chu Y .' bang link phu thuoc ala chi thu.muc tuyet ddi.

- Vi~t mQt img d\1TIg voi Access: co hai each co ban d6 xay dung mot M

thong thong tin:

+ Phan tich, thi~t ke va coo d~t he thAng mot each ky luOng.

+ u.p trinh thrr nghiern nhanh (trong do cac khau phan tfch, thi~t k~ va cai d~t diroc thuc hien l~p ill I~p lai),

Access cung cdp nhi6u tinh nang nhu cong C\1 thiet ke tnrc quan, cac Wizard, Macro thuan loi cho vi~c thi~t k€ nhanh. Access thirong diroc su dung d8 thi~t k€ he thAng vira phai va nhanh n~n each ti~p c;[m thu hai phil hop han.

Cac bUGC d~ l~p trinh thir nghiem mQt img d\1TIg Access duoc th~ hi~n trong sa db sau :

-:.t;. ..

7

Phltn tfch yeu cau, thiat ka rna hlnh

h$ th6ng thOng lin moe logic (thuc Ihi! quan M)

, l

Tao bang cho mOi thuc thi

-l

L$:p quan h$ giQoa cac bang

l

-Xii Iy thOng lin tren cac bang SUt dl,lng Query

l .

Tao Form va Report e~ hO trQ' nhap, xuAt, hian Ihi

l

NAng cao Form vai cacControl nh$p Ii$u

l

Tao cac query hanh <1l)ng Macro hay J$p trlnh VBA ee thl,l'Chi$n giao djch coa Crng dl,lng

l

Hoan thi$n oac thu tuc bAy su ki(ln

1.3. H~ TH6NG MENU CHiNH eUA ACCESS (hinh 1.1)

D~ khei dQng Access, tim bi~u tUQ11g cua Access rBi nhAn kep chuQt hoac goi Microsoft Access tiz nh6m Microsoft Office tren cira sf, Start.

- File: Get external Data: ldy dfr li~u tir nguBn khac vi du nr Foxpro, Excel Text.

Database Properties: xem thuQc tfnh cua CSDL. Export : xudt cac d3i tirong ra ngoai,

- Edit! Create shortcut.

- Tools: Relationship : th~ hien m3i quan h~ gifra cac bang

Database Utilities: cac ti~n fch cua CSDL

-View

Database Object: xem cac d3i nrong nhtr : bang, truy vAn, form, report ...

8

- Insert

-Windows

- Help '; co th~ nhAn Fl

Qpen .•.

!act External Do!It.e !:lose

Ctrl+O

Ctrl+5
Create t.e
Cre.ete t.e
Create t.e
CHIMUC
DIEM
DIEM_TG
KHOA
Ctrl+P LICHTRIN
~ .. 1.4. CACH T6 CHUC DO' LI~U TRONG ACCESS

Tit eel cac CSDL diroc thi~t k8 bing Microsoft Access d~u sir dung cac dAi, nrong CSDL co ban (hlnh 1.2). Dai nrong cua Microsoft Access

, ~

baogorn :,

Bang (table) : Noi tnrc ti~p chua dfr lieu, M6i bang chua thong tin v~ mQt ki~u dfr li~u rieng, vi du nhu cac khach hang, cac loai san phfun, cac nha cung cdp hay cac nhln congo Thong thuong viec tim ki8m cac thong tin d6 duoc thuc hi~n tren nhieu bang c6 lien quan nhau. MQt CSDL thirc su c6 quan h~ Sf! co nhieu bang va sa bang d6 tuy thuec vito chirc nang rna CSDL do duoc thi€t k8. Ben trong rn6i bang, thong tin duoc hru trit theo nhtmg ban ghi (record).

S.eve 8,5 ... E.xPort .•• Setlrcb. ••.

POlQ8 ".Hyp ... (g, Print Pre:il_ a Erlnt ...

Send. To

O.eto!lbo!ise Properties

ldbl.mdb

~ E :\Acc-prOQr.em\Acc-ProQr51ls\ .•• \QIsv .mdb

;:} \OOCl.JYlE l \HONGTR l \LOCALS ..... l \ .•• \Qlsv .mdb

!. \DOCUME 1 \HONGTR 1 \ ... \QLNh.!Insu .mdb

Hinh 1.1. He thOng menu chfnh cua Access 2000.

9

Truy vtin (query) .' Tao nguAD diI lieu cho cac giao dien nhap lieu, cac bao cao cua nguoi sir dung tnrc tuyen,

BiJu rnau (form) : D~ xay dung giao dien gii'ra nguoi sir dung va may, nhap di'r lieu VaG CSDL, xay dung menu cho nguoi sir dung ...

Bao cdo (report) : Dung dua thong tin fa gidy.

Macro: La mQt hinh thirc l~p trlnh don gian duoc sir dung d~ gAn kch cac d6i nrong chfnh trong chuang trinh nhu lien h~ gitra cac form, tao menu ...

Module : La chuang trinh vi~t bang Visual Basic for Applications (VBA).

Trang WEB: Chua cac trang web xay dung tir cac d6i nrong cua CSDL.

I]]
.
gJ Queries
~ Forms
• Reports
til P.aQes
z:! Macros
4-
!1J Create t~ble by entering d~ta

Hinh 1.2. Cac c:r6i tuong CSDL cc ban cua Access 2000.

1.5, C6NG C1,1 WIZARD V A. BUILDER

- Wizard co thJ su dung a nhiJu noi khac nhau :

+ Tao l~p toan bQ mQt CSDL (Database Wizard),

+ Tren bang, truy vdn, bi~u mdu, bao cao, Vi du, sir dung Tables Wizards d~ tro giup tao ra mQt bang b~ng each chon rnQt trong cac cdu true table vi du, r6i chon cac tnrong rna ta muon chua trong bang' d6 'Sf:

10

nhanh chong 410 ra me;,t table hoan chinh. NSu table khong hoan toan chfnh xac nhu y muon, e6 th~ thay d6i no trong ch~ dQ Design View.

+ Tren cac ruBu khien.

+ Tao trang web.

- Expresion Builder la be;, cong C1,l thu~ ti~n nhdt d6 t<;1O cac bieu trnrc, cac dieu khien lien quan toi form hoac report.

1.6. cAe vi Dl,J Vi d~ 1.1 :

Tao m¢t CSDL vidu.rndb (vi d1,l sir dung Access 2000) (hinh 1.3).

1. Chay Access, xudt hien cira s6 Microsoft Access.

2. Chon Blank Database, nhdn OK ~ xudt hien cira s6 File New Database.

3. Nhap ten vao muc File Name, vi du vidu sau do nhan Create. Tep vidu diroc tao va rna ra.

Chon Blank Database

Nhfut vao

Hinh 1.3. Tao m~t t~p CSDL moi trong Access 2000.

11

Vi sa s.z .

Ma mQt CSDL cii

1. Chay Access, xudt hien cira s6 Microsoft Access.

2. Chon File> Open.

3. Chon ten t~p cAn rna.

Vi dl;ll.3 :

Nhl)p dfr li~1Il tiI bang tfnh

, .

Vi~c nhdp dfr li~u nr cac ung dung khac sang bang cua Access rat dan

gian. Vi du, nhap mQt bang tiI Excel (hinh lA, 1.5).

1. GQi File> Get External Data> Import.

2. Trong hQP File type chon Microsoft Excel, chon ten t~p cAn nh~p.

3. Chon Show Worksheets, chon ten bang.

4. Chon First Row Contains Column Heading.

5. Chon In New Table.

6. Chon No Primary Key.

File > Get External Data> Import tit menu chfnh va chuyen dSn thu muc chua file mu6n nh(ip

Hinh 1.4. Nh~p dl:t li~u tit bang tfnh. Chon ten t~p va kieu t~p.

12

chqn dong ctAu tien chua hra chon c¢t tieu dS

H'inh 1.5. Chon cac cQt tieu de si! d6ng vai tro nhu cac ten tnrong trong bang.

1.7. CAU H61 ON T~P - BAI T~P

1. reo m¢t CSDL va d;;.t tlln 18 quanlysinhvien.mdb.

2. Nh$p bang * .xls V80 thanh bang.

13

Chuong II

TAO LAp co sa oCr LII:U

. . .

2.1. TJ).O BANG

Bang la noi chua du lieu cua CSDL. vi v~y bang la n~n tang cua moi img dung CSDL. Ben canh dti li~u. Access con cho phep luu nhtmg thuQc tfnh du li~u kern thea bang nhu tieu d~ cQt, gia tri ngAm dinh, dinh dang hi~n thi hay nh~p."

Nghien ciru ky viec thiet ke bang se cho pnep ngiroi Slr dung dan gian hoa duoc nhieu cong viec trong nhfmg giai doan k~ tiep. N~u cAu tnic bang hay quan h~ giii'a cac bang bi thay dbi se ddn d~n nhieu thay dbi khac trong irng dung, di~u d6 se lam cho cong vi~c tro nen nit phirc tap,

Man hinh tao bang moi cua Access 2000 se c6 dang nhu hinh 2.1

~ ,'FomI$ "" ' • Repcrts'" ~ PaoM

a ,to\acroj .. ~

T{I,o bang mei

Hinh 2.1. Man hlnh tao bang moi cua Access 2000.

Vi du : Tao bang ten la SINHVIEN d€ thea doi sinh vien trong tnrong Bang g6m cac thong tin sau :

14

TENTRU'ONG KI~U DO' LIeU MO T A (khong b~t buec)
MASV TEXT Ma sinh villn
HO TEXT HQ ella sinh vien
DEM TEXT Phtm ten (f$m ella sinh vien
TEN TEXT TAn ella sinh vien
NAM YES/NO Nam hay nCr
NGSINH DATEfTIME Ngtly sinh ella sinh vien
OUEOUAN TeXT Que quan ella sinh vien
NOlO TEXT NO'I a hi$n tal
MALOP TEXT Malap Cae buac tiin hiinh tao bang:

1. Chon New tit menu File, chon Blank Database, ~t ten cho CSDL III quanlysinhvien

~ ,

2. Chon phieu Table,

nhan New.

3. Chon Design View ttong cua sa New Table (hlnh 2.2). Man hinh nh~ tham s6 cua bang se bien ra

(hinh 2.3). Hinh 2.2. Man hlnh chon ki~ll thich k! cho bang,

Cac ki€u dft liqu :

- Text: xau ky tu ~ 255 ky t1)'.

- Memo: van ban ~ 64000 ky tl,I.

- Number: s6 (s6 nguyen va s6 thuc).

- AutoNumher: tu dQng tang khi them ban ghi moo.

- Currency ': tien t~.

- Yes/No : logic

- Date/Time: ngay/gia.

- OLE Object: luu anh bieu d6 van ban 160.

- Hyperlink : dia chi trang web.

15

Hinn 2.3. Man hinh nh$.p cac tharn s6 cua bang.

4. Trong cira s6 Table Design. thi~t k~ bang thea thong tin sau (cac thong tin nay la co ban. ngoai ra co th~ them mQt s6 tnrong phu),

Ldn hrot thuc hien cac thao tac nh~p ten tnrong, chon kieu dfr li~u va nh~p mo ta ten tnrong cho 9 tnrong cua bang. chon File> Save tir menu hay nhftn t6 hQ'P phfm CTRL S d~ luu bang dum ten SINHVIEN (hinh 2.4, 2.5).

O)t rna te. cho phep go vao rn(H dean chu thich ngan v~ tnrong (thong tin nay khong duoc xu ly boi Access)

GO vao ten tnrong va ki~u dil" li~u

'\!"P''''''."''I''..I Cac thu~c tinh tnrong cho phep ghi thang . tin v~ wang va cac rang bUQc v~ cac gia tr] L..-.l~==~~";';";'~':"' __ -I cho truong.

~--------------------------~

H'inh 2.4. Man hlnh tao cac wang cua bang.

16

Hmh 2.5. Bang SINHVlEN da: duoc tao l~p (ten cac wang nAm tren hang ngang).

2.2. THIET KE cAc KIEM soxr VU LI~U TREN BANG

2.2.1. ThuQc tinh ella tnrimg

f)~ xac dinh thuec tfnh cua tnrong c6 th~ su dung h(,>p thoai tao bang (hinh 2.4)

- Field Size/f)(J r(Jng : Phu thuoc ki~u dft li~u +S6

+ Ngay - gio + Logic

+ Khuon dang d~ bi~t

- Format: Cho bi€t khuon dang dfr li~u in ra

- Input Mask/Mijt na nhdp : Dang cua dft li~u nhap, D6 la mQt X8.U k9 tu

chua nhfmg ky tu sau :

2·GTACCE&UD

17

KYTl,f Y NGHTA
0 cac chO' s6. Bat bu¢c nhap
9 Cac chO' s6 hooilc dau each, KhOng bat bu¢C nhap
# Cac chO' so, dau +, - hoac dau each, KhOng bat bu¢c
nh$p
L ChO' cal. Bat bu¢C nb$p
? ChCr cal, KhOng bat buoc nh$p
A ChO' cal hoac chCr sO. Bat buec nhap
a ChQ> cal hoao chCr s6. KhOng bat bU¢C nh$p
& Ky tV' bat ky hoac khoang trOng. Sat bu¢c nh$p
.,:-/ Cac dau phAn each cho kieu dCr Ii$u sO va ngay gia
< eOi cac ky tV' Mn pMi ky tV' nay th8.nh chCr thlfCmg
Hi$n nguyen d;;mg ky ti,r 46. KhOng 6lfQ'C IlfU trong bang.
Ky tV' khac TlfO'ng tV' thu¢c tinh format. Dung each nay nglfai sii
d',mg d~ hieuhO'n.
Password Che gia.u thOng tin khi nh$p N~u sir dung chua thanh thao, c6 ~~ goi Wizard b~ng each nhdn 0 vuong ben phai cua Input Mask.

Vi du 2.1 :

Thuec tfnh Input Mask cua tnrong MASV cua bang SINHVIEN (hinh 2.6).

Hinh 2.6. Vi du v~ thu~ tinh Input Mask cua tnrong MASV.

18

- Default Value: Gia tri ngfun dinh, tmrong Is mQt bieu thirc c6 xuftt hien h~ng, cac ham mdu va cac phep toan,

=Required : (Yes/No - Ngam dinh No) khong chophep dira gia tri Null vao tnrong,

-AllowZeroLength: (YesINo - Ngfun dinh No) chi dung cho cac truong ki~u Text hay Memo: cho phep nhan gra tri la xiiu r3ng.

- Caption (ph(l ai) : Ten cQt khi hien bang a chS dQ Datasheet.

- Validation Rule : Di~u kien rna diI lieu nhi,lp vao tnrong phai thoa man.

Di~u ki~n dai t6i da 2000 ky t\I.

C6 th~ sir dung cac ky nr sau :

+ Cac h~ng: Cap nhay kep cho hing xau ky tu, c~p # cho h~g ngay gio' (khi sir dung cac h~ng phai dung dAu each).

+ Cac phep toan so sanh : =, <>, >, <, >=, <=

+ Cac phep toan logic: Not, And, Or, Xor, Imp, Like, In ...

+ Cac toan tit d~c bi~t khi thao tac diI lieu: Between, Like, In ...

Vi du 2.2 : tnrong NGSINH

- Validation Rule : Di~u kien rna diI lieu nhi,lp vao tnrong phai thoa man. Khi cdn ~t di~u ki~n tuBi cua sinh vien tfnh too thoi di~m hien tai n~ trong khoang 18 d~n 70. ta vi~t nhu sau :

Year(DateO) - Year([NGSINH]) between 18 and 70

- Validation Text: Dong thong bao duoc hien thi khi diI lieu nhi.lp vao tnrong vi pham di~u ki~n duoc neu trong Validation Rule.

- Lookup : D~ kiem soat diI 1i~u dam bao diI li~u nhi.lp vao mQt truong chi nhi.'in mQt trong s6 cac gia tri ho~e lAy tir mQt tnrong nao d6 a bang khac d.n chuyen thanh dang tnrong lookup.

+ Display Control: Listboxl Combobox

+ Row Source Type: Bang! Truy van danh sach gia tri

+ Row Source : Ten bang! truy v§n day gia tri, Day gia ttl phan each bang dAu ; . Vi du "A" ; "B" ; "C".

+ Bound Column: s6 thu t1,1' cua cQt eho gia tri

+ Column Count: s6 cQt dugc hi~n thi trong hQp Combo

19

+ Column Width: dQ fQng cac cQt (phan each bang ctftu ;)

+ Limit To List: Hl Yes thi kh6ng nh;\ip them gill tri moi, la No thi nhap them diroc gia tri moi.

2.2.2. ThuQc tinh bang

Viec kiem soat da li~u lien quan d~n nhieu tnrong khac nhau diroc xac

dinh tai Table Properties.

1. Nhfrn vao thanh teen cling chua ten bang.

2. Mo thuoc tfnh cua bang bAng each nhan chuot phai va chon Properties (hinh 2.7).

D.!Ita~eet View PlvQtT able Yle¥! PIy_otChart View Relationships .•.

Cut. c.opy

indexes

Hinh 2.7. M6' va xem thuec tfnh cua bang.

2.2.3. Xac djnh khoa chrnh

M6i bang co met khoa chfnh, khoa nay xac dinh duy nh~t ban ghi trong bang. Khi chi dinh mQt tnrong Hun khoa chfnh, Access kh6ng cho phep nh;\ip cac tri gi6ng nhau vao mrong khoa, D~ chi dinh rnQt hay nhieu tnrong lam khoa chinh, ta lam theo cac buoc sau :

1. Boi den tnrong chi dinh lam khoa (D~ chon nhieu tnrong, nh~ gift CTRL trong khi nhay len cac nut tam giac mao xam canh tnrong MASV).

20

2. Nhdn vao hlnh chia khoa tren thanh cong cu hoac chon Edit > Primary Key tir thanh Menu.

Thao tac xac dinh khoa chinh diroc th~ hi~n tren hinh 2.8.

CLt ctrl+X

~opy Ctrl+C

Office Clipboard ...

Hoac nhAn van hlnh chla khoa tron thanh cong cu hoJc chon Edit>Primary Key nr menu

De nhi~~ tnrong, nhdn ... giil' CTRL

trong khi

nhay len cac nut tam giac rnau xarn canh tnrong MASV

Hinh 2.8. Thao tae xac dinh khoa chfnh,

ThiiQ lu¥n vi kholi

Khoa la mQt hay nhi~u tnrong X3.c dinh duy nh't rnQt ctbi tirong trong th€ gioi thuc duoc th~ hi~n qua rnQt ban ghi trong bang. Vi du met ban ghi SINHVIEN chua dung thong tin ve sinh vien cu th~. D~ dam boo ban ghi do la duy nhat, no phai chua d\ll1g rnQt tnrong lit MASV (rna sinh vien),.ma nay diroc dam bao Ia duy nhdt (phai sir dung MASV lam khoathay cho ten sinh vien vi co th~ co hai sinh vien trung ten).

Cac khai niem chinh v~ khoa duoc tom tit nhu sau :

- Khoa chinb (primary key) : khai niem khoa va khoa chinh duoc sir dung IAn lQn. MQt bang c6 th~ c6 nhieu tnrong diroc chon lam khoa, cac tnrong nay goi la khoa du tuyen (candidate key). Ngiroi thi~t k~ chon khoa chfnh tit rnQt trong cac khoa du tuyen.

- Khoa ghep (concatenated key) : khoa diroc tao til nhieu tnrong. Vi du ket hop rna khoa voi s6 khoa hQC tao nen khoa.

21

- Khoa ngoai (foreign key) : trong quan M rnQt nhieu, rnQt khoa ngoai Itt tnrong (hay cac tnrong) trong bang b~g nhieu ("parent") lien k~t voi khoa chfnh trong bang bimg mQt ("child").

2.2.4. Nh,p dfr U,u

Chon Datasheet View tren thanh cong C\1 hoac rna bang (] ch~ dQ Open.

Bang hien ra de nhap dfr li~u. '

Ubi cAu true bang : Vi~c d6i cAu tnic cua bang lien quan d~n nhfmg c6ng

viec sau :

- Them tnrong : rna 1~ ch~ dQ Design View, them tnrong vao cu6i hay vao mQt vi ttl bitt kYo

- Xoa truong : chon tnrong coln xoa va An chuQt phai, chon delete.

- Ubi thu nr tnrong. .

- Ubi kieu tnrong.

- UBi khoa chinh.

2,3. QUAN H~ GIO'A cAc BANG

CAn xay dung m6i quan h~ gifra hai bang LOP va SINHvIEN vi sinh vien c6 rna s6 sinh vien la duy nhdt nhung c6 th~ c6 cling rna lap do d6 quan h~ gifra bang SINHVIEN va LOP Iii quan M rnQt - nhieu (hinh 2.9).

Mii lap "00 1 "

, l

nam trong nhieu

ban ghi

Hinh 2,9. Quan h~ mQt - nhiau gill'a MASV va MALOP.

,22

M6" cira s6 Relationship bang each chon Tools -> Relationship I kfch

hoat bi~u nrong :

1. Chon ki~ dBi nrong. Nhu chon ddi tirong Iii cac bang hay troy vdn hoi;ic ca hai (hinh 2.10).

2. Chon bang hay troy vdn rna ban mudn them (hinh 2.11).

3. NhAn dup hay An ADD. Up lai khi cAn (hinh 2.11).

Rinh 2.10. Man hlnh Show Table.

Chon hang . ban mu6n ;', them hoac

nhAn dup

.·ho~ An ..

ADD.14p ...

l{li khi cAn

Rinh 2.11. Chon hang hay truy vAn d! tao m6i quan M.

23

2.3.1. Xac (fjnh quan h~ gilla cae bang (hinh 2.12)

HiM 2.12. Xac dinh quan M gifta cac bang.

Keo tnrong d~t quan h~ tu ben mQt sang ben nhieu (hinh 2.13)

N~u lam dung thi m6i quan M m{lt - nhieu chi fa b~g duong quan M

Hinh 2.13. MAi quan h~ duoc thi~l13p.

2.3.2. Toan vqn tham c~16u

- Trong ella s6 Relationship, chon kiSu k~t nBi Join Type (hinh 2.14).

- Cira s6 Join Properties xuAt hien, chon m¢t trong ba ki~u k~t nbi :

+ K~t n6i nr nhien. + Ket n6i ben trai, + Ket n6i ben phai,

24

Hinh 2.14. Chon kieu kef n6i Join Type. * Enforce Referential Integrity

- Khfmg cho nh~p vao ben nhieu nhtrng gia trj kh6ng t6n tai trong ben mot.

- Kh6ng cho xoa gia tri ben mQt neu hen nhieu con gia tri nrong irng.

* Cascade Delete Related Records : nhUng ban ghi lien quan ben mQt xoa thi ben nhieu xoa theo.

* Cascade Update Related Fields: ben mQt c~p nh~p thi ben nhieu cap nh~p theo.

2.3.3. Chinh SLra va xci cae quan h~ (hinh 2.15) - Ly do dJ chinh su~ va xoa cac quan h~ :

1. Khi muon thay d6i ki~u dii' li~u cua mQt trong cac tnrong trong m3i quan h~. Access se khong cho phep lam di~u nay khi chua xoa mbi quan h~ (sau khi thay d6i ki~u dii' li~u cAn phai 4\0 lai m3i quan h~).

2. Quen mo ta referential integrity - neu 1 va 00 khong xu§.t hien tren dong quan h~ thi ta da khong chon ht)p enforce referential entegrity

- Cdc buac chinh sua hO(lc xod cdc quan h~ :

1. Chon quan M b~ng each click vao dirong quan h~ (neu hun dung thl duong nay se sang).

25

2. V.m rn6i quan h~ diroc chon, click chuet phai d~ tha menu xuong, chon xoa hay sua.

Chon quan h~ bang each click van duang quan h~. NBu lam dung thi duong nay si! sang

Qu!n cac ky hi~u "I" va "w" khi chua chon enforce referential integrity

V6i m6i quan h~ duoc chon, click chu()t phai d~ tha menu I ~1I'IA!_U' xu6ng chon xoa hay sua

Hinh 2.15. Cac thao lac chinh sua hay xoa m6i quan M.

2.3.4. Nh.p, xuAt d(P Ii~u

- Nh~p : File --+- Get External Data --+ Import hoac Link Tables

- Xuat : File --+ Save as/Export

- Luu bang dum dang HTML : File --+ Save as HTML.

2.4. T ~O TRUY VAN BANG NGON NGD' QBE

Cac troy vdn (query) cho phep k~t nAi dfr lieu til nhieu bang, s~p x~p dii li~u thee cac tieu chi khac nhau, tinh toan tnrong moi va chi dinh di~u kien d~ loc ldy nhirng ban ghi d.n thiet.

Ban than query khong chua dfr li~u rna chis~p xep lai dfr 1i~u tren bang (cac bang) rna query thao tac tren d6 va khong lam thay d6i cac bang d6.

Khi dii c6 query, ta c6 th~ su dung chung giAng nhu bang. C6 th~ coi cac query la cac "bang ao", Trong rnQt s6cae h~ quan tri CSDL khac, query c6 ten la "view". vi chung cho phep nguoi sir dung c6 cac quan sat khac nhau tren cling rnQt dii li~u.

2.4.1. T,o query (hinh 2.16)

1. Sir dung mit New trong muc Queries cua cira s6 Database d~ tao mot query moi. Chon Design View.

26

2. Chon hang d~ dira vao cua s3 Query (VI du tren hlnh 2.17 chon bang SINHVIEN)

3. Khao sat cac thanh phAn cua cua s6 thiet k~ query.

4. Luu query (CTRL S).

Hinh 2.16; Cac thao tac tao m{lt query moi.

Khung chua

nghia truy

vAn hi¢n tai

Khung tren chua danh sach tnrong

cho cac

bang rna

truy vAn dua vao

-Hinh 2.17. Man htnh truy vAn.

27

- Dong Field chi ra ten cua tnrong trong truy vAn, duoc keo tnrc ti~p til

bang xuong,

- Hang Table chi ra ten cua bang rna truong d6 thuoc vito.

- Dong Sort xac djnh thu tu cac ban ghi duoc hien thi.

- Cac hQp Show xac dinh cac tnrong trong troy van eo duoc hi~n thi

hay khong,

- Dong Criteria chi ro tieu chuan de bao gAm ho~ loai trir cac ban ghi til cac ~p k~t- qua.

2.4.2. Truy van chen cac ban ghi thea di6u kl,n (Select Query) tPtiep chitJu

Chieu rnQt tnrong len mQt query chi don thuan lit dua tnrong d6 vito query. Kha nang tao query dua tren mQt ~p con cac tnrong cua bang (hay cac bang) rdt ti~n 19i khi xu ly cac hang c6 cac thong tin khong bi m~t va thong tin bi m~t. Trong mQt sa tnrong hQ'P, chi chon mQt s6 thong tin.

Vi du 2.3 :

LAy mot viii thong tin v~ sinh vien tit' bang SINHVIEN (hinh 2.18)

1. Keo cac tnrong MASV, RO, DEM, TEN. NGSINH, NOlO len hroi tao query (mra dum cua cira sf, query). .

2. Nhftn nut f~!il tren thanh cong cu de xem k~t qua

Hinh 2.18. T1;lO truy vAn chon lAy mc}t sthrubng cua hang SINHVIEN ..

28

2.SapxfJp

S~p x~p tren query khong anh hirong d8n tr~t tv v~t 1y cac ban ghi trong bang lien quan (nrc 13. khong s~p x~p bang). Vi v~y co th~ tao cac query s~p x~p thee cac c¢t khac nhau cho cung m¢t bang.

Vi d{i 2.4 :

SAp x8p MASV thee chien tang (hinh 2.19).

". : ~ ,

cho

: ." :~ .. '.:.

_~ __ .. . __ .•. . __ _ ....•. _ _ L.. .•..•... '_"' __ "_"' .

Hinh 2.19. slip xep MASV theo chiSu tang,

3. Phep chOn

Cac ban ghi dircc chon bAng each chi dinh di~u kien m6i him ghi phai thoa man d~ dua V3.0 bQ k~t qua. Trong lu6i QBE (query by example), ta nhap di~u kien vao hang criteria.

Vi d{i 2.5 :

Chon cac ban ghi c6 HO ;::; "Nguy~n". Nh~p "Nguy~n" vao hang criteria (hinh 2.20). Ta co thS nh~p ;::; "Nguyen" nhung toan tu;::; la ngam dinh nen khong d.n nhap,

•. HO

Sitlvien

Hinh 2.20. VI du v~ phep chon .

... ._._ J91YIlfC __ -----+

29

4. £>;eu k;cn chOn ph(fc hvp

C6 th~ tao cac di~u kien chon plnrc hQ'P sir dung phep AND, OR hay

NOT.

- Dua them tnrong MALOP V3.0 troy vdn.

- Tao troy' vdn xem tc1t ca cac ban ghi c6 rna lap 13. 00 1 hoac 003

(hinh 2.21).

,,, NIl"'

iWI_loftkdlD1 m!l81c11 GlHDI '1/lIT"1 "'" O!tll -001 1J6II~ T" \jjI. I 001 ;0119111 Dlth~1DI M.'U~(DI , 1'11_' n "WI! Uft 1"" 001

IIJI

Cac k& qua. Chi c6 cac ban ghi thoa man Criteria moo

Hinh 2.21. Di8u ki~n chon phtrc hop va cac ket qua thu duoc.

5. Phep n6i

Tao mQt query dira tren nhieu bang d~ c6 nhieu thong tin tren nhieu bang (hinh 2.22).

V( du 2.6 :

- Tao mot query moi ten thesinhvien dua tren ba bang SINHVIEN, LOP,KHOA.

- Access tv tim thong tin tren hang "mot" dua tren khoa ngoai tren hang "nhieu",

30

Hinh 2.22. Tao mQt query dua tren nhi~u bang.

6. T~o truy ~n tfnh toan (hinh 2.23)

- Tao mQt truy van moi va nguon dli lieu.

- Chuyen til truy vdn chon thong thirong sang truy van tinh toan bang

each chen bi~u urong totals tren thanh cong cu hoac View -- totals

Hinh 2.23. Man hlnh thi~t kc! truy vAn tinh toano

Tren dong total c6 th~ chon cac gia tri :

+ Group by : dung d~ chi ra tnrong nao la tnrong phan nh6m. + Sum: Tfnh tbng.

+ A vg : Tfnh trung birth cQng.

31

+ Min: Tim gia tri nho nhdt. + Max : "Tim gia tri 100 nhdt. + Count: £)~m.

+ StDev : TIm d91~ch chuan. + Var : TIm phirong sai.

+ First: Tim ban ghi dftu tien. + Last: Tim ban ghi cu6i cung.

+ Expression: MQt bi~u thirc trong tinh toan di kern voi rnQt tinh toan bang ham thu vien khac.

+ Where : £)i~u kien loc cac ban ghi tham gia vao tinh toano Vi dl:/.2.7:

Vi du v~ cac truy vdn tinh toan : Tim tbng s6 sinh vien""nu trong tnrong (hinh 2.24).

1. Xet tnrong NAM voi di~u ki~n 13. sinh vien nu.

2. Xet tnrong MASV dA tinh tbng sA sinh vien thea rna sinh vien,

H"inh 2.24. Vi du ve tim tAng sa sinh vien ml trong tnrong. 7. Truy van cO tham sO

Khi thuc hien truy vdn, man hinh nhap gia tri tham s6 se xudt hien (hinh 2.25) :

32

Truy vAn se thuc hi~ v&i ghl tri tham s6 duQ'C nh~p' tit ban phim cho k6t qua ho~c di~u kien.

f)~ bien mQt d6i nrong nao thanh troy vftn, chi cAn bao no boo c~p ngoac vuong.

Hinh.2.25. Man hinh nh~p gin tr] tham s6.

Vi d1,l2.B .' (hinh 2.26)

Dua fa danh sach sinh vien mot Iop, gAm rna sinh vien, ho ten, ngay sinh, ten lap. Ma lap nh~p til ban phirn.

Hinh 2.26. Vi d\1 ve truy vAn c6 tham sO. '

8. Truy van v6i d~u k~n (/Qc b~t

, Trong di~u ki~n cua troy vAn co th~ xuftt hien nhtrng gia tri cua cac di~u ki~n xuat hien trong form nao do. Thay cho vi~c oh~p th6ng tin qua tham s6, co the chon thong tin d6 tren mQt hOp combo cua form.

VI dJ:l2.9.'

Dua ra danh sach sinh vien rnQt lop, g6m rna sinh vien, ho ten, ngay sinh, ten lap. Ma lap duoc chon tren form (hloh 2.27).

Troy van duoc thiet kS (hioh 2.28)

3--GTACCE&lfD

33

Hinh 2.27. Fonn de lua chon miilap.

Hinh 2.28. Troy vdn duoc thi~t k~ thea VI du,

2.4.3. Truy vin bO sung (Append Query) (hinh 2.29)

- Khi dn b6 sung du li~u vao CSDL, c6 th~ thuc hien thea cac each sau :

1. B6 sung vao ban ghi dan le : M6' bang hoac form, them mQt s6. dong.

2: B6 sung hang 10<,1t ban ghi thea mot 'di~u kien nao d6 (chang han sao luu hay nhap dfr li~u til bang trung gian) : 4P troy vdn.

- D~ t<,10 truy v§.n b6 sung moi bing QBE phai tuan thea trinh tl,f sau :

1. T<,1o truy vdn moi : tai hOp thoai Show Table chon bang cho dfr li~u.

2. Tren man hinh thi~t k~ truy v§.n chon moi, chuyen sang truy vdn b6 sung bing each chon menu Query ~ Append Query.

34

Hinh 2.29. Man hinh thiet ke· truy vAn be sung.

Vi du 2.10:

Dua vao bang LUU thong tin v~ sinh vien v6i rna sinh vien nh{ip tir ban phfm (hinh 2.30).

HiM 2.30. Vi du v~ truy vAn b6 sung.

2.4.4. Truy van 10,1 be) (Delete Query) (hinh 2.31)

N~u 10,," be mQt s6 ban ghi dan Ie, co th~ loai bo tnrc tiep tren bang hoac form. Troy vAn 10,," b6 diroc sil dung khi 10,," bo cac ban ghi thoa man .di~u kien nao do, co th~ duoc thiet k~ bang QBE hoac bang SQL d~ chay tu dQng trong chuang trinh.

Cdc buac thuc hifn :

35

1. Tao troy vAn moi ----i- Tren man hinh Show Table chon cac bang lien quan d~n troy vdn. D6 la bang eAn loai be va cac bang lien quan d~n di~u kien.

2. Tren man hinh thi~t k~ troy vdn chen ngAm dinh ----i- menu Query ----iDelete Query.

Hinh 2.31. Man hinh thiat kS truy van IO{li bo.

Vi du 2.11 :

Xoa be thong tin v~ mQt sinh vien voi rna sinh vien nhap til ban phfrn (hinh 2.32).

36

Hinh 2.32. Vi du vi! truy vAn IO{li bo,

2.4.5. Truy vin thay dOi (Update Query) (hinh 2.33)

Vi~c thay dbi cac gia tri don le duoc thuc hien true ti~p tren bang hoac form.

Khi thay d6i nhieu ban ghi thoa man di~u kien nao d6 dn tao truy vftn thay d6i.

Cae buoc thi€t k€ truy vdn thay d6i : 1. Tao troy v~n moi.

2. Chon cac bang lien quan : MQt bang cdn thay d6i va' cac bang xuat hien trong di~u kien,

Tnroc khi thirc hi~n truy v§.n thay d6i cAn xern tnroc nhtmg ban ghi dn thay d6i xem S1,l thay d6i d6 c6 phu hop voi yeu du kh6ng.

Chu y d~n viec xac l~p tuy chon Cascade Update d~ tranh nhfmg thao tac thay d6i kh6ng d.n thiet.

Vi dlj 2.12 :

Ubi rna lap 001 thanh 010 va thay ten lap thanh ten "LOp chat hrong cao" (hinh 2.34).

Hinh 2.33. MAn hinh thi8t k! truy vAn thay d6i.

Hinh 2.34. Vi du v8 truy van thay d6i.

37

2.4.6. Truy van Crosstab

- Truy van Crosstab diroc dung trong cac bi~u m~u thfmg ke. D~c trung quan trong cua truy van nay la c6 th~ chuyen dii li~u trong bang thanh tieu d~ c¢t.

- De tao truy v.1n Crosstab co the dung Wizard hoac nr thiet. ke. * Niu dung Wizard, phai thuc hien cac buCrc sau :

1. Chon hang hoac truy van nguon dii li~u.

2. Chon cac tnrong d~ nhom thea hang.

3. Chon cac tnrong de nhom thea c¢t.

4. Chon gia tri de hru a giao cua m¢t hang va ffi¢tC¢t, a do eo the dung cac ham thu vien d~ tfnh toano

* fliu t~ thidt Td pJuii thea cac butrc sau :

1. Tao truy v.1n moi.

2. Chon bang hoac truy van nguon.

~ .

3. Chuyen sang truy van Crosstab (Menu Query).

Vi dI,J 2.13:

M¢t truy vdn de tfnh tAng nam nu trong timg lap, chuan bi nguon dii li~u cho bao cao v~ ty l~ nam nu trong tung lap. Man hinh thiet kS truy van cho ket qua tren hinh 2.35.

HiM 2.35. Vi du va truy vAn Crosstab.

38

2.5. T~OTRUYVAN BANG NGON NGU SQL

Query - By - Example (QBE) va Structured Query Language (SQL) Ht hai ngon ngft chuan cong nghiep ndi tieng d~ vAn tin tren CSDL quan h~. tr u di~m cua QBE Iii d~ sir dung nho vao giao di~n db hoa con Uti di~m cua SQL Iii tfnh phd bien cua n6 trong th~ gi6i CSDL quan h~.

QBE va SQL hoan toan c6 th~ trao ddi qua lai chi tm mQt s6 tnrong hop ngoai l~. Vi v~y, nfun vtrng duoc cac phep toan tren mQt ngon ngu (chien, chon, sap x~p, k~t n6i va tnrong tinh toan) thi bin ngon ngu con lai cling tUOTIg t1,1'. Trong thirc t~ Access cho phep chuyen mQt query qua lai gitra QBE va cau lenh SQL.

- each t(10 mot query su dung SQL.

l. Tao mQt query moi nhung d6ng cira s6 "show table" rna khong chon bang nao.

2. Chon View> SQL d~ chuyen sang SQL editor.

PivQl:Table View Pi~otChart View

F4

,loin Properties

Hinh 2.36. each tao mQt diu l~nh SQL.

- M(Jt diu l?nh SQL ph6 biin c6 dang :

SELECT MASV, Ho, Dem, Ten FROM SINHVIEN WHERE Ho = "Nguy~n"

Bao g6m b6n phdn :

1. SELECT <fieldl , field2, ... .fieldre- chi ra tnrong nao duoc chon (phep chieu)

39

2 .... FROM <table> ... Chi ra bang (cac bang) la nguon cua query.

3. WHERE -cdklc- AND/OR <dl<2>, ... , AND/OR <dkn> Chi ra di~u kien hay cac di~u kien ban ghi phai thoa man

4. Diu chim phAy (;) TAt ca cac cau l~nh SQL duoc k~t thuc bAng dAu cham phay. N~u quen, Access se tv di~n vao.

- M¢nh l¢nh WHERE phuc hap

Cac l~nh AND, OR v~ NOT duoc sir dung trong menh d~ WHERE d~ t<;10 cac di~u kien phirc hop.

Vi du 2.14 :

Tim ho ten, Ngay sinh cua cac sinh vien nu, ten bAt dfiu bang chu cai

SELECT DISTINCTROW ho, dem, ten, ngsinh FROM sinhvien

WHERE Not Nam AND Ten like "[A,D,C]*";

Chu y : cac tnrong van ban dn d~ trong dAu nhay kep, N 'gtm ngil thap tac dillifu

1. B6 sung

INSERT INTO <Ten bang> [«danh sach ten cQt»] VALUES (<Danh sach gia tri>

Vi du :

INSERT INTO LOP

VALUES ("220" ,"TAl NANG", "F20" ,"K44") 2. Lo<;1ibo

DELETE [«danh sach cac tnrongc-j] FROM <Ten bang> WHERE <di~u kien»

2.6. cAu H6I ON T!P- BAI T~P

SlY dl,lng t(lp Quanlysinhvien.mdb vCra te-o trong chU'ong 1 1. T90bang

- T<;Io cac bang DIEM, LOP, KHOA theo nhU' cac hlnh 1, 2,3 :

40

Gener~ •. :,jo .............

FJeldSIze . FormlIt, .'\; Input M.u!<. -',

caption ' .

Defllult V~lue . ,,;,;e' (·,;""5\1000000" V~Ii~ ~ull' ,. ·.·.Ah.;_ .. ::<~<:, .".:-.:.~;~"

lJaidlitlon Text:;;,i; .• F';~!

$~~~"';C':

.=~reH~:ye5

Hinh 1

Hinh2

41

. Hinh 3

- Thuc hanh gan khoa chfnh, SlI' dl,mg autonumber

- Dung cac thuec tfnh khac nhu caption, lookup

2. TE;JoQB£

- T;;IO mC)t query sap xttp bang SINHVIEN dB. tao trong nC)i dung phan Iy thuyet theo MASV nhu hlnh 4 :

Hinh4

42

- T<;Io mOt query ket n6i bang SINHVIEN va LOP. Khi nhap mot MASV, mol thOng tin lien quan nhv ten sinh vien, ngay sinh, ten lop, ma lop .. se tl,l' xula hi(m. Query c6 d<;lng nhU' hlnh 5 :

HinhS

- Nh';'p m¢t s6 dCr li~u vao bang SINHVIEN va bang LOP

(Yeu cau nhap m¢t s6 dCr Ii$u vao bang nham rnuc (lfch (16 cho ngU'ai Slt dl,lng thay reS (lU'QC Sl,l' phoctap cua cOng vi/ilc nhap dCr Ii$u true tiep. GOng vi~ nay se dan gian khi Slt dt,mg form (Ii! nhap),

43

Chuang III

THII:T KI: BIEU MAu vA BAo cAO

3.1. cror THltU BrEU MAU (Form)

Form Iii giao dien gifra ngirot sir dung va dii li~u ; cho phep ngiroi Slr dung vi~t chuang trinh di~u khien each hi~n thi liii li~u trong bang/query, dbng thm kiem soat nguoi sir dung trong cac thao tac nh~p va hi~u chinh dfr li~u.

Cling gi6ng nhu

query, form khong chua dfr lieu rna chi la "cira s6" thong qua do ngiroi sir dung nhin thAy dfr lieu tren cac table hay query. quan h~ gina form, table va query diroc minh hoa trong hinh 3.1.

.G..-------""""--------forms

(bi~u maul

queries (1ruy vlm)

tables (bang)

Courses Departments Employees

Hinh 3.1. Quan he giua Form, Table va Query.

Form thuong dung trong cac tnrong hop sau : - Thi~t k~ man hinh nh~p dfr lieu.

- Thi~t k~ menu.

- Thit~t k~ cac man hinh tra CUll thong tin.

- Tao cac man hinh gioi thieu, tro giup,

MQi d6i nrong xuat hien teen form duoc goi Hl di~u khien, f)i~u khien

diroc chia thanh 3 loai chinh :

- f)i~u khien bi buoc : Chi xufit hien tren form c6 nguon dfr li~u.

- f)i~u khien khong bi buoc : Khong gfut voi nguon dfr lieu,

- f)i~u khien tinh toan diroc : La di~u khien khong bi buoc.

Cae bwc t(lO form mO'i (hinb 3.2) :

1. f)~ thi~t k~ mQt form moi, til cira sf, Database chon Forms.

44

2. Chon New d~ rna hQp thoai New Form. Trong hQP thoai New Form:

+ Chon mQt trong hai kieu thiet k~ : Design View hay Form Wizard. + Chon cac bang hay truy vAn rna bieu mau dua tren.

Xay dung bieu 'm§u dua tren cac Choose the table' or query wt-e ---'_':':;""_--I bang hoac truy the object's data CQITIM fran: v~n, du oc c h i ra

trong danh sach

I!!!I
Il!!P Queries
.
• RepOrts
~ P~es
Macros create ~ new Fcrm Wthout: . using a Wlz¥d. .:

Forms til cira s6 Database

....

Hinh 3.2. Trinh tV' tao Form moi, Tren hQp thoai nay c6 th~ chon mQt trong hai each :

- Dung Wizard: chon 1 trong 6 Iva chon cu6i trong d6 :

+ Form wizard : thi~t k€ form don mQt each tu dQng. Ngiroi su dung co th~ quyet dinh m¢t s6 d~ tnrng cua form: chon bang, chon tnrong, chon each hien thi tnrong, hinh thirc trinh bay, form ...

+ Auto form: nguoi su dung chi duoc chon bang hay truy vAn nguon, Access se nr d¢ng thuc hien rnoi thao tac tiep theo d~ cu6i cung tao diroc form dang Datasheet (bang), Columnar (phieu nh~p dfr li~u) hay Tabular (Bang khong ke nhung c6 trang trf nhu form dang Columnar).

+ Chart Wizard : tu dQng tao bieu d6 so sanh dfr lieu trong CSDL.

Duoc phep quyet dinh m¢t 56 tham 56 : thong tin a true tung, true hoanh, hinh thirc cua bieu d6.

+ Pivot Table Wizard: Wizard cho phep dira bang tfnh Excel, qua do co th'~ tfnh toan ngay khi lam vi~c teen form.

45

- Kh/mg dung Wizard: Chon muc Design View. Man hinh thith k~ form c6 dang nhu hinh 3.3, trong d6 xudt hien hai cong cu luon ducc dung trong thiet k~ form lIt toolbox (hinh 3.4) va fieldlist. Vi du tit cira sa New Form, chon Design View sau d6 chon bang SINHVIEN.

icon.

Danh sach ten tnrong chi ra cac tnrong trong bang hoi)c truy vAn rna bieu mftu bi buoc

46

fI~~:I!~~·r";.·.y ! ~ a~>I.2& ~

~,

Hinh 3.3. Man hinh thi~t ke Form.

Hinh 3.4. Thanh cong cu thiSt ke Form. 3. Luu form voi met ten th~ hien y nghia cua form,

3.2. THIET KE cAe DIiu KHIEN TREN FORM 3.2.1. Vi tri cua eae dltu khi6n

Khi thi~t k€ cac di~u khien elm quan tam d~n vi trf cua chung tren form. Man' hinh thiet k~ form c6 dang nhir hinh 3.5.

Hinh 3.5. Man hinh thiet k~ Fonn.

Form HeaderlFooter: header chua tieu d~ chfnh, footer chua nut lenh, Page Header/Footer: header chua tieu d~ cua trang.

Detail : chua nQi dung bieu mau.

3.2.2. Can chinh eac diAu khl6n

- Sao chep, cAt dan, xoa cac di~u khi~n (tuy nhien chi voi mQt s6 loai nao do).

- Danh dau cac di~u khien

-+ Danh elfiu theo cQt

+ Danh dau theo hang + Danh dau theo khung

+ Danh dau cac di~u khien rm rae

- Can chinh cac di~u khien :

+ Giang thiing : Tir menu Format chon Align (co 1 trong 4 hra chon :

Top, Bottom, Left, Right)

+ Kicn ca : Tu menu Format chon Size

47

+ each iJJu : Til menu Format chon :

Vertical Spacing: khoang each theo chieu doc Horizonal Spacing: Khoang each thea chieu ngang + Nhan h6i : Tu menu Edit chon Duplicate.

3.2.3. £>i@u khllm hQp van ban (Text box)

- HQP van ban, la di~u khi~n quan trong nhat tren form.

- Bay la loai dieu khien eo th~ thuoc ca 3 loai : bi buoc, khong bi buoc va tfnh toan duec.

+ Di~u khien khong bi buoc : la di~u khien nam trong form khong eo nguon dfr lieu. Nhu v~y no dung d~ nhap dfr Ii~u vao mot bien nhc su dung trong tfnh toan hoac d~ lam trung gian khi nhap dfr lieu vao bang. D~ tao di~u khien dang nay keo tu Toolbox vao,

+ Di~u khien bi buoc : n~u di~u khien loai nay thi form phai co nguon dfr lieu. Hien Fieldlist d~ hien thi danh sach cac tnrong, Keo truong tir Fieldlist van form, ta co Textbox bi buoc voi tnrong duoc chQTI.

+ Di~u khien tfnh toan duoc : Neu la dieu khien tfnh roan thi tnroc h~t tao nhu mot dieu khien khong bi buoc sau do nhap cac bieu thirc vao :

Chfnh 0

dieu khien

• Thuoc tfnh Control Source cua di~u khien, i:J day eo th~ goi Expression Builder d~ tu dong xfiy dung bieu thuc.

- Cac thao tac :

+ V6i dieu khien bi buoc. Cac biroc thuc hien nhu sau (hinh 3.6) :

Access sir dung thuoc ttnh ten cua

nhu nhan m~c dinh cho text box.

De di chuyen m~t d6i nreng va nhan cua no, keo tlim cua d5i nrong (can tro tra thanh ' mui ten trang), D€ di chuyen chi d6i nrong hose nhan, keo len phfa tren ben trai (con tr6 tra thanh hlnh ban tay

trongdanh sach ten tnroo!!;

Hinh 3.6. Cac buoc thuc hi~n d~ tao ra dieu khien hQp v~n ban textbox,

48

• Chon MASV trong danh sach ten twang. Access su dung thuQc tmh ten ella twang nhu nhan mac dinh cho Textbox.

• Keo tnrong sang vito trong muc chi tilh cua form.

+ f)i~u khien khong bi buec ("unbound") : chi dn chon di~u khien Textbox tren thanh cong cu sau d6 keo n6 len form.

- Thuoc tinh cua di~u khien Textbox (hinh 3.7) (chon hop van ban fbi

nhdn chuotphai rna hQP thuoc tinh van ban Properties) :

• Decimal Places: dinh dang s6 cua dfr li~u .

• Default Value: gia tri m?c dinh .

. Input Mask .dinh dang nhap lieu,

• Validation Rule: quy t~c quy dinh kiem soat dil li~u nhap vao.

• Validation Text: thong bao,

• Cac thuoc tinh khac 'quy dinh khu6n dang cua di€u khien nhu mau n~n, font chit, each can l~ (trai, phai, ... ), thanh cuon (Scrol1Bar).

Chon dOi nreng rna ban muOn xem thuQc tlnh. NhAn chuQt phai de menu tha xu6ng

Cac thul)c tfnh 'dUc;1C chia thanh 4 nh6m. Xem tAt ca cac thuQc nnhchon All

, ,,' cb.onge To •

Tali. Order ...

Chon Properties -'--~--'--I de hi~n bang

Font/Fore Color property

Hinh 3.7. Bang thuQc tfnh cua Textbox,

4·GTACCE&UU

49

Diem khac biet gifra mQt hQP van ban khong bi bUQC va bi bUQC d6 lA thuoc tfnh Control Source cua hQp van ban hi buoc diroc gan cho ten tnrong

rna n6 rang buQc.· .

3.2.4. Nhin (Label)

- Nhan diroc sir dung lam tieu .d~, chu giai ... Go nQi dung cua nhan vao trong hQP. Khi soan thao nhieu dong. tren nhan sir dung t6 hop phfm Ctrl Enter de xuong dong gifra chimg.

- Hinh thirc trinh bay tren nhan : Font, mau s~ ...

- Viec t<;10 va chinh sua nhan tren bieu mAu duoc th~ hien tren hinh 3.8.

Chinh kfch thuac cua nhan ••. phu hqp v6i chQ trong nhij.n

Hinh. 3.8. Tao va chinh sua nhan tren form.

3.2.5. £)iI~u khlin anh (Image)

- Trong Access, vi~c di~u khien anh c6 the la mQt trong ba 10<;1i sau :

+ Image: kfch co nho, khong d6i khi xem 6 che de;, Form View, dung de trang trf.

+ Bound Object Frame, OLE Object : dung de nhap anh, a che de;, form view thay d6i duoc.

+ Unbound Object Frame: c6 th~ thay d6i ache dQ Form View, dung . db nhap anh khi co nut nh~p anh,

50

I~~ ."

- Khi dung diau khien anh cdn chri y cae thuoc tlnh sau : + Clip :. cho phfin anh vira voi khung.

+ Size Mode: ch~ dQ ~t kfch tmroc anh,

+ Special Effect: cac hi~u ling anh ~cbi~t.

3.2.6. HQp kl6m tra (Check Box), nut 11/8 chen (Option Button), nut b,t tit (Toggle Button)

- N~u di~u khien bi buoc, no luon luon diroc giin voi truong kieu Yes/No. Tuy yeu d.u thuc t~ rna chon kieu nut cho thfch hop. Sau khi da chon kieu nut thich hop, keo tnrong ki~u YesINo til Field List vao form,

- Neu di~u khien khong bi bUQc : quan dim d~n thuQc tinh value -110 d~ l~p trinh xu ly su kien,

3.2.7. Nh6m 1118 chQn (Option Group)

- Nhom lua chon la t~p hop gam nhiau diau khiBn thuoc mQt trong ba dang : Option Button, Check Box, Toggle Button.

- Neu la dieu khienbi bUQc, no dung d~ kiern soat saocho dfr li~u nh~p vao mQt tnrong chi co the nh~n mQt trong mQt s6 it gia hi.

- £)~ tao nh6rn hra chon c6 the chon mQt trong hai each : dung wizard va khong dung wizard

+ Dung Wizard, cAn xac dinh cac thong tin sau :

• Chon cac nhan

• Gia tri ngarn dinh

• Gia hi ling vo; tirng muc chon - cbu y gia tri ngfun dinh la 1,2,3

• Ki~u nut

• Ki~u khung

• Ten cua nh6m

+ Kh/mg dung Wizard

• T~t Control Wizard

• Chon .Option Group

51

• Chon tnrong ten Fieldlist dua vao form - MQt khung xuftt hien

• Chon ki~u nut - dua vao khung. Khi do phAn ben trong khung se d6i mau

• Neu gill tri nh~p vao tnrong diroc chon la khac 1, 2, 3 ... thi phai d6i cac gia tri d6 tai thuoc tfnh Option Value.

- N~u di~u khien Ul khong bi buccthi lam nhu each tao nh6m hra chon khong dung wizard, chi c6 di~u la khong keo mrong vao (thuong form cling kh6ng c6 nguon dfr lieu), Sau khi tao di~u khien xong, din err vito gill tri cua cacmit hra chon rna I~p trinh xu 19 su kien.

3.2.8. HQp litt ke (List Box) va hQp k~t hQ'p (Combo Box)

HQp k~t hop (hinh 3.9) va hQP li~t ke (hinh 3.10) thuong duoc sir dung trong cac truong hop sau :

- Chon mQt trong nhi~u gia tri d~ nh~p vito tnrong (teen 7 gia tri),

- Chon trong mQt danh sach diroc hien thi tren form.

~p nh;t cal: thong tin ve mOt lOp trong khoa

r.:; lap ~h"t Thong Tm lop "~; ~ •

Hinh 3.9. Vi du v~ hQp k€l hQP.

Hinh 3.10

VI du v~ hQp liet ke.

52

Su khac nhau gilla h{lp li~t ke va h{lp k~t hop : co th~ them gia ttl khac (khong co trong danh sach gia ttl) vao h¢p k~t hop trong khi khong th~ them vao hQP li~t ke. -

Tao List box hoac Combo box - Dung Wizard

1. B~t Control wizard.

2. Chon di~u khien tren Tool Box (List hoi,lc Combo).

3. Keo vao form, Wizard khoi dQng.

4. Chon nguon dtr li~u : Ifry tit bang hay danh sach gia tri (vi du hinh 3.11).

Hinh 3.11. Chon ngu6n dii" li~u cho List box sir dung Wizard.

5. Chon bang hoac danh sach gia tri nguen,

6. Chon cac truong hien trong h{lp.

7. Quyet dinh co hien tnrong khoa khong?

8. Chon tnrong nhap dfr lieu (Form phai co ngulln dfr li~u. Truong nhan dfr li~u la mQt trong cac tnrong cua bang ho~ truy vfut nguen dii lieu).

9. Ten hQP.

53

- Kh6ng dung wizard

1. T~t Control Wizard.

2. Chon bieu nrong, keo tnrong vao form (n~u la dieu khien b] bU9C).

3. M& thuoc tfnh cua List/Combo (hlnh 3.12).

4. Xac dinh cac thuQc tfnh sau :

-Row Source Type: Chon Table/Query hoac Value List

- Row Source : Ten bang hoac truy vAn neu Row Source Type la

Table/Query. N~u Row Source Type la Value List thi d~ danh sach gia tri phan each bang cldu ; (co the la cldu khac n~u d~t hili & Regional Setting. Cling co th~ la m<)t l~nh Select cua SQL n~u muon hi~n th] dfr li~u trong hQp hay nhap vao tnrong gia tri cua nhtrng cQt bftt kyo

Hinh 3.12. Bang thuQc tinh cua List Box.

• Bound Column: cQt eho gta tri

• Column Count: 56 cac cQt sl!! hien dfr li~u trong hQP (tfnh ttr cQt 1) .

• Limit to list (yes/no) : Dung rieng cho Combo. Khi them moi gia tri vao, gia tri sl!! diroc c~p· nh~t vao nguon n~u thuoc tfnh nay d~t la No. Tuy . nhien kh6ng them gia tri nay vao danh sach ngubn cua hQp Combo.

3.2.9. Nut I~nh (Command Button)

Khi ta kfch heat nut l~nh, M quan tri CSDL sl!! thuc hien mQt cong vi~c nao do. Cong ~i~c do co th~ dan gian la rna 1 form, cling co th~ la tfnh di~m trung binh cua sinh vien ca mQt tnrong dai hoc,

54

- Taonta lfnh bdng Wizard (hmh 3.13) 1. Chon linh vue cua nut l~nh

, • Di chuyen con tro ban ghi

• Thao tac tren ban ghi : them xoa, ghi ...

• Thao tac tren form

• Thao tac tren baa cao

• GQi 1 irng dung/ Thoat khoi irng dung,

• Khac : Troy v~n, bang

2. Chon hanh dong va tham s6 cua hanh dong.

3. Hinh thirc : chon chir, hinh anh tren nut l~nh.

Record

To Frst Record To Last Record Go To Next Record

Command Button Wlzatd ' , ,

Hink 3.13. Man hinh t;;1O Command Button Wizard.

- Khong dung Wizard

1. TAt Control Wizard

2. Chon nut I~nh, dira vao form

3. 4P trinh xu 19 S11 kien tren nut I~nh : S11 kien quan trong nhat IS. Click (Macro' hoac Code).

3.2.10. £)i6u khiin Tab

£)i~u khien Tab duoc sir dung khi dn nh~p dfr li~u cho nhieu bang doc l~p khac nhau tren cling mot form hoac khi form nh~p du li~u qua dai.

55

3.2.11. ThuQc tinh eua form

Vi du : MQt form quan If sinh vien (hinh 3.14)

Hinh 3.14. Vi du v~ form qulin 19 sinh vien, Cac thuQc tfnh cua form gAm:

- Thuoc tinh. v€ hinh thuc trinn bay cua form

1. Default View: Dang hien thi ngfun dinh cua form.

2. Scroll Bars: Cac thanh cuen d~ chuyen di~u khien len xuong.

3. Record Selector: Miii ten chi ra ban ghi hi~n hanh,

4. Navigation Button: Cae nut d~ dich chuyen ban ghi,

5. Dividing Lines: Duong phan chia cac bQ phan cua form.

6. Picture: Hinh anh tren toan form.

7. MinMax Buttons: Nut cho phep thu nho, ph6ng to kich cO' cua form.

- Thuoc tfnh lienquan din dii li~u cua form

8. Records Source: Ten truy vAn, bang nguon dfr lieu hoac van ban SQL cua nguon dfr li~u.

9. Allow Filters: Cho phep IQc dfr li~u hien tren form.

10. Allow Delections : Cho phep xoa cac ban ghi tren form.

56

11. Allow Edits : Cho phep chinh sira dii li~u tren form.

12. Allow Additions: Cho phep b6 sung banghi tren form. - Cdc s¥ kifn trenform

- Cdc thu9c tfnh khac

13. Menu Bar: Menu hi~nkhi rna form.

14. Shortcut-Menu : Menu hi~ khi nhAn chuQt phai,

3.3. BIEU MAU LIEN QUAN D~N NHIEU BANG - BIEU MAU CHiNH PHV

De dam bao thoa man t8ida yeu cc1u cua nguoi sir d\mg,·cc1n thi~t ke cac form nh~p dfr li~u sao cho c6 hinh thnc giarig nhu cac bang bieu rna ngtroi Slr dung da quen thuoc. Access da dira ra form chfnh phu d~ c6 thS nhi,lp dii

lieu d6ng tho; vao nhieu bang. "

Mot form chfnh dang ,,cot (Columnar) voi mot Form phu dang bang (Tabular) 13. each dS tiu! hien thong tin tren cac bang c6 quan h~ mot nhieu, Vi du Form chinh hi~n thi thong tin 'timg khoa, khoa hoc, lop, mon thi, con Form phu chua cit ca cac thong tin v~ sinh vien nhu di~m men hoc d6.

Vid1,l3.1 :

Form ci,lp nh~p thong tin v~ diem cho sinh vi~n (hinh 3.15).

Kat n6i duoc tao giila form ehinh vA form phu

Subform lA cac form ducc

trinh bay thanh bang rieng hien th] thong

tin nr bang DIEM

Hinh 3.15. Vi du ve Form ehfnh phu "C~p nhat cac thong tin ve diem ehosinh vien".

57

Co nhieu each d~ tao Main FonnlSubForm, each ph6 bien nhAt duqc gioi

thi~u dum day:'

1. Tao Form chfnh dang cot (columnar) va Form phu dang hang(tabular) rieng biet ;

2. Keo Form phu len Form chinh ;

3. Kiem tra m6i lien k~t gifra hai Form:

3.3.1. Tflo Form chinh

1. Sir dung Wizard d~ tao rhQt Form dang columnar dua tren bang Courses

2. S~p x~p lai ea~ tnrong sao eho hop 19

3. Luu lai Form dum ten frmDIEM

3.3.2. T~o Form phlJ

1. Sir dung Wizard da tao Form phu (hinh 3.16).

DIEM l!yalable FieIds:<;' ... "

LAN

H'inh 3.16. T;;LO Sub Form dung Wizard.

2. Cac Subform tao dung Wizard thuong phai diroc tinh ehinh d~ giam khong gian chiem dung cua cac tnrong.

+ Chon dang Form la tabular.

58

+ Khong dn d~t tieu d~ eho Form vi n6 se duoc nhung vao Form ehfnh + Chon Modify the form' s design d~ sang eh~ dQ thi~t k~.

3. Luu Form dum ten sfrmDIEM va d6ng lai.

3.3.3. lien kat hai Form

Trong ph~n nay, chung ta se quay lai Mainforrn va keo Subform tit cira

sA Database len vi trf thfeh hop tren Mainform ..

1. M6 lai Form chfnh trong eh~ dQ thi~t k~.

2. Keo Form phu tit cira s6 Database len Form ehfnh. Chi~u rQng cua di~u khien Subfonn (cua s6 trang) nr dQng gian d~ chua hSt Subfonn.

- 3.4. GI01 THItU REPORT WIZARD

Bao cao dung d~ in ra gi~y nhimg thong tin kSt xu~t tit CSDL theo khu6n

dang, yeu cfru cua timg co quan .

Cac bao cao co th~ dua tren ca table hoac query.

Bao cao co nhieu dang nhung thong dung nhfrt Ia cac dang sau : ~ Bao cao dang van ban

~ Bao cao dang bang dan gian

~ Bao cao tmrc hi~n mQt s6 tfnh toan, th6ng ke ~ Bao cao dang bieu db, nhan thu ...

Tao khuon dang

cua bao cao ban dAti 1. Chon muc Report tren cira s6 Database.

2. Chon New - XuAt hien man hinh tao bao cao moi (hinh 3.17).

Hinh 3.17. Man hinh tao bao cao rnoi,

59

3.4.1. T\IIo bao eao blng,wlzard

. Co th~ chon m¢t trong cac lira chon sau : • Auto report

Tu d¢ng dira ra bao CaD khi da chon nguon dfr lieu. Khong diroc chQTI thong tin nao .

.. Report Wizard :

Nguoi sir dung phai lua chon cac muc sau :

1. Chon nguon dfr li~u (bang hoac troy v§.n).

2. Chon tnrong dung trong bao cao.

3. N~u mubn tao bao cao phan nhom thl xac dinh tnrong cdn phan nhom.

4. SAp thing (neu cftn).

5. Dang va ki~u, trinh bay cua bao cao, • BaD eao d(;mg nhan

- Dung wizard d~ xac dinh kfch cu, cac van ban chu y~u 1fty thong tin nr trongCSDL

- Cac bmrc co' ban :

1. Xac dinh kfch cO' va each b8 trf cac nhan tren gidy.

2. Font chi} ngAm dinh.

3. Cac tnrong can l!y thong tin.

4. Truong cdn sAp x~p.

5. M& thi~t k~ cua bao cao dang nhan, di~u chinh lai m¢t 56 di~u khien cho phu hop yeu du thuc t~ .

• BaD CaD d~ng bi9u <16

1. Tao bao cao moo va xac dinh ngubn dfr li~u.

2. Chon Chart Wizard.

4. Chon cac tnrong xudt hi~n trong bieu db.

5. Chon dang trinh bay cua bi~u db.

6. SAp d~t cac thong tintren cac true toa d¢.

7. Tieu d~ cua bieu db.

60

3.4.2. Cac van dlt khac lien quan dAn thiAt kA bao cao 1. Cae thU¢c tinh chung cUa bBD caD

Bao cao co cac thuoc tfnh giAng bieu m§.u nhu cac thuoc tfnh lien quan d~n kich cO', anh,

Ngoai ra bao colo con co mQt 56 thuec tfnh d~ biet : Page Header I Footer: tren moi trang khong co 6 trang co Report Header, khong co 6 trang co Report Footer, khong co ca 6 hai trang do.

2. Cac 51)' ki~n tten bBo cao

- On Open: khi rna Report

- On Close : khi dong Report

- On Activate: khi cira 56 chua Report tro thanh ella s6 hien hanh

- On Deactivate : khi ella s6 chua Report khong con la cira so

hien hanh

- On No Data : nguon dfr lieu khong co ban ghi nao, t6t nh.1t la khong nen in bao cao do.

3.ln bBo cao

- Kich hoat bi~u nrong may in hoac dung menu File 0--0-.) Print

- N~u dung hQp thoai Print co th~ in ra t~p dang .PRN va co th~ dem in

tai may tfnh b.1t ky co nAi voi may in du tren may do khong coo Access.

J

3.5. THIET KE cAe THANH PHAN eVA BAo cAo

Chon muc Design View. Man hlnh thiet k~ bao cao hi~n ra voi 3 che dQ

song song t6n tai, dola :

1. Design View: thiet k~ bao cao.

2. Print Preview: truong hQP xem ban in bolo cao.

3. Layout Preview: Xem hinh tlnrc cua bao cao khi in ra.

Tren man hinh thi~t k~ bao cao ding luon luon d~ hai cong cu la toolbox va fieldlist.

Nhin vao man hinh thiet k~ bao cao (hinh 3.18) ta thAy c6 cac ph.1n chmh : + Detail: MQt ban ghi mQt lan, dung d~ chua dfr li~u cua bao cao,

61

+ Page Header: tiSu d~ dAu cua trang, l~p mQt trang/mQt Ian. + Page Footer: s6 trang, dung ham P~ge.trang hien.thci. .

dung ham Pages ding s& trang e "Trang"& [page]&"/"& [pages] ngay l~p, tieu d~ duci

+ Report Header: Chua tieu d~ chung cua toan bao cao, + Report Footer: Chua dong tbng cQng.

+ Group Header! Footer: Dung cho cac bao cao duoc phan nhom.

_~ ReJlu~l_: ~eporl _ _ _ __ _ __ ~~·a; -

x

Hinh 3.18. Man hlnh thi8t k8 Report.

3.5.1. BaD cao d,ng van ban

- f)~c trung : Van ban trQn IAn voi thong tin trong CSDL, vi v~y toan bQ thi~t k~ nam trong phfm Detail'

- Trong bao cao tlurong bao g6m cac diSu khien :

+ Nhlin : chua cac doan van ban voi font chfr theo ·ySu du thirc t~. + Textbox : thirong bao g6m 2lol,ti : bi bUQC va tfnh toan ducc.

+ Anh : C6 th~ bi buoc hay khong bi buoc.

Vi du 3.2:

In danh sach sinh vien trong mQt lap. Man hinh thi~t k~ bao cao c6 dang nhu hinh 3.19 :

62

't;;l Repott1 : Report "',:

--- ----- - -- ~ ~~-~-~-- ------

.. , - - - . . .. .. ~ . . . . . . . . . . . . . . . . . . .. or"....................... ::: ~ : : . : : : : ~ : : : : : : : : : :: .

•••••••••••••••• " + ,",.,'

Hinh 3.19. Man hinh thiet ke bao cao "In danh sach sinh vien trong m{lt lop",

3.5.2. Bao cao (fan gian d,ng bang

1. Tieu de:

+ N~u chi xudt hien & trang dAu : Report Footer. + N~u xudt hi~n teen moi trang : Page Footer.

2 D{,u C¢t : thuong d~ & Page Footer (Chi tnr truong hop dan n6i cac trang thi d~ & Report Footer)

Textbox chua dft li~u nguy2n dang : keo cac dfr li~u xuiit hien trong bang tir Field List vao phAn Detail. Xoa nhan di kern di~u khien. Vi~e din chinh gi6ng nhu din chinh di~u khien teen fOnTI.

- Cac gia tri hung nhau chi xudt hien mQt IAn

- £)~ co cac Textbox e6 d{i eao gi6ng nhau, nen chon each sao chep hoac

tacdong vao thuoc tfnh cua di~u khien,

3. Teo cac Textbox (di~u khien tfnh toan diroc) chua" dfr li~u k4h xuiit dang :

DCounl

DSum DAvg

(<Bi~u thuc>,<MiSn dn tfnh>,<diSu ki~n»

63

DMax

Vi du 3.3 :

Tfnh di~m Trung binh cua sinh vien

DSum("[diem)*[sotrinh)","[Bang Diem Ca Nhan]","[Ian]='I''')IDSum ("[sotrinh)","[Bang Diem Ca Nhan)","[Ian]='I"')

4. Textbox GU'Q'c dung lam trung gian de tfnh gia ttf cho nhO'ng Textbox khac.

5. Vi trf dong t6ng_ ~ng: Report Footer. 6; 56 trang h~c ngay 16p bao cao

, 3.5.3. Bao cao thOng ke

N6i chung ngu6n dli lieu cua cac bao cao nay thtrong la cac truy v~n dang crosstab hoac ~p trung dli li~u vao bang trang trong qua trinh I~p trinh.

,

Ciing c6 ih~ dung cac di~u khien dang tfnh roan duoc chua cac ham thu

vien hoac ham thu vien c6 di~u kien, d6 la :

= SumIAvgIStDevIStDevPIVarIVarP«Bi~u thirc s&» = CountlMinlMax( <Biau tlurc sa»

= DSumIDAvglDStDeviDStDevP1DV ariD V arP( <Bi~u thirc s&>;<Ten ngu6n dfr li~u>; <Di~u ki~n»

3.6. BAo cAo CHiNH - rnu

Ciing nhu tren form, khi tao bao cao voi ngu6n dfr 1i~u> l~y tit" nhieu bang yeu cAu dam bao dung khuon dang th\Ic tS, ngirci ta phai dung bao cao chfnh - phu, vf du phieu xuat v~t tu, lylich nhan vien '"

each tao bao cao chfnh- phu ciing gi6ng nhu tren form, bao gam cac

bu6c sau :

1. Tao bao cao chfnh

2. Tao bao cao phu nhu m¢t bao cao d¢c l~p

3. Mer dang thoi cira s6 thi~t k~ bao cao chfnh va cua s6 Database. Keo bao cao phu nr cira sf, Database vao,

64

4. Me thuoc tfnh cua dieu khien SubReport tren baa cao chinh. Kiem tra su k~t n6i dii li~u qua cac thuQc tinh LinkMasterFields va LinkChildFields

Chu y : cac thuoc tinh LinkMasterFields va LinkChildFields con xu§.t hien ca trong bieu db. (Khong dn tao bolo cao phu dang bi~u db rna nen dung Insert ~ Chart). Do v~y co th~ lien k~t dO li~u a baa colo chfnh va dO li~u U bien dB trong do.

3.7. sAp XEP, PHA.N NHOM TR@N BAo cAo

-MaView

- Chon Sorting and Grouping d~ rna hop thoai Sorting and Grouping

(hinh 3.20)

Vi du 3.4: S~p xep tnrong MASV theo chi~u tang

Hinh 3.20. sap xc!p phan nh6m tren bao cao,

1. Chon tnrong/bieu thirc,

2. Chon thir t\I s~p x~p.

3. Cac thuQc tinh (C6 cAn Group HeaderIFooter hien thi khong),

4. Group On: gia tri d~ phan nh6rn.

ChO cai dAu d~ phan nh6rn (phai quan Him d~n thuoc tinh ti~p thea Group Interval).

5. Group Interval (khoang phan nh6rn) : s6 ky tu cdn d~ pnan nh6rn.

5--GT ACCE&IJD

65

Vi du 3.5:

ANH Group Interval 1 -- & cung nhom 1 AN Group Interval 2 -- (] cimg nh6m 2 AT Group Interval 3 -- & cung nhom 3

6. Keep together Vi du 3.6:

In 5 ngiroi ml)t nh6m nhung chi in 2 nguoi da h~t trang thi chuyen toan bl) sang trang moo (Yes) hoac d~ nguyen (No).

3.S. cAu HOI ON T~P - BAI T~P

1. St} dl,mg Wizard d~ tao Form d~mg columnar cho tat ca cac bang ea tao,

T<;Io mainforrm va subform cho sinh vi~n va di~m thi. Form nay dU'Q'c dung de theo doi di~m thi, 56 IAn thi cua sinh viltn theo lap, khoa va khoa hoc cua sinh viltn (hlnh 1).

Rinh 1 Cac form 6U'Q'c tao theo cac chi dan sau :

- Form chfnh dl,fa trsn query 1<;10 theo bang LOP va bang MONHOC lay vao cac trU'ong nhu tren form

66

- Form phu dl,l'a tren bang DIEM_TG dU'Q'c tao b&i cac tnrang dLFQ'C cho trong hlnh 2 :

Hinh2

2. T<;Io Form danh sach lap, chi nMp cao thOng tin tao danh sach lap (hlnh 3)

~ Tao Danh Sach lop "

.. Nhip thong tin t{lO daoh sach 16'p

Hinh 3

3. T<;Io cac bao cao sau :

- Bao cao : bang dj~m ca nhan, Thong ke diAm oua tung sinh vilm (hlnh 4)

67

Hinh4

- Bao cao CJll in danh sacn sinh vien (hlnh 5)

Hinh5

68

ChucmgIV

L~P TRiNH TREN ACCESS

4.1. L~P TRiNH DON GlAN BANG MACRO - UNG Dl)NG CVA MACRO

Macro ia mot trong 6 dbi nrongco ban cua CSDL (Database) dung d~ lien k~t cac dbi urong cua CSDL lam cho CSDL tre nen linh boat han.

Macro Ja t~p cac thao tac thuong duoc dung trong cac xu iy dfr lieu, duoc chuyen thanh cac hanh dQng va co th~ su dung no thay vi phai viet mQt doan chuang trinh xu iY.

Macro thuong diroc g~n vOi cac str kien cua dbi nrong CSDL. Cac tnrong hop sir dung Macro :

- Khi khoi dQng CSDL.

- Khi kfch hoat nut l~nh hay chon mQt rnuc trong Menu Pull Dowm,

- Thtrc hien khi kfch hoat cac su kien tren form/report.

Khi ra )~nh thuc hien Macro, cac hanh dQng duoc thirc hi~n nr tren x uong duoi.

4.1.1. Tao Macro mai

Tai cira s6 Database - chon Macro - chon New, hQP thoai tao Macro moi dtroc rna ra (hinh 4.1).

4.1.2. Cac hanh dOng trong Macro

1. Addmenu: Them mQt cQt vao Menu. Cac tham s6; + Menu Name: Ten cQt hi~n tren man hinh,

+ Menu Macro Name: Ten Macro (rug voi cQt do + Status Bar Text

69

III. Queries I)lFams • Repott$ !!1 p.

l2

v'.'.·

Hinh 4.1. Hi)p thoai tao Macro moi,

2. Close: Dong mot d6i tirong

+ Object Type (Ki~u d6i nrong) : Form/ Record! ... + Object Name (Ten d6i nrong)

+ Save: YesfNolPrompt

3. Apply Filter: LQc tren Form -+ Troy v~n1 Di~u kien.

4. Run Command: Thirc hien mot muc chon tren menu chinh,

5. Run Application: Chl;1Y mot irng dung (phai chi ra dirong din).

6. Goto Control : Chuyen con tro den di~u khi~n nao do.

7. Find Next. Find Record: TIm ban ghi thoa man dieu kien.

8. GotoRecord : chuyen con tr6 too mot ban ghi nao do, co th~ la ban ghi

dJ;. /..., /.. h). au, cuoi, tnroc, sau, so cu t e.

9. MsgBox: Hi~n th] thong bao,

10. Open Form: Cho phep rna form (hinh 42). GAm 6 tham sf, : + Form Name: tef.1 cua form dn rna. Chon trong list box.

+ View: Me a 1 trong 3 ch~ do Design View, Form View (ngAm dinh), Datasheet View.

70

+ Filter Name: Ten cua be} IQC - Ten cua query. + Where Condition: Dieu kien lQC.

+ Data Mode: Phuong thirc hien dii' 1i~u': c6 th~ sua them ... hay khong, + Window mode: Kich cp' cua cua s6.

FormNIIIlIII View

Fiter Name Where COfdtICln

Data Model . :,., .

Window Modo

. Innh 4.2. Vi du Macro Open Form.

11. OpenReport : Mo report. Cac tham s6 : Report Name - View - Filter Name - Where Condition.

12. Open Query, Open'Table.Opensiodule ..

13. SetValue : D~t gia tri cho mQt tnrong hose di~u khien nao d6.

14. Requery : ThI!C hien lai truy vdn ngufm cua form (dieu khi~n chua dii' li~u lfiy tren form).

15. Repaint Object: Tfnh toan lai gia tr] cua cac di~u khien tinh toan diroc.

16. RunSQL

17. TransferDatabase : Chuyen dbi du 1i~u trong bang sang .xls(Excel) hay Text.

18. TransferText: Chuyen d6i dii' li~u trong bang sang .xls(Excel) hay Text.

4.1.3. Macro c6 dl6u kltn

Vi du 4.1 : Thi€t ke form s6 dia chi. Man hlnh thi~t k~ form s6 dia chi c6 dang nhir.hlnh 4.3.

71

Thayd6i thuoc tinh Onclick

H'inh 4.3. Man hlnh thi~t k~ form s6 elja chi.

1. Dung truy vitn qrySodiaehi g6m ho ten, noi cr, sa dien thoai, Email ella sinh vien d~ xay dung form s6 dia chi,

2. Tao cac nut lenh va thay d6i thuQe tinh Onclick ella cac nut thanh mcrFilters. McrFilters eo nhiem vu loc cac ten sinh vien co chu cai dAu trong HO thoa man dieD kien ella nut l~nh. Maero eo di~u kien duqe tao ra (hinh 4.4).

72

Hinh 4.4. Macro c6 elieu kien,

4.1.4. lfng d~ng cua Macro

1. Xu 19 cac Sl) kien

2. Tao menu

3. T~o shortcut Menu

4. Menu Switchboard: MQt trong nhfrng trng dung dan gian nhirng hi~u qua cua biy su kien la gan l~nh OpenForm vOi mi)t nut tren Form d6 tao bang menu rna cac form khac.

Vi du v~ mQt menu Swichboard diroc minh hoa tren hinh 4.5.

Cac nut I~nh diroc d~t vao met form khong b] buQc. Ghi nhc Sl,l' v~ng m~t cua scroll bars

M~c du kMng duoc th~ hien 0 day. cac switchboard c6 th~ goi cac switchboard khac, cho phep ban them vao du true cay vcii ung dung cua ban

Cac key shortcut d~ trong m6i nut cho phep nguot sit dung dieu huang vo; keystrokes

Cac clip tl,l' dQng cho vao c6 th~ duoc sil dung d~ can trc cac form cua ban va giam vi~c tlurc thi toan bQ irng dung

Hinh 4.5. Vi du mQt menu Switchboard. - Tao mot Form khong rang buoc det lieu,

- B6 thanh trugt (scroll bars), mit duyet (Navigation Buttons) va mit bOi den

ban ghi (record selectors) SIl dung bang thuec tinh.

- Cit form dum ten swbMain.

73

4.2. GI(~lI THI~U NGON NGU VBA (Visual Basic For Application) V A PHUONG PHAp L;.p miNH HUONG su KltN

D~ l~p trinh ACCESS co th~ vi~t cacloai sau ;

1. Macro

2. Ham (Function)

3. Thu tuc (Subroutine)

Cac thu tuc va ham duoc thuc hien khi co Sl,l ki~n duoc kfch hoat

VBA la mQt t~p con cua Visual Basic, cac khai niem l~p trinh a day la chung

cho cac ngon ngtr I~p trmh.

VBA duoc dung trong ACCESS d~ :

- Sir dung cac cdu tnic di~u khi~n phuc tap

- Sir dung hang va bi~n

- Sir dung cac ham co sAn

- Xu Iy giao dich

- Tao va xir Iy cac dBi nrong database chuang trlnh

- Cai d{it xu ly 16i

- Tao thu vi~n ham ngtrei dung

- GQi cac ham API cua Windows

- Thuc hien cac l~nh DDE va OLE

Ciiu sa debug

Cira s6 Debug ta rnQt cong C1,l giup chay thir, go 16i chuang trinh, in k~t qua

trung gian.

1. Nh~n van module trong cira s6 va nh~n New. Chung ta phai rna mQl module thi moi rna duoc cira s6 Debug

2. Chon menu Debug

Trong cira s6 Debug Window, co th~ nh~p cac l~nh tnrc ti~p, vf du nh~p cac I~nh sau :

74

Print "hello world!"

s. = "hello"

? s & "world"

? "s" & "world"

pi = 3.14159 ? cos(2*pi)

s ="basic or cobol"

? UCase(s) ?mid(s,5,6)

4.2.1. cau true ella mQt module (dan thll) • Module s& dl,mg chung

Module Slr dung chung bao gbm cac bQ ph~n sau :

-Tuy chon :

+ Option Explicit: khai bao moi bien, + Option Base

+ Option Compare: quy djnh cac so sanh xiLu ky nr Binary: phftn biet chu (ky ur) hoa/thtrong, Text: Khong phan bi~t chit hoa/thuong. Database: quy dinh tlnr nr rieng cho CSDL

- Khai bao : Bi~n hing dung chung cho toan module

- Cac thu tuc, ham cua module

• Module I&p, module xli Iy Sl,l' k~n tfen form, report

Module lop, module xu ly s\1" kien tren form, report chi dung cho cac su kien chua n6 va bao g6m cac bQ phan sau :

- Cac tuy chon.

- Khai bao bi~n xac djnh hang dung trong module.

75

- Cac thu tuc xu ly s\l kien.

- Cac thu tuc ducc thu tuc xu ly s\l kien goi, Khong dung chung vOi sl!

kien khac,

1 Module .1
I
I I
DO'n th~ rieng DO'n th~ cua Form/Report
Thu tuc/ ham cj'U'Q'C nap Thu tuc/l-larn chi dU'Q'c nap khi nap
ngay khi chay Li'ng d!,lng Form/Report chLi'a no * Cau true m¢t module [General Deciarations]

'Ph~n khai bao chung

Option Explicit Option Base 0 Sub Thu_tucl( ... ) 'Cac l~nh

End Sub

'N~u co muc nay thl khai baa bien lit b~t buoc 'D~ khai bao chi muc cho rnang (0 hay 1)

Sub Thu_tucn( ... ) 'Cac l~nh

End Sub

Function HamXYZO 'Cac lenh HamXYZ= ...

End Function

<!> N6u muon d~t khai bao bien luon lit b~t buoc, vao menu tools chon Options, chon Editor chon Require Variable Declaration.

76

·4.2.2. CAu true cua thu tve va ham

PMn loai ham, thu h,IC theo sl,t' ki~n
Ham, thu b,IC
I .
I I
Ham, tM tuc Sl,l' ki~n Ham, thu tuc d¢C I$p
Gan vai SI,I' ki$n nnu nhtm nut, nMn Focus Kh6ng gan vai Sl,I' ki$n cu tha nao Philn loal ham, thu tuc theo pnam vi
Ham, thu tuc
j_
I I I

Public Private Static
Co th~ gO; tCr Chi dU'Q'c gOi tCr Form N@!u khai m¢,t thu tuo
mol noi trong hay Report chlra no. Static thl tat ca cac biem
lrng dl,lng Cac ham, thu tuc trang trong thu tuc se giCr
Form va Report ngam nguyen tr] glCra cac Ian
dinh Iii. Private gOi thu tuc, Vi dIJ 4.2 "

V ~ thu tuc static. Nh~p trong module va chay trong cira sf, Debug, cac bien khong l~p lai 0 sau m6i IAn goi

Static Sub IncremenThemO

Dim intCounterl As Integer

Dim intCounter2 As Integer

Dim intCounter3 As Integer

intCounterl = intCounterl + 1 intCounter2 = intCounter2 + 1 intCounter3 = intCounter3 + 1

77

Debug.Print intCounterl & "" &intCounter2 & "" &intCounter3 End Sub

.. C8u true cUa f7'1¢t ham

[Static] [Publici Private] Function <Ten ham> «Danh sach tham s6 hinh thirc va kieu> As <kieu>

<Cac l~nh>

<Ten ham> = <Bi~u thee» <Cac l~nh>

End Function

<Danh sach tham s6 hinh thirc va kieu> co dang

IBy Vall <Ten> As <Ki~u>I,IBy Vall <Ten> As <kieu> ... 1 -cCac l~nh> bao gam cit l~nh khai bao

.. C6u true cUa f7'1¢t tM tuc _ Thu tuc thuang

[Static] [Publicll'rivate] Sub <Ten thu tuc» «Danh sach tham s6 hinh tlurc va kiBu»

<Cac l~nh>

End Sub

_ Thu tyc,xu Ii St.l kien + Sl,C ki¢n tren form

Private Sub FOITn_ <ten SlJ ki~n>( <Danh sach tham s6 hinh thirc va kieu> <Cac lenh>

End Sub

+ S{l kien tren baa cao

Pri vate Sub Report_ <ten SlJ ki~n>f cDanh sach tham s6 hlnh th uc va ki ~u» <Cac I~nh>

End Sub

78

+ Stt ki~n tren JiJu khidn eua F onn hoije Report

Private Sub <Ten di~u khien> _<Ten str ki~n>( <Danh sach tharn sb hinh thirc va ki~u»

<Cae l~nh>

End Sub

Man hinh 410 thii tuc moi co dang

nhir hinh 4.6. .

Add f'rocedure • 'J

... -: ~ -.

Hinh 4.6. Man hlnh tao thu tuc mci.

4.2.3. cae quy c.1jnh khi viit ehU'O'ng trinh V8A

- M6i I~nh thirong diroc vi~t tren m¢t dong. N~u muon xuong dong

dung dau_

- M6i dong duqe phep vi~t nhieu l~nh. Cac l~nh phan each bang dau :

- Khong phan bi~t ehC'r hoa, chii thtrong

- Chu thfch co th~ vi~t nhu sau :

+ Chu thich tren toan b¢ dong: Mcr ddu dong bAng dau nhay dan (co th~ dung nr Rem thay cho dAu nhay dan nhir trong cac phien ban tnnrc)

+ Clui thfch tren phdn con lai cua dong : Phan chii thfch b~t ddu bang dAu nhay dan.

4.2.4. Djch va ch\lY chll'O'ng trinh Cira sf, true ti~p : Thu tuc

<Ten thu tl)c><Danh sach tharn s6 thirc str> Ham

?<Ten ham>( <Danh sach tham sf, thuc sl,l'» Debug.Print (In ra tren cira sf, Debug)

79

4.2.5. Cach 991 chl.J'O'llg trlnh con trong VBA

1. GQi thu tuc

<Ten thu tuc» <Danh sach tham s6 thuc sI!>

Call <Ten thu tuc» (<Danh sach tham s6 thuc SI!»

Cac tharn s6 tlurc su co th~ kh6ng d~t dung trlnh tu rna tharn s6 hinh thirc nrong ung xuit hien trong chuang trlnh con. Khi do vi~t danh sach tharn s6 thuc Sl! diroi dang:

<Ten tham s6 hlnh thirc> := <tham s6 thuc SI!> [,<Ten tham s6 hinh thirc>

:= <Tham s6 thirc SI!> ... ]

2. Goi ham

<Ten ham> (<Danh sach tharn s6 thirc SI!»

Khi khong quan tam d~n gia tri rna ham tra lai, co th~ goi ham nlur thu tuc <Ten harn><Danh sach tham s6 thirc SI!>

4.3. cAe YEU TO eo BAN TRONG CHUONG TRINH VBA

4.3.1. Ca~ kieu dir lI,u dung trong chU'O'ng trin h - S6 nguyen: Integer, Long, Byte

- S6 thirc : Single, Double

- Logic: Boolean

- Ti~n t~ : Currency

- Xau ky nr : String, String*n

- Ngay/ gio : Date

- Variant : Bi~n thuoc kitSu dfr li~u nay c6 thtS nhan gia tri 1ft s6, xiiuky tu,

ngay/gia hoac gia tr] Null.

4.3.2. Ten

- Ten g6m tai da 255 ky tlJ va duoc b~t dAu bang chi! cai.

so

- Ten co th~ xay dung tu moi ky hieu tru .!@&$#%(Day IS. nhtrng ky hieu phan each va ky hieu dinh ki~u cua Access).

4.3.3. 81~n

- Khai bao bi~n : Phai khai bao moi bien n~u dung Option Explicit. N~u khong dung, cac bien kh6ng khai bao se duoc ngfun dinh ki~u la Variant.

- Khai bao bien co tac dung djnh ki~u cho cac bien ducc khai bao, ngoai ra

con khoi tao (gan gia tr] dAu) cho bien.

1. Khai bao bi~n b8ng f~nh Dim

Kieu khai bao thong dung nh~t IS. dung I~nh Dim Dang l~nh :

Dirn-cten bien> As <ki~u>\<ten bien> As <ki~u>\ <kieu> 13. tfit ca cac kieu dii lieu d1l nhac d~n & tren

VI d1,l4.3 :

Dim a,b,e As Integer Thi a,b ~ 'variant e ~ integer

* Khai bdo biJn C1,lC bp (local) Vi du 4.4 :

Private Sub emdOkey-Click Dim strAnimal As String strAnimal = "Dog"

Call ChangeAnimai

Debug.Print strAnimal ' vin IS. Dog End Sub

Private Sub ChangeAnimai strAnimal = "Cat"

End Sub

6-GTACCE&lID

81

* su« tinh (Static) : la met dang bien dja phirong d~c biet VI du 4.5 :

Private Sub cmdLocal_C1ickO Dim intCounter As Integer intCounter = intCounter + 1 Debug.Print intCounter

End Sub

Trong vi du nay. bien CI,lC bQ duoc khoi gan lai sau m6i l~n chay VI d~4.6:

Private Sub cmdStatic_ClickO Static sintCounter As Integer sintCounter = sintCounter + I Debug.Print sintCounter .

End Sub

Trong vi du nay, bien tinh van hru lai tri sau m6i IAn chay. * min Private

[General Declarations] Option Explicit

Private Sub cmdModule_ClickO mintCounter = 5

Debug.Print mintCounter End Sub

2. Khai bao trong Form, Report hay Module * Bidn Public

[General Declarations] Option Explicit

Public pintCounter as Integer

82

Vi d!J4.7:

Private Sub crndPublic_ClickO PintCounter = 50 Debug.Print pintCounter End Sub

Bi~n Public diroc khai bao trong Form, Report hay Module. Cac bien Agio-IDs, bien thi~t l~p moi tnrong thtrong duoc khai bao Public.

Vi d!J4.8 :

Form dang nhap M thong (hinh 4.7). Chi cho phep nh~p m~t khau 3 lin. N~u :a 3 IAn d~u sai, se til d{)ng thoat kh6i Access.

":" . .).\."

.......•. ThQ.at

. '"

" .. !" Dang nhap he thong

~ "' . .

Ten ngu'Oi sU'dl,lng II

---------------------

M~tkhau

Hlnh 4.7. Fonn dang nhip h~ th6ng.

4.3.4. HAng

1. each bi~u dien cac h~ng thU¢c cac ki~u dO' lieu khac nhau - s6 - Toan hoc

- Xau ky tu - d~t trong cc}p chlu " "

- Ngay gia - phan each b~ng dAu # #

- Logic - True, False.

83

2. Mot sO hAng d(jc b~t

- Ole hang cua VB: b~t d~u bang vb. Vi du: vbYesNo, vbMonday ...

- Cac hang cua Access: b~t dAu bing ac. Vi du: acFonn ...

- Cac hang cua DAO: b~t d~u bAng db. Vi du: dbReadOnIy, dbOpenDynaset

- Cac hAng cua ADO: b~t d~u bAng ad. Vi du : adModeRead,

adLockReadOnly

3. Khai beo hang

Const <Ten hing> = <BiSu tlurc hfing>

4.3.5. Mang

Dim <Ten mimg>([<kich ciJ>])[As<ki6u>][,<ten mang>([<kich c5>])

[As-ckieu»] ... ]

(PubI iclPri v ate] Static) Kfch co:

[<c~n dum> Tol-ccan tren>[[<c~n duoi» Tol-ccan tren> ... ] Mang S 60 chieu

Vfd1,i4.9 :

Dim aCtO), b(5,-2 to 6) As Integer

a: mang variant co t6i da 10 hoac 11 phan ill' thuoc OptionBased l8. (1 hay 0) b: rnang cac s6 nguyen

1. Mimg dong kh6ng khai beo kfch CO

Mang d{>ng khong khai bao kich cO', khi su dung moi chi ra kich cO' bing lenh Redim.

Redimj Preservejcren mangot-ckfch co» [As<ki~u>][,<ten mange-t-ckfch co> )[As<kiSu>] ... ]

Vi du 4.10:

Dim a( 10) As Integer

84

Dim aO As Integer Redim a(5) As Integer a(l) =1

a(2) = 2

a(3) = 3

a(4) = 4

a(5) = 5

Redim a(6) As Integer a(6) = 6'

thi a(i) (i= 1 .. 5) = 0, a(6) = 6

2. Ham Airay

Ham array thirong g~n cho cac bien Variant Vi du 4.11 :

Dim a

a = Array ("mot" "hai" "ba" "b5n")

• t , ,

a( 1) = "mot"

4.3.6. Cae phep toan 1. SO hoc

- Dbi dAu (-), luy thira (A)

- Nhan (*), chia (/), chia lAy ph~n nguyen (\), mod (du)

- Cong (+), tnr (-)

2. Xau ky tI,f

Ghep xau va ghep cac ki~u diI li~u khac nhau

3. Ngay

Ngay - Ngay --+ s6

85

Ngay + s6 -+ Ngay Ngay - ss -+ Ngay

4. So sent:

Logic: Not, And, Or, Xor, Imp (keo theo), Eqv (tuong duong)

Cho bieu thirc chua nhieu loai phep toan rna khong c6 d~u ngoac thl thtr tv uu tien la :

- s6 hoc - xau - ngay

- So sanh

- Logic (NOT_AND_OR)

4.3.7. cae ham mAu

1. Ham xau kY tv

Space(n), Left(s,n), Right(s,n), Mid(s,i,n), Len(s), Instr(n,s,t, [cD, . Lease(s), Ucase(s), Rtrim(s), Trim(s), Str(x), Vales)

Vi dlti 4.12 :

b = Val ("2457") c := Val ("245 7")

d = Val ("2457 And 1396") -+ b = c = d = 2457

2. Ham sO toe

Abs(x), Fix(x), lnt(x), Rnd(n) Vi du 4.13 :

sb ng~u nhien tir 1 -+ m: Int(rn*Rnd + 1)

3. Ham ngay go

Date, Time, Now, DateAdd(s,n,d), DateDiff(s,dl,d2), DateValue(s) Day(d), Month(d), Year(d), Weekday(d)

Vi du 4.14 :

DatePart("),YYY" ,DateO) , ham DatePart : tra lai cac phftn cua kieu ngay

86

DateDiff("yyyy", [ngayBC], [ngaysinh]) 2: 17 'Ham DateDiff: cQng, trir so voi ngay

4. Ham cho kh3u dii fiCu Variant IsDate(x), IsNumeric, IsNull

5. Ham lien quan nhh3u kiBu dCi fiCu

if«di~u kien>, <gia tri l>,<gia tr] 2» Choose «chi s6>,<gia tr] 1>, ... <gia tri n» Format «bi~u thuc>,<khu6n dang>

6. Ham InputBox VB MsgBox

InputBox «Uri nhk>[,<tieu d~>][,<gia tr] ngfun djnh>](,<to{l dQ x>,<to{l dQ Y>])

Vi d1,l4.15: Dim mk

mk = InputBox("Cho biet"& Chr(lO)&"Mat khau","quan ly sinh vien")

Vi du 4.16 :

Private Sub Form_Before DelConfirm(Cancel as Integer, Response As Integer)

If MsgBox("Dong y xoa", VbOkCancel + vbQuestion) = vbCancel then Cancel = True

Else

Response = acDataErrContinue EndIf

End Sub

4.4. cAc CAU TRUC L!P TRINH 4.4.1. L.nh If

Ki~m"tra di~u kien d~ re nhanh ; vi du kiern tra tr] nhap van hQP van ban txtValue :

87

1. Viet tren m¢t dong

If <di~u ki~n> Then <nhom I~nh 1> I [Else -cnhom l~nh 2>] Thea vi du tren

If IsNull(Me!txtValue) Then MsgBox "You must enter a value" End If

2. Viet tfen nh~u dong If <dieu kien> Then <nhom l~nh 1> Else

[-cnhom I~nh 2>] End If

Thea VI du tren

Private Sub cmdIf_Click()

If IsNull(Me!txtValue) Then

MsgBox "You must Enter a Value" Else

MsgBox "You entered"&Me!txtValue End If

End Sub

Vi du 4.17: v~ If nhieu nhanh

Sub MultipleIfs(intNumber as Integer) _ If intNumber = 1 Then

MsgBox "You entered a One" ElseIf intNumber = 2

MsgBox "You entered a Two"

ElseIf intNumber >= 3 And intNumber <= 10 Then MsgBox "You entered a Number Between 3 and 10"

88

Eiself

MsgBox "You entered some other number" End If

End Sub

<!> Cac di~u kien trong lenh If dUQ'c ki~m tra theo tr~t W xu~t hien. Nen dung Case thay vi If nhieu nhanh, trir phi dung TypeOf d~ kiem tra ki~u mot d6i urong khi do bat buec phai dung If

3. Ham Immediate If (IIF)

MQt bien th~ cua l~nh If. Vi du ham danh gi<\ doanh thu Function EvalSales(curSales As Currency) As String

EvalSales = IIf(curSales >= 100000, "Great Job", "keep Plugging") End Function

VI d(l4.1S :

Hi~n thi 0 cho mot tr] Null tren form

= IIf (IsNull(Fonns!frmOrders !Freight),O,Forms!frmOrders !Freight)

4.4.2. ~nh Select Case

Thay vi dung If ... Then ... Else nhieu nhanh nguoi ta dung Case Select Case <bi~u thirc>

[Case <danh sach gia tri 1>]

[<nhern lenh 1>]

[Case <danh sach gia tr] n>] [<nhern I~nh n>]

[Case Else

[cnhom lenh n + 1>]] End Select

89

VI d!J4.19:

Ham nhirng ngay trong tuan

Public Function Thu(ngay As Date) As String Select Case Weekday (ngay)

Case 1

Thu = "Chu nhat" Case 2

Thu = "Thu hai" Case

Thu = "Thu ba"

End Select End Function

4.4.3. L,nh For ... Next

For <bien> = <gia tri dAu> To -cgia tr] cuoi> [Stepcbuoc nhay»] <Ok lenh»

Next [cdanh sacn bien»] Vi d!J4.20 :

Dim b(5,5,5) As Integer, i As Integer, k As Integer, j As Integer For i = 1 to 5

For j = 1 to 5

For k = 1 to 5

a(i,j,k) = i+j+k Nextk

Nextj

Next i

90

4.4.4. L9nh Do ... Loop "~ng1

. Dol { WliilelUntil} <di~u kien> J {<Cae I~nh>]

Loop

"D~ng2 Do

[ccac l~nh> J

Loop [{WhilejUntil }<di~u kien»] Vf du 4.21 :

,. Do WhUe .. Loop

Sub DoWhileLoopO

Dim intCounter As Integer intCounter = 1

Do While intCounter < 5 MsgBox i~tCounter intCounter = intCountpr + 1 Loop

End Sub

<!> Lap kh6ng bi~t s6 IAn l~p va than vong l{ip co th8 kh6ng diroc thuc hi~n

l&n nao, .

." 00 Loop ... While

Sub DoLoopWhile ()

Dim iCounter As Integer iCounter == 5

Do

MsgBox iCounter iCounter = iCounter + 1

91

Loop While iCounter < 5 End Sub

<!> 4P kh6ng bi~t s6 IAn li,ip va than vong l~p duoc th.,tc hien it nhAt ~ I ~

mot an.

* Do Until ... Loop

Sub DoUntilLoop 0

Dim intCounter As Integer intCounter = 1

Do Until intCounter = 5 MsgBox intCounter intCounter = intCounter + 1 Loop

End Sub

* Do ... LoopUntil

Sub DoLoopUntil 0

Dim intCounter As Integer intCounter = 1

Do

MsgBox intCounter intCounter = intCounter + 1 Loop Until intCounter = 5 End Sub

4.4.5. L,tnh While While <di~u kien> [ccac l~nh>] Wend

92

4.4.6. Ltnh Goto

Thuong dung d~ nhay d~n nhan thong bao 16i Vi d1tl4.22 .'

Click nut chuyen v~ ban ghi truce Private Sub Commandl? _clickO

On Error Goto Err_Commandl?_Click DoCmd.GotoRecord, acPrevious Err_Commandl? _Click

MsgBox Err .Description

Exit Sub

End Sub

Day la l6i do dli iJ ban ghi dAu rna v~n click

On Error Resume Next (bo qua 16i ti~p tuc chay lenh tiep)

4.4.7. Thlfc hi.n hanh dt')ng Macro trong chU'O'ng trinh - Obi tU'Q'ng DoCmd DoCmd.<phuang thlrc><Danh sach tham s6>

De chay cac Macro tir VBA

Vi du: DoCmd.OpenReport strReportName, acf'review

Cac Macro khong co l~nh DoCmd nrong ung la: Addmenu, MsgBox, RunCode, Sendkeys, SetValue, StopAIlMacros, StopMacro.

Vi du v~ mot s6 phirong thirc cua d6i nrong DoCmd

* Close

DoCmd.Close [<ki~u d6i tuqng>],[<Ten d6i tuqng>],[<cach ghi>]

! ! ~

Chi;m, actable acFonn

Chon acSave Y es

I acSavePrompt

VD : DoCmd.Close acform.t'cap nhap", acSaveYes

93

* Open Form

DoCmd.OpenFonn . [<ten form»,[view], [<ten bi} lQc>],[<diell kien Where>),«Data Mode>U<Window Mode>}

4.5. cAc THAO T Ac TREN T~P

Cac thao tac tren tep cho phep chuyen dir li~ll ra hru trfr a ngoai t~p mdb - Mil t¢p

Open <ten> For <each thirc> As [#]<s6 hi~ll t~p> =Dong t¢p

Close [#) <s6 hieu vung> Reset

- Ghi len t¢p tudn tv

Write # <s6 hi~u vimg>, [cdanh sach bi~u thirc»] - Doc tit t?P tudn tv

Input #<s6 hieu t~p>,[<danh sikh bien> * Cac ham dung trong thao tac t~p

- Eof «s6 hieu vimg>

- Lof «s6 hieu vung»

- Seek «s6 hieu vung>

4.6. CAU H6I ON T~P - BAI T~P

1. Viet ham de ohuan hoa xau ten nh;)p vao, nghia cac chii' cai dau viat hoa, giO>a cac tll' chi each nhau m¢t dau each, Dung ham d6 trong Form.

2. Tao bang, tao tnrcmg nha dOi tU'Q'ng.

- Tao bang: CreateTabledef, VI dl,J tao bang KHOA.

- Tl;IO tnl'ang : CreateField.

- Them trU'ang vao bang.

- Them bang vao CSDL.

94

3. XU> Iy Sl,l ki~n : Khi ngU'ai sii d.,mg can pMi nhap MASV VaG textbox. Cac tnrang hQ'p xay ra:

- N6u c.!~ trOng, hay kh6ng nh(lp rna. thl cAn dU'a ra thOng Mo.

- Nh$p rna. khOng c.!ling cOng cAn dll'a ra thOng baa cho ngU'oi sii dvng.

- N~u <lung rna thl hilln thi d~ ngU'O'i sii dl,mg quyet ainh

+ N~u e6 xlv Iy thl chuyim sang sao lU'u rOi xoa. + N~!J kh6ng xii 'V thi be. qua.

95

ChuO'I1g V

sCr Dl)NG £)61 TlJ'Q'NG TRONG VBA

5.1. KHA.I NltM v6. nfONG - T~P HOP v6. TVONG

Access 2000 h6 tro VBA giup cho viec phat trien irng dung theo huang dbi nrong ducc don gian han. Trong phkn sau day se gioi thieu ve phat trien irng dung theo huang d6i nrong trong khuon kh6 cua VBA va Access 2000.

5.1.1. T,p h9'P va dOl tll'Q'ng (Collections va Objects)

Access 2000 la moi tnrong phat trien irng dung huang d6i nrong, Cira s6 Database cua ACCESS cho phep nguoi su dung truy c~p toi cac tables, queries, forms, reports, modules va cac macros. VBA ding cho phep dieu nay thong quacac dbi tUQ11g nhu recordsets .va tableDef. DS t~n dung duoc cac th~ rnanh cua VBA trong Access, dn phai hieu ve d6i nrong cling nhir cac khai niem lien quan .

. M¢t d6i tUQ11g 18. rn¢t thuc th~ nhu oto, di~n thoai, bang video ... Cac d6i tUQ11g d~u co thuQc tinh. Vi du, 0 to co cac thuoc tfnh nhu mau sac, cira, cac thong 56 ky thuat cua dQng co va rnQt 55 thuoc tfnh khac. Thuoc tfnh eo the xac dinh ban ch~t ella dbi tUQ11g tao thanh, Hanh vi huang d6i tuong nay cho phep xac dinh m¢t thuc the rieng biet cua d6i tuong dua tren cac thuoc tfnh cua chung. Vi du rn¢t chiec xe 6t6 mau do va rnQt chiec 6t6 mau den la hai thuc the rieng biet cua dbi tuong 6to.

Cac thuoc tfnh cua d6i nrong phu thuoc vao lap d6i nrong rna no thuoc ve. MQt chiec oto co rnQt t~p hop cac thuoc tinh khac voi m¢t may dien thoai, Ca hai deu co thuoc tinh mau sic, nhung may dien thoai co thuoc tfnh am thanh. Mat khac, xe oto lai co thuoc tfnh d¢ng eo voi cong su£t khac nhau. M¢t vai d6i nrong dong vai tro 'Ia kho chua' cac dbi nrong khac (containers). Cac d6i tuong kho cling co cac thuoc tinh.

Ben canh cac thuoc tfnh, dbi nrong con co cac phuong tlurc (methods).

Phuong thirc III cac hanh vi rna dbi tirong co the t<,1O ra. May di~n thoai eo th~ k~t nbi, xe oto co the chay ... Nhieu d6i nrong co hang 10<;1t cac phirong

96

thirc khac nhau. Vi du di~n thoai co th~ cho phep ban goi n¢i hat hoac goi duong dai.

Access khong lam vi~e voi cac d&i nrong v~t Iy, Chung lam viec voi cac cAu true l~p trinh nhu forms, tables, queries va dai dien cho cac d&i nrong voi cac hanh vi va thuoc tinh. Cira sd Database window hien cac lap d6i tuong dfr lieu, nhAn chuot van mit Forms d€-mo mQt khung nhin v~ cac d6i nrong cua form, D3i tuong Form co th~ chua cac d&i tuong khac goi la cac di~u khien (controls).

T~p hop (Collections) ciing gi6ng nhir d6i nrong. MQi t~p hop trong Access d~u co thuQc tinh Count, n6 xac dinh s6 cac thuc th~ (phdn tu) trong collection. Collections ciing c6 th~ c6 thuoc tinh Item. 1'a c6 th~, su dung thuoc tfnh chi doc Item d~ tra v~ mQt form cu th~ trong t~p hop AliFomtS. Vi mQt thanh vien trong ~p hop la mQt d6i nrong cu th~ nen no khong c6 thuoc tfnh Count.

"

5.1.2. Thu4)c tinh va phl1O'ng thCrc (Properties va Methods)

Thuoc tinh va phuong thirc xac dinh hinh thai, hanh vicua cac dbi nrong.

D~ truy c~p d~n pnuong thirc hay thuec tfnh cua d6i tirong ta dung cu phap : object.property hoac object.method. Thuat ngfr object c6 th~ hi@u 13 mQt d6i nrong hay mQt t~p hop cac d6i nrong. Vf du, txtInputI.BackColor xac dinh mau n~n cua mQt hQp van ban (text box) tren form, AllForms.ltem(O) tham chieu d~n form dau tien trong t~p hQ'P cac forms, N~u form nay c6 ten Ia frmSamplel, co th~ dung AIlForms.ltem (''frmSamplel''J.

D€ xern cac thuQc tfnh ella d6i ureng, ta chon ct6i nrong trong cira s6 thiet

ke rbi nhan-chuot vao nut Properties tren Toolbar. '

D6i nrong DoCmd'la dbi nrong co nhieu pmrong tlnrc nhat cua Access vi v~y no d~c bi~t hfru dung d~ ta b~t dau voi cac plnrong tlurc. DoCmd c6 cac phuong thirc nhu Close, OpenForm, Go To Control, FindRecord va RunCommand (d§. hoc trong chuang tnroc). Nhieu phuong thirc cua DoCmd doi hoi c6 tham s& d€ xac dinh each tmrc hoat dQng cua phuong thirc, MQt s6 phuong thirc khac doi hoi cac tham s6 tuy chon (optional arguments). Neu khong xac dinh tham s& cu th~ cho mot optional argument, phuong thirc se dung cac gia tri ngam dinh.

D~ d6ng mQt form trong Access ta su dung phucmg thirc close cua d6i nrong DoCmd. Phuong tmrc nay doi hoi 2 tham sA b~t buec va mot tham s&

97

tuy chon, Tham s5 tlur nhAt xac dinh ki€u d5i nrong cAn dong. N~u muAn d6ng mQt form, su dung acFonn (acFonn Itt mQt hang chua sfu1 cua Access). Tham s5 thir hai la ten form. Tham s6 tuy chon baa cho Access co hru nhtrng thay dBi tren form hay khong (ngam dinh la acSaveYes, nguoc hili chon acSaveNo).

Wd1,i5.1:

DoCmd.Close acForm, "formname", acSaveNo

Nhi~u phirong thirc cua DoCmd diroc ap dung true ti€p cho tirng d6i nrong cu th~. Vi du, phuang thirc GoToControl g~n di~u khien (focus) cho mQt control cu th~ tren form, ta cling co th~ dung phuong thirc SetFocus d~ lam viec do.

5.1.3. S" kit" (Events)

Su kien la khai niem vo cung quan trong trong I~p trinh VBA. Ta co th~ dung cac su kien d~ lam cho ung dung trc nen linh hoat. Cac d6i tirong va cac t~p hop c6 nhtrng su kien duoc dung de dua rna l~nh cua l~p trlnh vien van irng dung. Khi lam vi~ v61 cac forms, co th~ dung cac events cho nhirng viec nhu : kiern tra tinh dung dan cua dft lieu, b~t hay t~t cac controls, thay dBi control dang co focus, rna form, va dong form ...

Ngiroi sir dung cAn phai biet khi nao nay sinh su kien cling nhu trinh tv nay sinh cua cac su kien, Khi rna rnQt form, n6 se gay ra hang loat su kien :

Open, Load, Resize va Current. S1,I' kien Open nay sinh khi form b~t dAu rna nhung tnroc khi cac ban ghi duoc hi~n ra. Su kien Load phat sinh sau su kien Open. Sau su kien Load, cac ban ghi tren form se hien ra. BAt cu doan rna l~nh nao lam thay dbi kich thuoc hay vi tri cua form nhu cac phtrong thirc Movesize, Minimize, Maximize, hay Restore cua dBi tirong DoCmd se lam nay sinh su kien Resize. Su kien Current Iii. S1) kien cuBi cling xay ra sau khi mQt form duoc rna. No cling nay sinh khi nguoi su dung dinh vi t61 ban ghi moi hoac Hun nroi lai fonn.

Cach ti€p c~n cac sir kien cua forms va cac controls cua chung : 1. Chon form hay control trong cira sB thiet k€ (Design view).

2. Nhdn phim phai chuot - chen Build Event til menu ngu canh d~ rna hQP thoai Build Event.

98

3. ChQTI ti~p Code Builder d6 rna mot thu tuc S\I kien trong VBA. C6 th~ lua chon cac su kien cAn xu ly tren trong hop combo a g6c phai tren cira s6 code ..

Vi du v~ mot su kien tren form diroc trlnh bay tren hinh 5.1.

End Sub

Private- Sub tennv_Click()

End Sub

Hinh 5.1. Vi du v~ m9t SI,!' kien tren form.

5.2. cAc VOl TIfONG CVA ACCESS

Cac tldi tuang cua mot ling dung Access

Mot irng dung MS.Access g6m cac d6i nrong (Objects) va cac t~p hopsau :

KI~U DOl TlfO'NG Y NGHTA
APPLICATION U'ng dl,lng
CONTROL f)Oi tl100ng (lieu khien trong Form hay Report
FORM Form hO(jc SubForm
REPORT sao cao
FORMS COLLECTION T(lp hop cac doi tU'Q'ng Forms dang rna
REPORT COLLECTION T(lp hoop cac Reports hi~n hanh
CONTROLS T(lp hoop cac Controls tran Form hay Report
COLLECTION hien nanh
MODUL Modul chu~n hO(jc mot lap chuAn
MODUL COLLECTION T~p hop cac moduls dang rna 99

REFERENCE Tham ehj~u d~n m¢t thU' vi~n (fOi tU'O'ng
REFERENCE COLLECTION T$p hop cac tham ehieu hi~n (fa dU'Q'e Ihi~t I~p
DOCMD aOi 1U'Q'ng DoCMD
SCREEN MO ta each sAp x~p cua cac doi tU'O'ng hi~n e6 tren
man hlnh Ba ki~u d6i nrong Table, Dynaset va Snapshot con c6 ten chung lit dbi tUQ11g Recordset.

Cac d6i nrong tren dircc t6 chUc thea rna hinh phAn cAp (hinh 5.2) :

Hinh 5.2. Mo hlnh phan cAp cac d6i nrong,

5.2.1. £)01 tU'Q'llg Form vii quan ht phin cAp

MQt dAi tUQTIg Form tham chieu to; mot bieu mau Cl,.1. th~ trong ACCESS.

Quan h~ phan c~p cua dAi nrong Form diroc rna ta nhu hinh 5.3.

I
Forms I I I I


I
Forms I Controls
Hinh 5.3. M{; hinh phful cAp cua d6i nrong Form.

MQt d6i tUQ11g Form lit thanh phan cua mot t~p hop cac Forms, T~p hop nay do Ms. Access nr tao ra d~ quan ly t~t ca cac bieu mdu. Cac thanh phan trang tip hop duoc danh sA nr chi s6 0 theo thir nr duoc tao.

100

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->