Professional Documents
Culture Documents
Lap Trinh VBA
Lap Trinh VBA
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.1 VBA l g?
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
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
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
13
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
15
16
C.1 Hm vs. Th tc
Hm Function
1. 2. 3.
Th tc Procedure
1. 2. 3.
17
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
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
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
Sub cmdComputer_Click() If opgComputeType = 1 Then MySquarer txtInput.Value Else MyCuber txtInput.Value End If End Sub
26
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
29
l Bi
tp:
Result = ???
30
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
32
33
l Do
34
l Do
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.
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
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
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