You are on page 1of 48

Microsoft Visual Basic for Applications

Created & Presented by: TBL Group

Ni dung chnh
thiu v VBA. l Cc khi nim c bn trong VBA. l V D v
v d v li v d.
l Gii

I. Gii thiu v VBA


VBA l g ? Lp trnh hng s kin. Why is VBA?

I.1 VBA l g?
l VBA:

Visual Basic for Applications l Ngn ng lp trnh Microsoft Office l VBA:


Mnh m hn. Linh hot hn. Li nhiu hn.

I.2 Lp trnh hng s kin


- mi trng pht trin theo hng i tng. l S kin ng vai tr quan trng. l Ngi s dng xc nh trnh t.
l VBA

Why is VBA?
l l l l l l l

D dng sa cha v duy tr Hm cn tm khng c trong th vin? By li (Error trapping). T ng to mi, thay i cc i tng Thc hin cc lnh cp thp (system-level) Can thip n tng bn ghi Kh nng ty bin tham s cao
6

II. Cc khi nim c bn


a. b. c. d. e. f.

Objects Properties & Methods & Events Procedures & Modules Son tho m lnh vi VBE. Mnh logic, cu trc lp... Cc hm c sn trong VBA

A. OBJECTS
l Bng l Form l Truy

vn l Bo co
.

B.1 Properties
Xc nh thng tin v i tng l Truy xut: (Tn i tng).(tn thuc tnh)
l

V d: hovaten.caption

Hnh 1: Bng thuc tnh ca mt i tng

10

B.2 Methods
l l

Phng thc, thc hin cc thao tc nht nh V d: i tng DoCmd: [Close, OpenForm,
GotoControl, FindRecord,]
DoCmd.Close acForm, "formname", acSaveNo

11

B.3 Events
l l

Event = s kin

(Oxford Dictionary)
VBA thc hin cc cng vic cn thit khi s kin tng ng xy ra.

12

C.1 Chng trnh con-Procedure


cha m chng trnh l C th nhn tham s l Gm 3 loi
Th tc: Sub procedures Hm: Function procedures Property procedures
l on

13

C.1.1 Th tc - Sub Procedure


l Th

tc:

Tnh ton, cp nht d liu,... M lnh nm gia mnh Sub v End Tham s, cch du phy ,.
Sub Tinh_Tong(First, second) Dim Result Result = First + Second Debug.Print Result End Sub
14

C.1.2 Hm - Function
l l l

Tr v gi tr Khng dng x l s kin. Bao gm tp lnh nm gia Function v End. Thot khi hm bng cu lnh: Exit Function

V d: Kim tra xem ngy a vo c thuc thin nin k th 3 hay khng

15

16

C.1 Hm vs. Th tc
Hm Function
1. 2. 3.

Th tc Procedure
1. 2. 3.

C gi tr tr v Khng dng x l s kin M lnh nm gia Function v End

Khng c gi tr tr v Dng x l cc s kin M lnh nm gia Sub v End

17

C.1.3 Properties procedures


l Property

procedures: nh ngha cc thuc tnh ca ngi s dng. l Gm 3 loi:


Property Get, Property Set, Property Let.

18

C.2 Modules
l

Cha: th tc, hm v cc khai bo nh Dim, Option, Explicit,... C hai loi Modules c bn:
Modules chun Modules trong Class (form class modules, report class modules, custom class modules)

19

20

D. Son tho m lnh - VBE


l

Truy xut:
Tools-Macro-Visual Basic Editor Alt F11

Cc ca s:
Edit windows Properties Debug window Objects window
21

E. Cc khi nim c bn
Khai bo bin - S dng mnh Dim n gin: Dim (tn_bin) As (Kiu_d_liu) l V d:
l l

Dim Dim Dim Dim

x number As Integer Matrix(1 To 3, 1 To 4) As Integer Dynamic() As Integer

22

E. Cc lnh c bn
l

Cu lnh iu kin - If...Then if condition Then 'Statements End If if (In Love) Then
(Get Married)

End If
23

E.1 Cc lnh c bn If then


l If...Then

If condition Then Statements1 Else Statements2 End If


24

E.1 Cc lnh c bn If then


If condition1 Then 'Statements ElseIf condition2 Then Statements Else 'Statements End If
25

E.1 Cc lnh c bn If then


Form trong hnh v sau y cho php ngi s dng chn la vic tnh cn bc hai hay ba:

Sub cmdComputer_Click() If opgComputeType = 1 Then MySquarer txtInput.Value Else MyCuber txtInput.Value End If End Sub

26

E.1 Cc lnh c bn If then


l l l l

Sub MySquarer (Number As Double) dblResult = Number * Number MsgBox dblResult, vbInformation, _ "What a silly example! End Sub Sub MyCuber (Number As Double) Dim dblResult As Double dblResult = Number ^ 3 MsgBox dblResult, vbInformation, _ Lets play HL 2.0 End Sub

l l l l l

27

E.2 Mnh la chn Case


Select Case test expression Case list-1 'Statements Case list-2 'Statements Case Else 'Statements End Select
28

E.3 Cc lnh c bn ForNext


For counter = start To end [Step step] [statements] [Exit For] [statements] Next [counter]

29

E.3 Cc lnh c bn ForNext


l Cc

cu lnh For c th lng nhau:

Result = 0 For I = 1 To 10 For J = 1 To 10 For K = 1 To 10 Result = Result + I J + K Next K Next J Next I

l Bi

tp:

Result = ???
30

E.3 Cc lnh c bn ForNext


l

Result ?
A. B. C. D. 0 55 1000 5500

I & J trit tiu nhau. K c tnh 1000 ln = 100 *(1 + 2++10) = 5500

31

E.4 Cc lnh c bn For Each


l For

Each element In group [statements] [Exit For] [statements] l Next [element]

32

E.4 Cc lnh c bn For Each


For Each c in worksheets("Sheet1").Range("A1:D10") If c.Value < .001 Then c.Value = 0 End If Next c
Lp trong vng A1:D10, trong Sheet1, thay gi tr trong nh hn 0.001 bng 0.

33

E.5 Cc lnh c bn- Vng lp Do

[{While | Until} condition] [statements] [Exit Do] [statements] l Loop

l Do

34

E.5 Cc lnh c bn- Vng lp Do

l Do

[statements] [Exit Do] [statements] l Loop [{While | Until} condition]

35

F. Cc hm c snBuilt in function
Danh mc
Hm chuyn i Date/time

Tn hm
CDbl, CSng, CInt, CCur, CDec, Str, Val, Hex, Oct Date, Now, DateAdd, DatePart, DateDiff, Year, Month, Day, Hour, Minute, Second, Weekday, DateValue, DateSerial, MonthName, WeekdayName, FormatDateTime DLookup, DCount, DSum, DAvg, DVar Error, CVErr, IsError IsDate, IsTime, IsNumeric, IsNull, IsEmpty,VarType Rnd, Sqr, Exp, Log, Sin, Cos, Tan MsgBox, InputBox Left, Right, Mid, Trim, UCase, LCaseStrReverse, Replace, InStr, InStrReverse Sum, Count, RGB, FV, NPV, CreateObject, GetObject, SysCmd

Tc dng
Xc nh, v i kiu cc bin Tnh ton vi ngy thng v gi.

Hm theo min Hm x l li Hm kim tra kiu Hm ton hc Thng ip Vn bn Cc hm khc

Thng k, tng hp kt qu theo tng min By li v nh ngha cc li do ngi s dng Xc nhn kiu ca cc bin Thc hin cc php ton Gi thng bo v nhn d liu X l chui, vn bn

36

T m Hiu VBA Qua V d


V d 1: Kim tra d liu Data Validation
37

T m Hiu VBA Qua V d


V d 1: Kim tra d liu Data Validation
38

Kim tra d liu - Data Validation


1. 2. 3. 4. 5. 6. 7. 8. 9.

Private Sub Form_BeforeUpdate(Cancel As Integer) If IsNull(Diachiemail) Then If MsgBox("Ban da khong nhap dia chi email. Van luu lai chu?",_ vbYesNo + vbQuestion, ApplicationName) = vbNo Then Cancel = True Diachiemail.SetFocus End If End If End Sub

39

V d 2: By li - Errors trapping

40

V d 2: By li - Errors trapping

41

V d 2: By li - Errors trapping
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15.

Private Sub Form_Error(DataErr As Integer, Response As Integer) Select Case DataErr Case 2237 Call MsgBox("Sinh vien khong co trong tep." & _ "Hay kiem tra chinh ta va nhap lai chinh xac, hoac kich" & _ "nut Them de nhap mot ban ghi moi", vbExclamation, ApplicationName) Response = acDataErrContinue Case 3022 Call MsgBox("Ban dang co them mot sinh vien co" & _ "so chung minh thu da co trong tep roi. Xin hay" & _ "sua lai so chung minh thu hoac huy bo ban ghi nay" & _ "va chuyen toi ban ghi ban dau", vbExclamation, ApplicationName) Case Else Response = acDataErrDisplay End Select

16.

End Sub

42

V d 3: To Shortcut

43

V d 3: To Shortcut

44

V d 3: To Shortcut
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13.

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) 'Thuoc tinh Key Preview cua form phai dat la Yes If KeyCode = vbKey1 And Shift = acCtrlMask Then 'Ctrl + 1 Thanhpho = "Ha Noi" Tinh = "HN" Dienthoaitruong = SetFocus End If If KeyCode = vbKey2 And Shift = acCtrlMask Then 'Ctrl + 2 Thanhpho = "Vinh" Tinh = "NA" Dienthoaitruong = SetFocus End If End Sub

45

Tng kt
l l

Gii thiu v VBA. Cc khi nim c bn trong VBA.


a. b. c. d. e. f. Objects Properties & Methods Procedures & Modules Son tho m lnh vi VBE. Mnh logic, cu trc lp... Cc hm c sn trong VBA

V D, v d v v d. 1. By Kim tra d liu Data Validation 2. li Errors trapping 3. To phm tt


46

References
l l l l l l l

H qun tr c s d liu Access Gio trnh khoa CNTT HQG H Ni. Microsoft Visual Basic for Applications 6.0 Software Development Kit Access 97 Nguyn Vn t Nh xut bn vn ha thng tin Programming Microsoft Access 2000 Rick Dobson Microsoft Press 1999 MSDN Libary April 2000 Microsoft Access Help. Advanced Access tutorial Vander Kulk

.
47

The End.
Thank you for your participation!

48

You might also like