You are on page 1of 40

I

Micros

1-She



-
Privat
' Sh
Sh
End S

Privat
'Sh
Sh

End S

2-Tim


I[

oftAccess200
ell Functio

te Sub btn
hell ("Calc.e
ell ("C:\Win
Sub
te Sub btn
hell ("C:\Pro
ell ("winwo
Sub
me and Tim


-

1-Form D







2- F

07
on: [

[
[
calc_Click
exe")
ndows\Sys
openWord
ogram File
ord.exe")
merInterva

Label
TimerI
Design View
orm View


k()
stem32\cal
d_Click()
es\Microsof
al

[
2

[
nterval=50
w

0
lc.exe")
ft Office\O
I

00

Cambo

ffice12\win

[

dia Internatio
PathName
nword.exe"


onal Coopera
e B
")
[
ation Institute
Page:
e
1

I
Micros

Privat
If lb



Els


En
lble
lblk
End S

3-Tim












Privat
If lb
l
Els
l
En

End S

4-Tim


I[

oftAccess200
te Sub For
blenglish.V
lblenglish
lblkh.Visib

seIf lblkh.V
lblkh.Vis
lblenglish
d If
english.For
kh.ForeCo
Sub
me and Tim


te Sub For
bltitle.Left <
lbltitle.Left
se
lbltitle.Left
d If
Sub
me and Tim

07
rm_Timer()
Visible =Tr
h.Visible =
ble =True
Visible =Tr
ible =Fals
h.Visible =
reColor =Q
lor =QBCo
merInterva
Label [
rm_Timer()
<=2000 T
=lbltitle.Le
=0
merInterva
Label [


)
rue Then
False
ue Then
se
True
QBColor(R
olor(Rnd()
al
[
)
hen
eft +20
al
[

Rnd() * 15)
* 15)
Cod
Cod
Cambo
de

L
de

dia Internatio
Label

Label

onal Coopera
I
I
ation Institute
Page:
I

I
e
2
I

I
Micros

Privat
S
If
l
I


E







End S


5-Tim












Privat
Me
End S


I[

oftAccess200
te Sub For
Static a As
f a =False
lblwelcome
If lblwelcom
a =True
End If
Else
lblwelcom
If lblwelc
a =Fals
lblwelco
End If
End If
lblwelco
Sub
me and Tim
C
te Sub For
e.Caption =
Sub

07
rm_Timer()
Boolean
e Then
e.Left =lblw
me.Left >=
me.Left =lb
come.Left
se
ome.Left =
ome.ForeC
merInterva
Code

rm_Timer()
="This is fo


)
welcome.L
=5000 The
lwelcome.
<=100 Th
lblwelcom
olor =QBC
al

)
orm time ti

Left +50
en
Left - 50
hen
e.Left - 50
Color(Rnd(
For
tle" & Spa
Cambo
() * 15)
rm
ce(3) & No
dia Internatio


ow()
onal Coopera

ation Institute
Page:


e
3

I
Micros

Va
B
I-[
Asses
II-

Privat



End S

Privat
Dim
Dim




End S


I[

oftAccess200
ariable:


[
Syntax 1
Syntax 2
Syntax 3
sskey

- [
Unde
- [
- [
- [
else,
- [

te Sub For
Dim i As
i = 100
MsgBox
Sub


te Sub btnC
m i As Inte
m j, re As I
i = InputB
j = InputB
re = i + j
MsgBox "R
Sub

07
[
[

: Asses
2: Asses
3: Asses
keyw

erscore(_)

do, while,

Form
rm_Load()
Integer
x i
Calc B

Calc_Click(
eger
Integer
Box("Please
Box("Please
Result is: "

sskey VarN
sskey VarN
sskey VarN
word: Dim,
[

for

load
Inp
()
e input i",
e input j",
" & re
[

[
[

Name As D
Name As D
Name1, Va
Static, Pri

[ (Alph

[
[
[

Messa
putBox

"Using Var
"Using Var
Cambo
Variable


DataType
DataType,
arName2,
ivate, Publ

habet 26


agebox

riable")
riable")
dia Internatio
e
[
0
VarName2
VarName3
lic
[
A-Z or a-


[I
[
i
i

j
onal Coopera

Memor
2 As DataT
3 As DataT

-z Num
&,%,@,
D

ation Institute
Page:
ry

Type
Type
mber(0-9)
,*,(,) .
Dim, As, i

e
4


f,

I
Micros




Privat
If f

Els

Els

Els

En
End S
-------
Log-i
Optio
Dim s
-------
Privat
Do
End S
-------
Privat
Dim
Dim
If


E

E
End S
Log-i


I[

oftAccess200

Form

te Sub frm
rmCalc.Va
txtre.Value
seIf frmCal
txtre.Value
seIf frmCal
txtre.Value
seIf frmCal
txtre.Value
d If
Sub
--------------
in Passwo
on Compare
str As Strin
--------------
te Sub btn
oCmd.Close
Sub
--------------
te Sub btn
m user As
m pass As
f txtuser.Va
DoCmd.
DoCmd.
Else
MsgBox
End If
Sub
in Passwo
a-
b-
-

B
-

07

mCalc_Click
alue = 1 Th
e = Val(txta
lc.Value =
e = Val(txta
lc.Value =
e = Val(txta
lc.Value =
e = Val(txta
--------------
ord
e Databas
ng
--------------
cancel_Cli
e
--------------
log_Click()
String
String
alue ="CIC
.OpenForm
.Close acF
x "Incorrect
ord

Data
B
Passwo
Passwo

k()
hen
a.Value) +
2 Then
a.Value) -
3 Then
a.Value) *
4 Then
a.Value) *
--------------
e
--------------
ick()
--------------
)
CI" And txt
m "frmLefta
Form, "frml
t password
abase
Passwo
ord [

ord
[
Val(txtb.V
Val(txtb.Va
Val(txtb.Va
Val(txtb.Va
----
-----
-----
tpass.Valu
and right",
og"
d!", , "User

[
ord

Form 0
Mess
Cambo

Value)
alue)
alue)
alue)
e ="123" T
acNormal
Account"
Form
0
sage
dia Internatio
Then
Password
Please t
onal Coopera
d
try again
ation Institute
Page:
e
5

I
Micros










Optio
Dim i
Privat
If tx
M
Els
M
i
En
If i
M
D

En
End S
Main






Privat
Sh
End S
-------
Privat
Sh
End S
I[

oftAccess200
-

B

on Compare
As Intege
te Sub btn
xtuser.Valu
MsgBox "W
se
MsgBox "P
i =i +1
d If
=3 Then
MsgBox "b
DoCmd.Cl
d If
Sub
Switchbo
te Sub btn
ell ("calc.e
Sub
--------------
te Sub btn
ell ("excel.
Sub

07
Passwo
e Databas
r
log_Click()
ue ="cici"
Welcome to
Please try a
bye bye"
ose acFor
oard [
calc_Click
exe")
--------------
excel_Clic
.exe")


ord
e
)
And txtpas
o database
again"
m, "frmlog

Data
k()
---------
ck()
3

ss.Value =
e"
Advance"
abase



Cambo

Form
"123" The
dia Internatio

en
onal Coopera ation Institute
Page:
e
6

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:7

--------------------------------------------
Private Sub btnword_Click()
Shell ("winword.exe")
End Sub
--------------------------------------------
Private Sub btnform_Click()
Dim st As String
On Error Resume Next
st =txtname.Value
DoCmd.OpenForm st, acNormal
End Sub
-----------------------------------------------------
Private Sub btnreport_Click()
Dim rptName As String
On Error Resume Next
rptName =txtname.Value
DoCmd.OpenReport rptName, acViewPreview
End Sub
-------------------------------------------------------
Private Sub btntable_Click()
Dim str As String
On Error Resume Next
str =txtname.Value
DoCmd.OpenTable str, acViewNormal
End Sub

Using String Function with in VBA
- Left(string,length) : [ String
- Right(string,length): [ String I
- Mid(String,Start,length): [ String 0

Form [
-

Textbox

Button Left B

Message

[
-

Button Mid B Message

[
- Button Left B

Message

I
Micros











Write
Optio
Dim s
-------
Privat
On E
Dim i
str
i =
txtl
lbll
End S
-------
Privat
On E
Dim
Dim
str
l =
j =
"Input
i =
txtm
lblM
End S
-------
Privat
On E
Dim i
str
i =
txtr
I[

oftAccess200
e Code:
on Compare
str As Strin
--------------
te Sub btn
rror Resum
As Intege
=Trim(txt
InputBox("
eft.Value =
eft.Caption
Sub
--------------
te Sub btn
rror Resum
m j As Integ
m l As Integ
=Trim(txt
Len(str)
InputBox("
t integer")
InputBox("
mid.Value
Mid.Captio
Sub
--------------
te Sub btn
rror Resum
As Intege
=Trim(txt
InputBox("
right.Value

07
e Databas
ng
--------------
Left_Click(
me Next
r
name.Valu
"Input Inte
=Left(str, i
n ="Cut N
--------------
mid_Click(
me Next
ger
ger
name.Valu
"Input Inte
"Input Inte
=Mid(str,
on ="S: " &
--------------
Right_Clic
me Next
r
name.Valu
"Input Inte
e =Right(st


e
--------------
()
ue)
ger", "Inpu
)
: " & i
--------------
()
ue)
ger Start,"
ger lenght"
j, i)
& j & " L: " &
--------------
ck()
ue)
ger ", "Inpu
tr, i)
--------------
ut integer")
--------------
& Space(3
", "Input in
& i
--------------
ut integer")
Cambo
-----------
-----------
3) & "Len o
teger")
-----------
)
dia Internatio
of Name: "
onal Coopera
& l & " Ch
ation Institute
Page:
haracters",
e
8

I
Micros

lblr

End S
-------
Privat
txtn
txtl
txtm
txtr
End S

0
Right





Optio
Privat
Dim s
Me
Tim
En
Privat
Dim
Dim
Sta
str
cou




I[

oftAccess200
right.Captio
Sub
--------------
te Sub For
name.Valu
eft.Value =
mid.Value
right.Value
Sub


n Compare
te Sub For
str As Strin
e.Caption =
merInterva
d Sub
te Sub For
m count As
m str As St
atic i As Int
= "Welcom
unt = Len(s
If i < count
Me.Cap
i = i + 1
Else

07
on ="Cut N
--------------
rm_Load()
ue =Null
=""
=""
e =""
F
Left
e Databas
rm_Load()
ng
= str
l = 100
rm_Timer()
s Integer
tring
teger
me to Stud
str)
t Then
ption = Left


N: " & i
--------------
orm Desig
e
)
dy Access
t(str, i + 1)
--------------
n

Level II"
Cambo
-----------
View For
dia Internatio
rm [
onal Coopera

ation Institute
Page:


e
9

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:10

i = 0
End If
'-----------------------
Dim st As String
Static j As Integer
st = "Welcome to Study Access Level II"
If i < count Then
lblright.Caption = Right(str, j + 1)
j = j + 1
Else
j = 0
End If
'-------------------------
Dim s As String
Dim cr As Integer
Static k As Integer
s = "Welcome to Study Access Level II"
cr = Len(s)
If k < cr Then
lblleft.Caption = Left(s, k + 1)
k = k + 1
Else
k = 0
End If
lblleft.ForeColor = QBColor(Rnd() * 15)
lblright.ForeColor = QBColor(Rnd() * 15)

End Sub
-----------------------------------------

I
Micros











Privat
Dim
Dim
Dim
Dim
I[

oftAccess200
[

if


Block S

Syntax:


te Sub Cm
m data As
m x1, x2 A
m a, b, c A
m rs As St

07
.else
Proce
Statement

mdCalculate
Double
As Double
As Double
ring
if







Co
eif Stateme
ess Progra


e_Click()
condition1

elseif

else

end i
ntrol Sta
nt
am

1 then
Stateme
f condition2
Stateme
Stateme
f
Cambo
atement


ent
2 then
ent
ent..
dia Internatio

0
B
onal Coopera
0
B[
ation Institute
Page:1

e
11

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:12

a = Val(txtA.Value)
b = Val(txtB.Value)
c = Val(txtC.Value)
data = (b * b) - (4 * a * c)
If data < 0 Then
txt4.Value = "Not Root"
txt5.Value = "Not Root"
lblinfo.Caption = "Not root"
ElseIf data = 0 Then
x1 = (-b / 2 * a)
x2 = x1
txt4.Value = x1
txt5.Value = x2
lblinfo.Caption = "Root x1 = x2"
ElseIf data > 0 Then
x1 = (-b + Math.Sqr(data)) / (2 * a)
x2 = (-b - Math.Sqr(data)) / (2 * a)

txt4.Value = x1
txt5.Value = x2
lblinfo.Caption = "Has two root"
End If
End Sub



I
Micros

St

Synta










Dim a
Dim d
Privat
d =
age
Sel






E
End S

I[

oftAccess200
[

S
atement

ax:

age As Inte
d As Date
te Sub btn
= CDate(tx
e = CInt(Y
ect Case a
Case Is <
lblre.Ca
lblre.Fo
Case Is >=
lblre.Ca
lblre.Fo
End Select
Sub

07
Select Case




B

eger
OK_Click(
tdate.Valu
Year(Now()
age
18
aption = "Y
reColor = v
= 18
aption = "Y
reColor = v
S






e Statemen


You can
You c
)
e)
)) - CInt(Ye
ou cann't d
vbRed
ou can driv
vbGreen
Select Case
Case

Case

End S
nt

[

nnt drive
can drive a
ear(d))
drive a car
ve car, you
VarName
e Value1
Statemen
e Value2
Statemen
Select
Cambo

If


a car

a car

,your age
ur age is: "
nt
nt
dia Internatio

then
[


is: " & age
" & age
onal Coopera
Process

B
[

e
ation Institute
Page:1
s Statemen
B

B
e
13
nt

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:14

Loop Statement
Loop Statement

Statement

Loop Statement
- Do.loop
- While.Wend
- ForNext
I- [

DoLoop
1-Do loop [

[
Syntax 1 Syntax 2
Do While Condition
Statement.
Loop
Do
Statement.
Loop While Condition

Statement [

(True)

Process Statement

Statement [

(True)

Process Statement


[
Syntax 3 Syntax 4
Do Until Condition
Statement.
Loop
Do
Statement.
Loop Until Condition

Statement [

(False)


loop

Statement

Process
Statement

Statement [

(False)


loop

Statement
Process Statement

I
Micros

Privat
Dim
Dim

j





End S

Privat
Dim

j





End S
I[

oftAccess200


te Sub btn
m i, j As Int
m str As St
i = InputBo
j = InputBo
Do
str = str
i = i + 1
Loop W
txtre.Va
Sub
2- [

loop

Syntax:



te Sub btn
m i, j As Int
i = InputBo
j = InputBo
While i <=
Str = St
i = i + 1
Wend
txtre.Va
Sub

07

ok_Click()
teger
tring
ox("Please
ox("Please
r & "i=" & i

While i <= j
alue = str
While
[



ok_Click()
teger, Str A
ox("Please
ox("Please
j
tr & "i=" &

alue = Str


OK B
e input Star
e input End
& vbNewL
.loop

(True)

OK B
As String
e input Star
e input End
i & vbCrLf

M
rt Number"
of Numbe
Line

M
rt Number"
of Numbe
While
Wend
Cambo
Massage
")
er")

Do


Massage
")
er")
e Condition
Stateme
d
dia Internatio

while

n
ent..
onal Coopera
i

Loop
i

j
ation Institute
Page:1
B

e
15

I
Micros

Privat
Dim
Fo


Ne


End S









I[

oftAccess200
3- [

For Next
B
Syntax:



te Sub btn
m i, sum A
r i = 1 To 5
str = str &
sum = sum
ext
txtre.Value
txtre.Value
Sub


Dim i, n,
n=CInt(in
sum=0
Fo

Ne
M

07
ForN
t Loop




ok_Click()
s Integer,
5
"i=" & i & v
m + i
e = str
e = txtre &
Sum As In
nputBox (P
or i=1 to n
sum=
ext i
sgbox (Su
For c

Next


Next
Loop


str As Strin
vbNewLine
String(8, "
nteger
Please inpu
=sum+i
um is:) & s

countVar=S
Stateme
var
[

ng
e
"-") & vbCr
ut N))
sum
StartNum T
ent
Cambo


rLf & "Sum
To EndNum
dia Internatio
[j

m=" & sum


m [Step]
onal Coopera

While

lo
ation Institute
Page:1
loop
oop
e
16

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:17

How to Create Function


Function Sub Procedure

Block Code Block Statement [

[
[

VBA
- Sub procedure
- Function procedure
I-Sub Procedure

I_

[I

Statement B
Statement

Sub

End Sub
Syntax:



Sub ClearCtrl [ Clear Textbox

Argument
Sub ClearCtrl()
txta.Value = Null
txtb.Value = ""
txtre.Value = ""
End Sub

Sub ClearCtrl [ Clear Textbox ArgumentList


Sub ClearCtrl(txt1 As TextBox, txt2 As TextBox, txt3 As TextBox)
txt1.Value = ""
txt2.Value = ""
txt3.Value = ""
End Sub
[

Clear
Private Sub btnClear_Click()
ClearCtrl txta, txtb, txtre
End Sub

[ 0 Textbox

Argument [

Public/Private Sub ProcedureName(AgrumentList As DataType)
Statement..
End sub

I
Micros

Sub S
Dim
Dim
Fo




Ne

End S
Privat
Sh
End S








Optio
Sub S
re
re
End S
Privat
Ca
End S
I[

oftAccess200

ShowStar(n
m i, j As Int
m str As St
r i = 1 To n
For j = i To
str = str
Next
str = str &
ext
txt1 = str
Sub
te Sub btn
owStar 9,
Sub

n Compare
SumNum(a
= Val(a) +
= Format(r
Sub
te Sub btn
all SumNum
Sub

07
n As Intege
teger
tring
n
o n
r & "* "
vbNewLin

Show_Clic
txtshow
Sub Sub
e Databas
a As TextB
Val(b)
re, "0.00")
sum_Click
m(txta, txtb


er, txt1 As
ne
ck()
bNum
e
Box, b As T
k()
b, txtre)
TextBox)
Argu
TextBox, re

Cambo
ment
e As TextB
dia Internatio

Box)
onal Coopera ation Institute
Page:1
e
18

I
Micros

II-Fun





Optio
Funct
Dim




F
End F
Privat
Dim
tx
End S


I[

oftAccess200
nction Proc

Syntax:
Fu

En



n Compare
tion Fact(n
m i As Inte
F = 1
For i = 1 T
F = F *
Next
Fact = F
Function
te Sub btn
m n As Inte
xtre = Fact
Sub

07
cedure


Fu
unction Fun
Satem
nd Function
Functio
e Databas
n As Long)
ger, F As L
To n
i
factorial_C
eger
(Val(txtnu

nction


nctionName
ment.
n
on [
e
) As Long
Long
Click()
um.Value)

S
End Funct
e(Agrumen
..
Fac

)
Cambo
Statement
tion
ntList As D
ctorial
dia Internatio
[

ataType) A
onal Coopera
B
As DataTyp
ation Institute
Page:1
Statemen
pe
e
19
nt

I
Micros











Funct
Dim
Dim
P =
Fo

Ne
Po
End F
Privat
lblr
End S



I[

oftAccess200

Fu
tion Power
i As Intege
m P As Inte
= 1
r i = 1 To C
P = P * CI
ext
ower = P
Function
te Sub btn
re.Caption
Sub

07
Functio
unction
r(a As Integ
er
eger
CInt(n)
nt(a)
Power_Cli
= Power(V
Functio


on [
Agrumen
ger, n As I
ick()
Val(txta.Va
on


Pow
nt


nteger) As
alue), Val(tx

Cambo
wer


s Long
xtn.Value))

dia Internatio


)
onal Coopera

ation Institute
Page:2
. 3
2
e
20

I
Micros

Public
Dim
Dim
For



N
Min
End F



A-

Public
Age
End F


B-



I[

oftAccess200
c Function
m i As Inte
m Min As D
r i = LBoun
If Min <=
Min = n
End If
Next
nNum = M
Function

Funct
c Function A
e = Int((Dat
unction

Functio

07
MinNum(P
ger
Double
nd(n) To UB
n(i) Then
n(i)
in
tion [
Age(DoB As
te - DoB) / 3
n [

Excel:


ParamArra
Bound(n)
B
Date)
365.25)

DateofBi

ay n()) As D

rth-60/55*3
Cambo
Double
365.25
dia Internatioonal Coopera ation Institute
Page:2
e
21

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:22

Using Listbox
Listbox

ComboBox Control [ List [


ListBox

ComboBox Method
1- Properties ListBox

ComboBox
- Column Header [ Header Listbox ComboBox
- Column Count [ Column
- Column Width [ Column


- Row Source type [ [
o Table/Query: [

Table Query
o Field List: [ Field Table Query
o Value List: [

Table Query
- Row Source : [

Table/Query SQL Statement


2- Method ListBox

ComboBox
- Add

Listbox
ListBoxName.AddItem(Item As String)
Ex:



- Read

Listbox

Column
ListBoxName.ItemData(Index as Long)
Ex:



- Read

Listbox

Column
ListBoxName.Column(Index as Long, Row)
Index:

Column index
Private Sub btnAddList_Click()
ListProduct.AddItem (txtProList.Value)
End Sub
Private Sub btnRead_Click()
Dim i As Integer
i = InputBox("Please input Index")
MsgBox ("Product name is:") & ListProduct.ItemData(i)
End Sub

I
Micros








Option
Private
lstp
End S
-------

I[

oftAccess200
Ex


- Selec
Li

Ex

- Coun
Li

- Remo
Li
Listb
n Compare
e Sub btnad
pro.AddItem
ub
--------------

07
Ro
x:
ct Item


stBoxNam
IR
x:
t Item

L
stBoxNam
ove (Delete
stBoxNam
box
Database
dd_Click()
m (txtadd.Va
--------------
Private
txtn
End S
Private
list
End S
Private
Msg
End S


ow:


Listbox
me.Select
Row Row
Listbox
me.ListCo
e) Item

me.Remo
alue)
--------------
e Sub btnR
name.Value
ub
e Sub btnS
Book.Selec
ub
e Sub btnC
gBox listBo
ub
Row index
ted(IRow
w Index
ount()
Listbox
oveI tem(I
------
ReadMoreC
e = listBoo
Select_Clic
cted(2) = T
Count_Click
ook.ListCou
Cambo
x
w as Long,
Index)
Column_Cli
ok.Column(
k()
True
k()
unt
dia Internatio
, Row) As
ick()
(Val(txtcolu
onal Coopera
s boolean
umn), Val(t
ation Institute
Page:2
txtrow))
e
23

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:24


Private Sub btnclear_Click()
lstpro.RowSource = ""
End Sub
-----------------------------------------------------
Private Sub btndelete_Click()
For i = 0 To lstpro.ListCount - 1
If StrComp(lstpro.ItemData(i), txtitem.Value, vbTextCompare) = 0 Then
lstpro.RemoveItem (i)
End If
Next
End Sub
-----------------------------------------------------
Private Sub btnread_Click()
txtafterread.Value = lstpro.ItemData(CLng(txtindex.Value))
lstpro.Selected(CLng(txtindex.Value)) = True
End Sub
Private Sub Form_Load()
lstpro.RowSourceType = "Value List"
End Sub

Listbox











Option Compare Database
Private Sub btndelete_Click()
For i = 0 To LstProvince.ListCount - 1
If StrComp(LstProvince.ItemData(i), txtre, vbTextCompare) = 0 Then
LstProvince.RemoveItem (i)

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:25

MsgBox "Has been delete"


txtre.Value = ""
End If
Next
End Sub
Private Sub btnsearch_Click()
Dim b As Boolean
b = False
If LstProvince.ListCount < 1 Then
MsgBox "Not Item to found"
Exit Sub
Else
If txtsearch.Value <> "" Or IsNull(txtsearch.Value) = False Then
For i = 0 To LstProvince.ListCount - 1
If LCase(LstProvince.ItemData(i)) = LCase(txtsearch.Value) Then
LstProvince.Selected(i) = True
MsgBox "This is Province you search is: " &LstProvince.ItemData(i)
txtre.Value = LstProvince.ItemData(i)
b = True
Exit Sub
End If
Next
Else
MsgBox "Please type date that you want to search"
Exit Sub
End If
End If
If b = False Then
MsgBox "Not found"
End If
End Sub
Private Sub Form_Load()
LstProvince.RowSourceType = "Value List"
txtpro.Value = ""
txtpro.SetFocus
End Sub
Private Sub btnadd_Click()
LstProvince.AddItem (txtpro.Value)
txtpro.Value = ""
txtpro.SetFocus
End Sub




I
Micros

Form










Option


Private

I[

oftAccess200
[
n Compare
Dim num
Dim n As
Dim re
e Sub btnok
num = CD
n = num
If n <= 1
re
ElseIf n <
re
ElseIf n <
re
ElseIf n <
re
ElseIf n <
re
ElseIf n <
re
ElseIf n <
re

07

Database
As Double
Double
As String
k_Click()
Dbl(txtnum.
Mod 80
Then
= " .
<= 2 Then
= "2 .
<= 3 Then
= "3 .
<= 4 Then
= "4
<= 5 Then
= "5 .
<= 6 Then
= "6 .
<= 7 Then
= "7 . .



.Value)
. . ....
. ...
. . ..
. ...
. . ..
. . ..
... .
.. ...
... .
.. ...
.....
.....
....
.....
Cambo
......
.....
...,
... .
.....
.....
......
dia Internatio
"
. ."
."
...."
. ...
..,."
...."
onal Coopera
..."
ation Institute
Page:2
e
26

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:27

ElseIf n <= 8 Then


re = "8 . . . ........ "
ElseIf n <= 9 Then
re = "9 . .... ....... ......."
ElseIf n <= 10 Then
re = "10 . .. ..........., ._ "
ElseIf n <= 11 Then
re = "11 . . ..... .......... "
End If
Lblre.Caption = re
lblre.FontName = "Limon S1"
lblre.FontSize = 30
lblre.ForeColor = vbBlue
End Sub

0

Code

j Database

Database

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:28

Using Database




















Option Compare Database
Dim db As DAO.Database
Dim rs As DAO.Recordset
-----------------------------------------------------
Private Sub btnadd_Click()
Set db =CurrentDb
Dim str As String
str ="Insert Into tblStu Values(" & txtid.Value & ",'" & txtname.Value & "','" &
txtsex.Value & "',#" & txtdob.Value & " #,'" & txtpob.Value & "')"
db.Execute str
MsgBox "Has been add to database", , "Database"
End Sub
-----------------------------------------------------
Private Sub btnClear_Click()
For Each ctrl In Me.Controls
If TypeOf ctrl Is TextBox Then
ctrl.Value =""
End If
Next
End Sub
-----------------------------------------------------
Private Sub btnDel_Click()
Set db =CurrentDb
Dim st As String
st ="Delete from tblstu where id=" & txtid.Value

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:29

db.Execute st
MsgBox "Success to delete data"
End Sub
-----------------------------------------------------
Private Sub btnSearch_Click()
Set db =CurrentDb
Set rs =db.OpenRecordset("Select * from tblstu where id=" & txtid.Value)
If rs.EOF Then
MsgBox "Not found data"
Else
txtname =rs(1)
txtsex =rs(2)
txtdob =rs(3)
txtpob =rs(4)
End If
End Sub
-----------------------------------------------------
Private Sub btnupdate_Click()
Set db =CurrentDb
Dim str As String
str ="Update tblStu SET Name='" & txtname.Value & "',Sex='" & txtsex.Value &
"',Dob=#" & txtdob.Value & "#,Pob='" & txtpob.Value & "' Where id=" & txtid.Value
db.Execute str
MsgBox "Success to update data"
End Sub



















I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:30













Option Compare Database
Dim db As DAO.Database
Dim rs As DAO.Recordset
--------------------------------------------------------
Private Sub btnadd_Click()
Set db =CurrentDb
Set rs =db.OpenRecordset("tblStu")
rs.AddNew
rs(0) =txtid
rs(1) =txtname
rs(2) =txtsex
rs(3) =txtdob
rs(4) =txtpob
rs.Update
MsgBox "Success to data new data"
End Sub
--------------------------------------------------------
Private Sub btnClear_Click()
txtid =""
txtname =Null
txtsex =""
txtdob =""
txtpob =""
End Sub
--------------------------------------------------------
Private Sub btnDel_Click()
Set db =CurrentDb
Set rs =db.OpenRecordset("tblStu")
Do Until rs.EOF
If Val(txtid) =rs(0) Then
rs.Delete

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:31

End If
rs.MoveNext
Loop
MsgBox "Success to delete data"
End Sub
--------------------------------------------------------
Private Sub btnSearch_Click()
Set db =CurrentDb
Set rs =db.OpenRecordset("tblStu")
Do While Not rs.EOF
If Val(txtid) =rs(0) Then
txtname =rs(1)
txtsex =rs(2)
txtdob =rs(3)
txtpob =rs(4)
Exit Sub
End If
rs.MoveNext
Loop
MsgBox "Not found data"
End Sub
--------------------------------------------------------
Private Sub btnupdate_Click()
Set db =CurrentDb
Set rs =db.OpenRecordset("tblStu")
Do While Not rs.EOF
If Val(txtid) =rs(0) Then
rs.Edit
rs(1) =txtname
rs(2) =txtsex
rs(3) =txtdob
rs(4) =txtpob
rs.Update
End If
rs.MoveNext
Loop
MsgBox "Success to update data"
End Sub






I
Micros




Table
ID
1

Table
ID
2
3
4
5
6
7
8
9
10


I[

oftAccess200
Table
e

: tblIn
PC Num
2
3
4
5
6
7
8
9
10
e


PC
2 2
3 3
4 4
5 5 0
6 6
7 7
8 8

9 10
0 14 [

07

e
nfo
mber N
2
3
4
5 0
6
7
8


10
14 [


Name

0
0


Pro
Name khmer


Eng
Chhuy Ro
Lenh Cha
Phong Le
Nan Kosa
Chat Cha
Bak Sokm
Shoeng S
Thol Chan
Chhrem D

oject
Chhuy
Lenh
Phong
Nan K
Chat C
Bak S
Shoen
Thol C
Chhre
lish
oem W
ang W
akena P
al P
anphisey E
mom W
Shida W
nthy W
Darin E
Cambo

Name Englis
y Roem
Chang
g Leakena
Kosal
Chanphisey
Sokmom
ng Shida
Chanthy
em Darin
Subject
Word & Exce
Word & Exce
Power Point
Power Point
Excel Advanc
Word & Exce
Word & Exce
Word & Exce
Excel 2007
dia Internatio

sh
[
[
[
[
[
[
[
[
[
H
el 7:00 -
el 7:00 -
7:00 -
7:00 -
ce 7:00 -
el 7:00 -
el 7:00 -
el 7:00 -
7:00 -
onal Coopera
Sex
[
[
[


[
[


[
Hours
8:00 AM 1
8:00 AM
8:00 AM
8:00 AM
8:00 AM 3
8:00 AM 0
8:00 AM 2
8:00 AM 2
8:00 AM 0
ation Institute
Page:3
Note
None
None
None
None
None
None
None
None
None
Start Date
10/Sep/2012
13/Oct/2012
27/Oct/2012
01/Oct/2012
31/Mar/2012
04/Sep/2012
28/Sep/2012
28/Sep/2012
03/Sep/2012
e
32
2
2
2
2
2
2
2
2
2

I
Micros









Coding
Option
Dim d
Dim rs
-------
Private
Set db
If IsNu
Msg
txtP
ElseIf
Msg
txtk
ElseIf
Msg
txtE
ElseIf
Msg
txtS
Else
Dim
str
txtPCN
txtSex
db.
I[

oftAccess200
Form

g:
n Compare
b As DAO.D
s As DAO.R
--------------
e Sub btnAd
b = Current
ull(txtPCNu
gBox "Pleas
PCNum.SetF
IsNull(txtkh
gBox "Pleas
khmer.SetF
IsNull(txtE
gBox "Please
En.SetFocus
IsNull(txtSe
gBox "Please
Sex.SetFocu
m str As Stri
= "Insert in
Num.Value
x.Value & "',
.Execute str

07


Database
Database
Recordset
--------------
dd_Click()
tDb
m) = True
se input PC
Focus
hmer.Value
se input Nam
ocus
nglish.Value
e input Nam
s
ex.Value) =
e input Sex
us
ing
nto tblInfo (
& ",Trim('"
,'" & txtNot
r


---------
Then
Number", ,
) Then
me Khmer",
e) = True T
me English",
= True Then
", , "New St
([PC Numbe
& txtkhmer
e.Value & "
, "Number P
, , "New Stu
Then
, , "New Stu
n
tudent"
er],[Name k
r.Value & "')
"')"
Cambo
PC"
udent"
udent"
khmer],[Na
),Trim('" &
dia Internatio
me English]
txtEnglish.V
onal Coopera
],Sex,Note)
Value & "'),'
ation Institute
Page:3
Values(" &
'" &
e
33
&

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:34

MsgBox "Success"
LoadInvoice
End If
End Sub
-------------------------------------------
Sub LoadInvoice()
Set db = CurrentDb
LVStro.ColumnHeads = True
LVStro.RowSourceType = "Table/Query"
LVStro.RowSource = "Select * from tblInfo order by id"
LVStro.ColumnCount = 6
txtID.Enabled = False
txtID.Value = "Autonumber"
txtcount.Value = LVStro.ListCount - 1
End Sub
-------------------------------------------
Private Sub btnBrowse_Click()
DoCmd.OpenForm "FrmScore", acNormal
End Sub
-------------------------------------------
Private Sub btnDelete_Click()
If MsgBox("Do you want to delete record?", vbYesNo, "Delete Record") = vbYes Then
Set db = CurrentDb
Dim str As String
If IsNull(txtID.Value) = True Or txtID.Value = "AutoNumber" Then
MsgBox "Please select on the list."
Else
str = "Delete from tblInfo where ID=" & txtID.Value
db.Execute str
MsgBox ("Success")
LoadInvoice
End If
End If
End Sub
-------------------------------------------
Private Sub btnUpdate_Click()
If MsgBox("Do you want to Update record?", vbYesNo, "Update old Record") = vbYes Then
Set db = CurrentDb
Dim str As String
str = "Update tblInfo set[PC Number]=" & txtPCNum.Value & ",[Name khmer]='" &
txtkhmer.Value & "',[Name English]='" & txtEnglish.Value & "',[Sex]='" & txtSex.Value &
"',[Note]='" & txtNote.Value & "' where ID =" & txtID.Value
db.Execute str
MsgBox "Has been Update Recode!", , "Cambodia International Cooperation Institute"
End If
End Sub
-------------------------------------------

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:35

Private Sub Command33_Click()


DoCmd.OpenForm "FrmMain", acNormal
DoCmd.Close acForm, "FrmInformation"
End Sub
-------------------------------------------
Private Sub Command45_Click()
If MsgBox("Do you want to close form?", vbYesNo, "Close Form") = vbYes Then
DoCmd.Close acForm, "FrmInformation"
End If
End Sub
-------------------------------------------
Private Sub Form_Load()
'DoCmd.Maximize
LoadInvoice
txtNote.Value = "None"
End Sub
-------------------------------------------
Private Sub Form_Timer()
lblH.Caption = FormatDateTime(Now(), vbLongTime)
lblD.Caption = Format(Now(), "dddd dd-MMM-yyyy")
lblW.ForeColor = QBColor(Rnd() * 15)
End Sub
-------------------------------------------
Private Sub LVStro_Click()
txtID.Value = LVStro.Column(0, LVStro.ListIndex + 1)
txtPCNum.Value = LVStro.Column(1, LVStro.ListIndex + 1)
txtkhmer.Value = LVStro.Column(2, LVStro.ListIndex + 1)
txtEnglish.Value = LVStro.Column(3, LVStro.ListIndex + 1)
txtSex.Value = LVStro.Column(4, LVStro.ListIndex + 1)
txtNote.Value = LVStro.Column(5, LVStro.ListIndex + 1)
End Sub
-------------------------------------------
Private Sub txtSearch_KeyUp(KeyCode As Integer, Shift As Integer)
Dim str As String
If KeyCode = vbKeyReturn Then
If LVStro.ListCount > 0 Then
LVStro.SetFocus
LVStro.ListIndex = 0
End If
Else
LVStro.RowSource = "Select * from tblInfo where [Name English] like '" & txtSearch.Text &
"*'"
txtcount.Value = LVStro.ListCount - 1
End If
End Sub

I
Micros









Codin
Option
Dim d
Dim rs
-------
Private
Set db
Set rs
Subjec
If r

Exit
End If
If IsNu
Msg
ElseIf
Msg
Cbo
ElseIf
Msg
Cbo
ElseIf
Msg
Cbo
ElseIf
Msg
Cbo

Else
I[

oftAccess200
Form

ng:
n Compare
b As DAO.D
s As DAO.R
--------------
e Sub btnAd
b = Current
= db.Open
ct='" & Cbo
rs.EOF = Fa
MsgBox "Ple
t Sub
f
ull(cboid.Va
gBox "Pleas
IsNull(CboP
gBox "Pleas
oPCNum.Se
IsNull(CboS
gBox "Pleas
oSub.SetFoc
IsNull(CboH
gBox "Please
oHour.SetFo
IsNull(CboD
gBox "Please
oDate.SetFo

07


Database
Database
Recordset
--------------
dd_Click()
tDb
nRecordset(
oSub.Value
alse Then
ease Choos
alue) Then
se choose ID
PCNum) Th
se input PC
etFocus
Sub.Value)
se subject!"
cus
Hour.Value)
e choose th
ocus
Date.Value)
e choose da
ocus


---------
("select * fro
& "'")
se other Sub
D!"
hen
Number", ,
Then
, , "New Stu
) Then
he hour.", ,
) Then
ate.", , "New
om tblSubje
bject"
, "Number P
udent"
"New Stude
w Student"
Cambo
ect where I
PC"
ent"
dia Internatio
D=" & cboid
onal Coopera
d.Value & "
ation Institute
Page:3
" and
e
36

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:37

Dim str As String



str = "Insert into tblSubject Values(" & cboid.Value & "," & CboPCNum.Value & ",'" &
txtname2.Value & "','" & txtnameEng.Value & "','" & CboSub.Value & "','" & CboHour.Value &
"',#" & CboDate.Value & "#)"

db.Execute str
MsgBox "Success"
LoadInvoice
End If
End Sub
-------------------------------------------
Sub LoadInvoice()
Set db = CurrentDb
LVScore.ColumnHeads = True
LVScore.RowSourceType = "Table/Query"
LVScore.RowSource = "Select * from tblSubject order by id"
LVScore.ColumnCount = 7
cboid.Enabled = True
'txtStartDate.Value = FormatDateTime(Now(), vbShortDate)
'txtEndDate.Value = FormatDateTime(Now(), vbShortDate)
txtcount.Value = LVScore.ListCount - 1
End Sub
-------------------------------------------
Private Sub btnDelete_Click()
If MsgBox("Do you want to delete Record?", vbYesNo, "Delect Record") = vbYes Then
Set db = CurrentDb
Dim st As String
st = "delete from tblSubject where ID=" & cboid.Value & " And [Subject]='" & CboSub.Value
& "'"
db.Execute st
LoadInvoice
End If
End Sub
-------------------------------------------
Private Sub btnShowAll_Click()
Set db = CurrentDb
Dim st As String
st = "select * from tblSubject"
LVScore.RowSource = st
txtcount.Value = LVScore.ListCount - 1
End Sub
-------------------------------------------
Private Sub btnUpdate_Click()
If MsgBox("Do you want to Update record?", vbYesNo, "Update old Record") = vbYes Then
Set db = CurrentDb
Dim str As String
str = "Update tblSubject set[PC]=" & CboPCNum.Value & ",Name='" & txtname2.Value &
"',[English]='" & txtnameEng.Value & "',[Hours]='" & CboHour.Value & "',[Start Date]='" &
CboDate.Value & "' where ID =" & cboid.Value & " And [Subject]='" & CboSub.Value & "'"

db.Execute str
MsgBox "Has been Update Recode!", , "Cambodia International Cooperation Institute"

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:38

LoadInvoice
txtcount.Value = LVScore.ListCount - 1
End If
End Sub
-------------------------------------------
Private Sub cboid_Change()
Set db = CurrentDb
Set rs = db.OpenRecordset("Select * from tblInfo Where ID=" & cboid.Value & "")
If rs.EOF Then
MsgBox "Not found"
Else
txtname2.Value = rs(2)
txtnameEng.Value = rs(3)
CboPCNum.Value = rs(1)
End If
End Sub
-------------------------------------------
Private Sub Command33_Click()
DoCmd.OpenForm "FrmMain", acNormal
DoCmd.Close acForm, "FrmScore"

End Sub
-------------------------------------------
Private Sub Command52_Click()
If MsgBox("Do you want to close form?", vbYesNo, "Close Form") = vbYes Then
DoCmd.Close acForm, "FrmScore"
End If
End Sub
-------------------------------------------
Private Sub Form_Load()
'DoCmd.Maximize
LoadInvoice
loadName
CboDate = Now()
CboDate.Value = Format(Now(), "dd-mmm-yyyy")
cboid.Value = FrmInformation
End Sub
-------------------------------------------
Private Sub LVStro_Click()
txtID.Value = LVStro.Column(0, LVStro.ListIndex + 1)
txtPCNum.Value = LVStro.Column(1, LVStro.ListIndex + 1)
txtkhmer.Value = LVStro.Column(2, LVStro.ListIndex + 1)
txtEnglish.Value = LVStro.Column(3, LVStro.ListIndex + 1)
txtSex.Value = LVStro.Column(4, LVStro.ListIndex + 1)
txtNote.Value = LVStro.Column(5, LVStro.ListIndex + 1)
End Sub
-------------------------------------------
Private Sub txtSearch_KeyUp(KeyCode As Integer, Shift As Integer)
Dim str As String
If KeyCode = vbKeyReturn Then
If LVStro.ListCount > 0 Then
LVStro.SetFocus
LVStro.ListIndex = 0

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:39

End If
Else
LVStro.RowSource = "Select * from tblInfo where [Name English] like '" & txtSearch.Text &
"*'"
txtcount.Value = LVScore.ListCount - 1
End If
End Sub
-------------------------------------------
Sub loadName()
cboid.RowSource = "Select ID,[Name khmer],[Name English] from tblInfo ORDER BY ID
DESC"
cboid.ColumnHeads = True
cboid.ColumnCount = 2
End Sub
-------------------------------------------
Private Sub Form_Timer()
lblH.Caption = FormatDateTime(Now(), vbLongTime)
lblD.Caption = Format(Now(), "ddd dd-MMM-YYYY")
End Sub
-------------------------------------------
Private Sub Image31_Click()
Set db = CurrentDb
Dim s As String
Set rs = db.OpenRecordset("select * from tblSubject where Hours='" & CboSearch.Value & "'")

If rs.EOF Then
MsgBox "Not found!"
Else
s = "select * from tblSubject where Hours='" & CboSearch.Value & "'"
LVScore.RowSource = s
txtcount.Value = LVScore.ListCount - 1
End If
End Sub
-------------------------------------------
Private Sub LVScore_Click()
cboid.Value = LVScore.Column(0, LVScore.ListIndex + 1)
CboPCNum.Value = LVScore.Column(1, LVScore.ListIndex + 1)
txtname2.Value = LVScore.Column(2, LVScore.ListIndex + 1)
txtnameEng.Value = LVScore.Column(3, LVScore.ListIndex + 1)
CboSub.Value = LVScore.Column(4, LVScore.ListIndex + 1)
CboHour.Value = LVScore.Column(5, LVScore.ListIndex + 1)
CboDate.Value = LVScore.Column(6, LVScore.ListIndex + 1)
End Sub
-------------------------------------------
Private Sub LVScore_DblClick(Cancel As Integer)
Set db = CurrentDb
DoCmd.OpenForm "frmEnd", acNormal
Form_frmEnd.txtID.Value = LVScore.Column(0, LVScore.ListIndex + 1)
Form_frmEnd.txtpc.Value = LVScore.Column(1, LVScore.ListIndex + 1)
Form_frmEnd.txtnameKhmer.Value = LVScore.Column(2, LVScore.ListIndex + 1)
Form_frmEnd.txtnameEnglish.Value = LVScore.Column(3, LVScore.ListIndex + 1)
Form_frmEnd.txtsubject.Value = LVScore.Column(4, LVScore.ListIndex + 1)
Form_frmEnd.txthours.Value = LVScore.Column(5, LVScore.ListIndex + 1)

I [

Cambodia International Cooperation Institute


MicrosoftAccess2007 Page:40

Form_frmEnd.txtStartDate.Value = LVScore.Column(6, LVScore.ListIndex + 1)


Form_frmEnd.txtScore.SetFocus
End Sub
-------------------------------------------
Private Sub txtSearchName_KeyUp(KeyCode As Integer, Shift As Integer)
Dim str As String
If KeyCode = vbKeyReturn Then
If LVScore.ListCount > 0 Then
LVScore.SetFocus
LVScore.ListIndex = 0
End If
Else
LVScore.RowSource = "Select * from tblSubject where [English] like '" &
txtSearchName.Text & "*'"
txtcount.Value = LVScore.ListCount - 1
End If
End Sub
-------------------------------------------
Adding Code Export to Excel 1
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tbldb", "D:\test2.xls", True
MsgBox "Succesfully" '& strworksheetPath

Adding Code Show Object 2
Dim x As TableDef
Lsttables.RowSourceType = "Value List"
For Each x In CurrentDb.TableDefs
Lsttables.AddItem x.Name
Next
'display queries
Dim q As QueryDef
lstqueries.RowSourceType = "Value List"
For Each q In CurrentDb.QueryDefs
lstqueries.AddItem (q.Name)
Next
'display forms
Dim f As AccessObject
lstforms.RowSourceType = "Value List"
For Each f In CurrentProject.AllForms
lstforms.AddItem (f.Name)
Next

'display reports
Dim r As AccessObject
lstreports.RowSourceType = "Value List"
For Each r In CurrentProject.AllReports
lstreports.AddItem (r.Name)
Next
End Sub
Finish Project

You might also like