You are on page 1of 53

Build Bright University

DATABASE MANAGEMENT
S y s t e m
Using Variables
I. Using Variables
RKb;PasasMrab;sresr Program TaMgGs;minfacMeBaHEtPas VBA enaHeTEtgEt
man Variables EdlCaGefrsMrab;TTYlyktMéleTArkSaTukenAkñúg Memory ehIyral;
GefrTaMgGs;RtUv)anrkSatMélTukenAeBlEdlkmµviFIdMeNIrkar.
I.1. Declaring Variables
edIm,IkarRbkasGefrenAkñúgPasa VBA elakGñkRtUvGnuvtþtam Syntax
xageRkam³
Syntax

Dim Variable_Name As Data_Type


Prepared by Sann Sothearath 2
smasPaBenAkñúg GFib,ay
Syntax
Ca Statement mYyEdleRbIsrM ab;eFVIkarRbkas
Dim
Gefr.
CaeQµaHrbs;Gefr EdlelakGñkeFVIkarkMNt;eday
Variable_Name
xøÜnÉg.
Ca Statement mYyEdleRbIsMrab;kMNt;RbePT
As
Data_Type eTAeGayGefr.

CaRbePTTinñn½yEdleFVIkarkMNt; faetIGefrEdl
Data_Type )anRbkasenaHGacrkSaTukTinñn½yCaRbePTGVI.
Prepared by Sann Sothearath 3
I.2. Converting Variable Type
cMeBaHkarsresr Program eyIgEtgEtCYbnUvsßanPaBNamYyEdlTamTaeGayeyIg
eFVIkarbMElgBIRbePTTinñn½ymYy
eGayeTACaRbePTTinñn½ymYyepSgeTotedIm,IykeTAeFVIkargar epSg². kñúgPasa VBA
GnuBaØateGayeyIgeRbInUvGnuKmn_mYycMnYndUcxageRkam³
- CBool  Convert Boolean
- CByte  Convert Byte
- Ccur  Convert Currency
- CDate  Convert Date
- CDbl  Convert Double
- Cint  Convert Integer
- CLng  Convert Long
- Csng  Convert Single
- CStr  Convert String
- Cvar  Convert Variant
Prepared by Sann Sothearath 4
I.3. Variable Scope & Life Time of Variable
munnwgelakGñkeFVIkarsikSaBIkMritdMeNIrkarrbs;GefrelakGñkRtUvdwgBI Module
Camunsin. enAkñúgPasa Microsoft Access 2003 Module RtUv)anEbgEckCaBIrKW ³
- Standard Module
- Form Module
I.3.1. Standard Module
Ca Object mYyEdlGnuBaØateGayelakGñkeFVIkarbegáItnUv Sub Program
(Procedures) b¤RbkasGefr. CaTUeTA Sub Program b¤k¾eFVIkarRbkasGefrTaMgLayNa
EdlelakGñkcg;eRbIvaenARKb; Module TaMgGs;.
I.3.2. Form Module
Ca Object mYyEdlGnuBaØateGayelakGñkeFVIkarbegáItnUv Sub Program b¤
RbkasGefrdUcKñaeTAnwg Standard Module Edr b:uEnþvaxusKñaRtg; ³
Prepared by Sann Sothearath 5
- ral;Gefr b¤ Sub Program EdlelakGñk)anbegáItenAkñúg Form Module
KWGaceRbIEtenAelI Form EdlelakGñkzitenAb:ueNÑaH.
- enAelI Form Module man Event EdlGaceGayelakGñkeFVIkarbBa¢aeTAelI
Object EdlmanenAelI Form b¤bBa¢aeTAelI Form )an.
cMeBaHkMritdMeNIrkarrbs;GefrenAkñúgPasa VBA én Ms Access 2003 RtUv)an
EbgEckCaBIrKW ³
- Global Variables
- Local Variables

I.3.3. Global Variables


cMeBaHGefrEdlmankMritCa Global elakGñkRtUveFIVkarRbkasvaenAEpñk General

Declaration ehIykMritdMeNIrkarrbs;GefrRtUvEbgEckecjCaBIrKW
Prepared by Sann Sothearath 6
- Public
Syntax
Public Variable_Name As Data_Type
- Private
Syntax
Private Variable_Name As Data_Type
or
Dim Variable_Name As Data_Type
I.3.4. Local Variables
cMeBaHGefrTaMgLayEdlkMritdMeNIrkarrbs;vaCa Local KwvaGaceRbI)anEtenAkñúg Sub
Procedures EdlelakGñk)anRbkasvaEtb:ueNÑaH. CaTUeTAral;GefrEdlmankMritdMeNIrkarCa

Local enAeBlRbkas RtUvcab;epþImeday key word Dim b¤ Static.


Syntax:
Dim Variable_Name As Data_Type
Syntax:
Static Variable_Name As Data_Type
Prepared by Sann Sothearath 7
I.4. Constant Variables
cMeBaHGefrEdlmantMélefr KWCaGefrTaMgLayNaEdlelakGñkminGaceFVIkarpøas;bþÚr
tMéleGayeTAva)aneT enAkñúgeBlEdlkmµviFIdMeNIrkar. enAkñúgPasa VBA edIm,IeFVIkar
kMNt;tMélGefrelakGñkRtUveRbInUv Syntax dUcxageRkam³
Const ContstanName [As Data_Type] = Value
Ex:
Option Explicit
Const Pi As Integer = 4
……………………………………………………………………
Private Sub Cmd Area_Click()
Dim Area, Radius As Double
Radius=InputBox(“Please, enter Radius”)
Area=Pi*Radius^2
MsgBox Area
End Sub
Prepared by Sann Sothearath 8
I.5. User Defined Data Types
elakGñk)ansikSarYcmkehIy BIemeronenAcMNucxagelIKWral;GefrTaMgGs;Gacman
RbePTTinñn½yEtmYyKt;. cMENkenAcMNucenHvijelakGñkeFVIkarRbkasGefrmYyEdlGac
manRbePTTinñn½yeRcInepSg²Kña.
edIm,IRbkasGefrmYyEdlmanRbePTTinñn½yelIsBImYy)an Pasa VBA TamTareGay
elakGñkeRbIR)as;nUv User Defined Data Types EdlmanTMrg;dUcxageRkam.
syntax: Option Explicit
Type varType Private Type Employee
Variable1 As varTtype EmpID As String *10
Variable2 As varTtype EmpName As String *30
… Sex As String *5
Variablen As varTtype DateOfBirth As Date
End Type End Type

Prepared by Sann Sothearath 9


II. Decision Structures
Access Basic GaceFIV Test elIlkçxN½Ð (Conditions) ehIybgðajlTpltam
kardak; lkçxN½Ðrbs;eyIg.
Decision Structures man ³
* If…Then
eRbI If…Then block edIm,IRbtibtiþkar Statement lkçx½NÐmYyb¤eRcIn GacCa
mYybnÞat; b¤eRcInbnÞat;k¾)an .
Syntax: 1- If Condition Then Statement
2- If Condition Then
Statements
End If
Prepared by Sann Sothearath 10
Ex:
- If Average < 5 Then Mention = "weak"
- If Average < 5 Then
Mention = "weak"
End If
sMKal;³ sresrmYybnÞat;mineRbI End If eT.
eRbIedIm,IbBa¢ak;elIeRcIn blocks kñúg Statements Edlman block

NamYyCalkçx½NÐBit ehIyTaMglkçx½NÐBit nigminBitRbtibtiþkarTaMgGs;.


Syntax:
If Condition1 then
Statement block1
ElseIf Condition2 then
Statement block2...
Else
Statement blockn
End If

Prepared by Sann Sothearath 11


Flow Chart

False
Condition Private Sub cmdok_Click()
If Not IsNumeric(txtAverage) Then
MsgBox "Invalid Number"
Exit Sub
True End If
If IsNull(txtAverage) = True Or txtAverage = "" Then
MsgBox "Please enter Average", vbInformation + vbOKOnly,
"Imformation"
If Statements ElseIf txtAverage < 50 Then
txtResult = "Fail": txtMention = "Weak"
txtResult.ForeColor = vbRed
ElseIf txtAverage >= 50 And txtAverage < 70 Then
txtResult = "Pass": txtMention = "Good"
ElseIf txtAverage > 70 And txtAverage < 90 Then
txtResult = "Pass": txtMention = "Very Good"
ElseIf txtAverage > 90 Then
Statements txtResult = "Pass": txtMention = "Excellency"
txtMention.ForeColor = vbBlue
End If

Prepared by Sann Sothearath End Sub 12


- Select Case Statement: RtUv)aneRbIsMrab;CMnYseGaykareRbI IF Statement
dEdl²eRcIndg. edIm,IeGaykan;Etgayyl;sUmelakGñkemIlnUv Sytax nig]TarhN_xag
eRkamenH
Syntax:
Select Case Variable
Case Is Comparison Operator value1
Statements dMeNIrkarRbsinebItMélén Value1 eFVk
I areRbobeFobCamYytMél Variable ehIy
TTYl)antMélBit
Case Value3 To Value4
Statements dMeNIrkarRbsinebItMélén Variable zitenAcenøaH Value3 nig Value4 rWk¾téM ;l
enH Variable esµIeGay Value3 rWeGayesµI Value4

Case Else
Statements dMeNIrkarRbsinebIlkçxN
½ ÐenAkñgú Case TaMgelIsuTEtminBit
End Select
Prepared by Sann Sothearath 13
Ex:
Private Sub cmdResult_Click()
If IsNumeric(txtAverage) = False Then
MsgBox "Invalid Number"
Me.txtAverage.SetFocus
Exit Sub
Else
Select Case Me.txtAverage
Case Is < 50
MsgBox "Fail"
Case 50 To 70
MsgBox "Good"
Case 71 To 85
MsgBox "Very Good"
Case Is > 85
MsgBox "Excellency"
End Select
End If
End Sub

Prepared by Sann Sothearath 14


III. Control Looping
Loop KWCa Statement mYyEdlmansar³sMxan;bMput enAkñúgPasasresrkmµviFIeRBaHva

GnuBaØateGayelakGñkdMeNIrkar Code mYybnÞat;b¤k¾eRcInbnÞat;dEdl² rhUtdl;lkçxNÐNamYy


eTIbvaQb;dMeNIrkar. enAkñúgPasa VBA Loop Statement mandUcCa³
- Do...Loop
- While…Wend
- For…Next
- Do...Loop: eRbI sRmab;RbtibtþelI Block én Statement EdlminkMNt;eBl
Do...Loop

(Indefinite Number of Time) . b:uEnþral;lkçx½NÐ)a:n;sµanva)aneFVIkarsMercfa RtUveFVIRbtibtþi

karbnþ b¤Qb;dUc If...Then Edllkçx½NÐRtUvEtCa Value b¤ Expression Edlkar)a:n;sµanCa Zero


(False) b¤ Non Zero (True) . tagtémøsRmab; False=0 , True=1

 Do...Loop xageRkamenH Statement RbtibtþiEtelIlkçx½NÐminBit (False)


Do Until Condition
Loop_Statement
Prepared by Sann Sothearath Loop 15
Flow Chart Option Compare Database
Dim i As Integer

Private Sub cmdok_Click()


Statements

Do Until i > 2
If txtPassword = 2 Then
MsgBox "Password is Correct"
True Condition Exit Sub
Else
False
MsgBox "Invalid Password"
i=i+1
Loop Block Exit Sub
Statements End If
Loop
MsgBox "Application is turn off"
Statements DoCmd.Quit acQuitSaveNone

End Sub

Prepared by Sann Sothearath 16


kalNa Access Basic RbtibtþielI Do...Loop, CadMbUgeFVI Test elI Condition RbsinebI
lkçx½NÐBit (True) varMlg Statement TaMgGs;ebIlkçx½NÐminBit (False) Access Basic Rbtibtþi
elI Statement ehIy Test mþgeTotTal;Et Do...Loop CYblkçx½NÐ True eTIbQb;.
 Do...Loop xageRkamenHvaRbtibtþielI Statement muneTIbeFVI Test elI Condition Ca
eRkay mann½yfa ³ Do...Loop enHeFVIkarelI Statement TaMgrbs;lkçx½NÐBit nigminBitsinrYc
eTIbeFVI Test elIlkçx½NÐebIBitvaecal ebIminBiteTIbvayk.
Do
Loop_Statement
Loop until Condition

Prepared by Sann Sothearath 17


Flow Chart Option Compare Database
Dim i As Integer

Private Sub cmdok_Click()


Statements

Do While txtPassword = 2
MsgBox "Password is Correct"
Loop Block Exit Sub
Statements Loop
If i > 2 Then
MsgBox "Application is turn off"
DoCmd.Quit acQuitSaveNone
False
Condition Else
MsgBox "Invalid Password"
True i=i+1
Exit Sub
Statements End If

End Sub

Prepared by Sann Sothearath 18


 xageRkamenHCa Do...Loop EdlRbtibtþielI Statement muneTIbeFVI Test

rklkçx½NÐ Bit.
Ex:
Sub Form_Load()
Dim x
Do
x = Inputbox (“Please Enter Name”)
Loop Until x = “Sann Sothearath”
End sub

 Do…While
Do While Conditions
Loop_Statements
Loop

Prepared by Sann Sothearath 19


Option Compare Database
Flow Chart Dim i As Integer

Private Sub cmdok_Click()


Statements
Do
If i > 2 Then
MsgBox "Application is turn off"
False Condition DoCmd.Quit acQuitSaveNone

Else
True
MsgBox "Invalid Password"
Loop Block i=i+1
Statements Exit Sub
End If
Loop Until txtPassword = 2
Statements MsgBox "Password is Correct"
Exit Sub
End Sub

Prepared by Sann Sothearath 20


cMeBaH Syntax TI1enHebIelakGñkeFVIkarBinitüeTAelI Flow Chart rbs;vaeyIg
eXIjfa³
- vaeFIVkarRtYtBinitülkçx½NÐmunnwgvadMeNIrkar Block Loop Statements.
- RbsinebIllkçx½NÐBitenaHral; Block Statement rbs; Loop RtUv)andMeNIr
rhUtdl;va CYblkçx½NÐ (Condition) rbs; Loop TTYl)antMélminBiteTIbvacakecj.
 xageRkamenHCa Do...Loop EdlRbtibtþielI Statement muneTIbeFVI Test rk

lkçx½NÐBit.
Do
Loop_Statement
Loop while Condition

Prepared by Sann Sothearath 21


Flow Chart Option Compare Database
Dim i As Integer

Private Sub cmdok_Click()


Statements

Do While i > 2
MsgBox "Application is turn off"
Loop Block DoCmd.Quit acQuitSaveNone
Statements Exit Sub
Loop
If txtPassword = 2 Then
MsgBox "Password is Correct"
True
Condition Exit Sub
Else
False MsgBox "Invalid Password"
i=i+1
Statements Exit Sub
End If

End Sub
Prepared by Sann Sothearath 22
Note:
niyayrYm Do until and Do while Ca Loop pÞúyKña³
Ex:
Do until x <> “”
GacsresrCa
Do while x = “”

nig Do until x > 5


GacsresrCa
Do while x < 5
eFVIkar)anl¥cMeBaHeBldgminkMNt;KWminkMNt;eGayvaeFVI
- For...Next: Do...Loop:

b:unµandgeLIy. edIm,IkMNt;eBlevla b¤dg)aneyIgeRbI For Loop mindUc Do Loop eT For


Loop eRbI Counter Variable EdlbegáIt (Increases) b¤bnßy (Decreases) témøkñúgry³eBl

EdlvavilCaxYbdEdl².
Prepared by Sann Sothearath 23
Syntax:
For Counter = Start To End [Step Increment]
Statement
Next Counter
Argument: Counter, Start, End and Increment Ca Numeric
Note:
Argument Increment GacCa Positive b¤ Negative ebI Increment Ca Positive, Start RtUvEtmantémøtUcCag
b¤esµIeTAnwg End ebIFMCag Statement kñúg Loop minGacRbtibtþi)ancUleTAkñúg Body Loop ehIybnþeTA
Statement epSgeTot.

- ebI Increment Ca Negative, Start RtUvEtmantémøFMCagb¤esµInwg End eTIb Statement GacRbtibtþicUl


Body Loop

-ebIeyIglb; Step ecal Access Basic yk Increment esµI 1 Ca Option .


Ex1: Increment Ca Positive (viC¢man)
Public Sub Beeps()
Dim x
For x = 1 To 20 Step1
Beep
Next x
End Sub
Prepared by Sann Sothearath 24
]TahrN_³ enHCa Sub Procedure sMelgrbs; Computer cMeBaH
+ x = 1 To 20 mann½yfaeGay Computer bBa¢ÚnsMelgeTA Speaker cMnYn 20dg

+ Step1 mann½yfaeGayvarab; (elat) tamlMdab;BI1rhUtdl;eyIgGaceGaytémøeTA

Step dUcxag eRkamenH

- Step 1: 1 , 2 , 3 , 4 , 5 , 6 ,......20 (émÖdg)

- Step 2: 1 , 3 , 5 , 7 , 11 , 13 , 15 , 17 , 19 (db;dg)

- Step 3: 1 , 4 , 7 , 10 , 13 , 16 , 19 (R)aMBIrdg)

- Step 4: 1 , 5 , 9 , 13 , 17 (R)aMdg)

- Step N: ..........................................................(Ndg)

+ Next x mann½yfaeRkayBIsMelg)anbnøW 20 dgehIyvaRtUvQb;ehIycab;epþm I bnøeW LIgenAeBlNaeyIg Run mþgeTot


dUecñHvabnøWsMelg 20 dgral;eBl Run mþg .
Ex2: Increment Ca Negative (GviC¢man)
Public Sub Beep()
Dim x
For x = 20 To 1 Step -1
Beep
Next x
End Sub

Prepared by Sann Sothearath 25


Procedures
I. Control Looping
Procedure pÞúkeTAedayPasarbs;va (Statement or Code)
EdlmanenAkñúg ABC or VBA sMrab;sMEdgecjnUvRbtibtþikar nig
karKNnatMélelx. Procedure TaMgGs;sßitenAkñúg Modules.
Procedure EckecjCaBIrKW

- Sub Procedure

- Function Procedure
Prepared by Sann Sothearath 26
I.1. Sub Procedure

eKeRbIR)as; Sub Procedure sMrab;sresr Code


kñúgkarehA Action rbs; Macro mkeRbI R)as;enAkñúg
Module vij. ehIykarsresr Code enHRtUvcaMBI

Action Argument nImYy²Edr.


Sub Procedure EckCa2KW
- Private Sub
- Public Sub
Prepared by Sann Sothearath 27
KW
I.1.1. Private Sub: Statement TaMgLayNaEdlsresrecjBI Form, Report NamYyeday
pÞal;EtmþgehIyGnuBaØateGayeyIgykeTAeRbIR)as;elI Form, Report Edl)ansresr Statement
enaHEtb:ueNÑaH minGaceGayeyIgykeTAeRbIR)as;EtelI Form or Report epSg eTot)aneLIy.
Ex: cUrbegáIt Button mYysRmab;biT Form Categories
- ebIk Form Categories Ca Form Design
- cucelI Command button mkKUselIkEnøgEdlsmKYr
- cuc Mouse xagsþaMyk Properties Rtg; On Click cucsBaØaRBYjyk

[Event Procedure] rYccucelI rYcsresr Code dUcxageRkam³


Private Sub CmdClose_Click()
Docmd.Close acForm,” frmSplash_Screen”,acSavePrompt
End Sub
- cucelI File  Close
Prepared by Sann Sothearath 28
+ Docmd. eRbIsRmab;ehA Action rbs; Macro mkeRbIkñúg Module
+ Space bar eRbIsRmab;qøgeTA Action Argument

+ Comma eRbIsRmab;qøgBI Action Argument mYyeTA Action Argument

mYyeTot
Prepared by Sann Sothearath 29
Ex2:
Private Sub CmdTotal_Click ()
Total = Quantity * UnitPrice
End Sub
Ex3:
Private Sub CmdClear_Click ()
Total = “ ”
End Sub
Ex4:
Private Sub Form_Open()
With Employee_ID
.FontSize = 9
.FontBold = True
.FontItalic = True
.FontName = “Arial”
.FontUnderline = False
End With
End Sub
Prepared by Sann Sothearath 30
I.1.2. Public Sub: sresr Statement ecjBI Module ehIyeKGacyk
vaeTAeRbI. vaGaceRbIenAelI Form, Report NamYyk¾)anEdr eGayEteyIgRtUv
kar edayRKan;EtsresreQµaHrbs;va.
Ex1:
1- kñúg Database Window cucelI Module Object  New
2- cucelI Insert  Procedure
3- kñúgRbGb; Type cucelI Sub
4- eRCIserIs Public kñúgRbGb; Scope
5- cucelI OK button
6- enAkñúg Public Sub vayBakü Docmd.Close
Prepared by Sann Sothearath 31
1- cucelI File  Save
2- ebIk Form NamYyCa Design View
begáIt Button Close edayyk Command mkKUselIépÞ Form
Prepared by Sann Sothearath 32
3- begáIt Button Close edayyk Command mkKUselIépÞ Form
4- cuc Mouse xagsþaMelI Close yk Properties rYccucelI
Event  Code Builder
5- enAkñúg Private Sub vayenA Call Close_Click
6- biTpÞaMg Modules rYcebIk Form Ca Form View
7- cucelI Button Close edIm,IeFVI Test elIva.
Ex2:
Public Sub UseCall ()
Shell (“C:\Windows\Explorer.exe”) , vbMaximizedFocus
End Sub

Prepared by Sann Sothearath 33


I.2. Function
KWCakmµviFIrg (GnuKmn_ ) EdlekIteLIgkarpþMúKñaén
Function Procedures

Statement CaeRcInEdlral; Statement TaMgenaHzitenAcenøaH Function nig End

Function Statement ehIykmµviFIrgenH edIrtYnaTICaGnuKmn_ Edlral;eQµaHrbs;

GnuKmn_ (Function)mantYnaTICaGefr EdlGnuBaØateGayelakGñkbBa¢ÚntMéleGay


eTAva)an.
Function Syntax

Private / Public FunctionName ([arguments[As Type]]) [As Type]


function statements
End Function

Prepared by Sann Sothearath 34


II. Parameter CaGefredIm,ITTYltMélBIxageRkAcUl Sub Program eBleKehA
Sub Program rW epÞrtMélecjBI Sub Program eBlGnuvtþcb;.
Parameters manBIrKW Value Parameter nig Reference Parameter (rWeKGacehA

Variable Parameter) edIm,IRbkas Parameter Syntax

+ ByVal Par1 as DataType,ByVal Par2 as DataType sRmab; Value parameter

+ ByRef Par1 as DataType,ByRef Par2 as DataType sRmab; Reference parameter

Using byRef And byVal Keyword


CaTUeTAenAeBlEdlelakGñkeFVIkarbegáIt Procedure ehIyRbsinebI Procedure
rbs;elakGñkman argument enaH Compiler TamTareGayelak GñkRtUveRbInUv byvale b¤
byref enABImuxral; argument TaMgGs;. RbsinebIelak GñkmineRbInUv Keyword

NamYyeTenaH Compiler nwgeFVIkarkMNt;yk byref KeyWord edays½yRbvtþi


Prepared by Sann Sothearath 35
II.1. Using byRef (byReferance) KeyWord

eyIgeXIjfaral;]TahrN_ Procedure Edl)aneRbIxagelI


suTEteRbInUv byRef key word TaMgGs;eRBaHfaeyIgmin)an
eRbI byRef b¤ byVal key word enABImuxral; arguments
eLIy.ral; arguments TaMgGs;EdleRbInUv byRef key
word KwvaminrkSatMéledImrbs; arguments TukenaHeT
eRBaHvayktMélenaHeTAeFVIedaypÞal;.
Prepared by Sann Sothearath 36
II.2. Using byVal (byValue) KeyWord

cMeBaH byVal KeyWord enAeBlEdlelakGñkeRbIvaenABI


muxral; Argument enaHral;tMélrbs; argument TaMgGs;
RtUv)anrkSatMéledImTukeRBaHeBlEdldMeNIrkar Compiler
minyk argument eTAeFVIkaredaypÞal;enaHeT KWvaeFVIkar
cMlgykEttMéleTAeFVIb:ueNÑaH.

Prepared by Sann Sothearath 37


Sub testbyRefArgument(ByRef a As Integer)
a=a+1
End Sub

Sub testbyValArgument(ByVal a As Integer)


a=a+1
End Sub

Private Sub cmdByref_Click()


Dim i%
i = 10
testbyRefArgument i
MsgBox i
End Sub

Private Sub cmdByval_Click()


Dim i%
i = 10
testbyValArgument i
MsgBox i
End Sub
Function Sum(ByVal a As Integer, ByVal b As Integer) As Integer
Sum = a + b
End Function
Private Sub cmdSum_Click()
txtTotal = Sum(Val(txtA), Val(txtB))
End Sub
Using Build In Function
III. Using Build In Functions
III.1. Using String Functions
III.1.1. MonthName
GnuKmn_ MonthName RtUv)aneRbIsMrab;sMElgBIelxén Month Argument Edlelak
Gñke)aHeGayeTACaeQµaHEx edayecjCaGkSrkat; b¤GkSreBj.
Syntax:
MonthName(Month As Long,[Abbreviate As Boolean] = False]) As String

Ex:
Private Sub Form_Load()
MsgBox MonthName(9, False) & _
"", vbInformation, "MonthName"
End Sub False True

Prepared by Sann Sothearath 40


III.1.2. WeekdayName
GnuKmn_ WeekdayName eRbIsMrab;TTYlykéf¶kñúgs)þah_CaGkSrkat; b¤GkSreBj.
Ex:
Private Sub Form_Load()
MsgBox WeekdayName(5, False, vbSaturday) & _
"", vbInformation, "MonthName"
End Sub

III.1.3. Asc
GnuKmn_ Asc RtUv)aneRbIsMrab;elx Ascii Code rbs;tYGkSrNamYy.
Syntax:
Asc(String As String) As Integer
Ex:
Private Sub Form_Load()
MsgBox Asc("A"), vbInformation, "KeyAscii"
End Sub

Prepared by Sann Sothearath 41


III.2. Date And Time Functions
III.2.1. DateAdd
GnuKmn_ DateAdd RtUv)aneRbIsMrab;bUkRbePTTinñny½ Caéf¶ExeTAtam Interval Argument.
Sytax:
DateAdd(Interval As String, Number As Double, Date)

Interval GFib,ay
yyyy sMrab;qñaM
q sMrab;RtImas
m sMrab;Ex
d sMrab;éf¶
w sMrab;elxerogéf¶éns)aþh_
ww sMrab;elxerogéf¶éns)aþh_énqñaM
h sMrab;em:ag
n sMrab;naTI
s sMrab;vni aTI
Prepared by Sann Sothearath 42
Ex1:
Private Sub Form_Load()
MsgBox DateAdd("M", 3, #6/10/1980#), vbInformation, "DateAdd"
End Sub

Ex2:
Private Sub Form_Load()
MsgBox Format(DateAdd("yyyy", 3, #9/10/1977#) & _
"", "mmmm-ddd-yyyy"), vbInformation, "DateAdd"
End Sub

Prepared by Sann Sothearath 43


III.2.2. DateDiff
GnuKmn_ DateDiff RtUv)aneRbIsMrab;KNnapldkTinñn½yRbePTCaéf¶ExeTAtam
Interval Argument .
Syntax:
DateDiff(Interval As String, Date1,Date2,[FirstDateOfWeek As
VbDayOfWeek=vbSunday],[FirstWeekOfYear As
VbFirstWeekOfYear=vbFirstJan1])
cMNaM
tMélén Date2 argument dkeGaytMélén Date1 argument .
Ex:
Private Sub Form_Load()
MsgBox DateDiff("yyyy", #9/1/1980#, #7/1/2007#) & _
"", vbInformation, "DateDiff"
End Sub

Prepared by Sann Sothearath 44


III.2.3. DatePart
GnuKmn_ DatePart RtUv)aneRbIsMrab;kat;ykTinñn½yecj BITinñn½yEdlmanRbePTCa
éf¶Ex eTAtam Interval Argument .
Syntax:
DateDiff(Interval As String, Date,[FirstDateOfWeek As
VbDayOfWeek=vbSunday],[FirstWeekOfYear As
VbFirstWeekOfYear=vbFirstJan1])

Ex:
Private Sub Form_Load()
MsgBox DatePart("yyyy", #9/1/1980#) & _
"", vbInformation, "DatePart"
End Sub

Prepared by Sann Sothearath 45


III.2.4. TimeValue
GnuKmn_ TimeValueRtUv)aneRbIsMrab;kat;yk em:ag naTI vinaTI ecjBI Time Argument.
Syntax:
TimeValue(Time As String)

Ex:
Private Sub Form_Load()
MsgBox TimeValue(“September/01/1980 3:00PM") & _
"", vbInformation, "TimeValue"
End Sub

Prepared by Sann Sothearath 46


III.2.5. Now
GnuKmn_ NowRtUv)aneRbIsMrab;TTYlykéf¶ Ex qñaM nig TMrg; em:agkñúgeBlbc©b,nñeTAtam
ma:suInkMuBüÚT½rrbs;elakGñk.
Syntax:
Now()

Ex:
Private Sub Form_Load()
MsgBox Now(),vbInformation, “Now"
End Sub

Prepared by Sann Sothearath 47


III.2.6. DLookup
GnuKmn_ DLookup KWCaGnuKmn_EdleRbIsMrab;TajyktMélEdlmanenAkñúg Field tam
Record NamYyEedlmankñúg Table.
Syntax:
DLookup(Expr As String,Domain As String,[Criterial])

Ex: cUrsresrkmµviFIbegáIt Log In dUcxageRkam ³

Prepared by Sann Sothearath 48


Gnuvtþn_
-begIáIt Table dUcxageRkam

- begáIt Forms cMnYn3

Prepared by Sann Sothearath 49


Prepared by Sann Sothearath 50
Source Code
Option Compare Database
Private intLogonAttempts As Integer

Private Sub Form_Open(Cancel As Integer)


'On open set focus to combo box
Me.cboEmployee.SetFocus
End Sub

Private Sub cboEmployee_AfterUpdate()


'After selecting user name set focus to password field
Me.txtPassword.SetFocus
End Sub
Prepared by Sann Sothearath 51
Private Sub cmdLogin_Click()

'Check to see if data is entered into the UserName combo box

If IsNull(Me.cboEmployee) Or Me.cboEmployee = "" Then


MsgBox "You must enter a User Name.", vbOKOnly, "Required Data"
Me.cboEmployee.SetFocus
Exit Sub
End If

'Check to see if data is entered into the password box

If IsNull(Me.txtPassword) Or Me.txtPassword = "" Then


MsgBox "You must enter a Password.", vbOKOnly, "Required Data"
Me.txtPassword.SetFocus
Exit Sub
End If

'Check value of password in tblEmployees to see if this matches value chosen in combo box
'lngenid =1

If Me.txtPassword.Value = DLookup("strEmpPassword", "tblEmployees", "[lngEmpID]=" &


Me.cboEmployee.Value) Then

lngMyEmpID = Me.cboEmployee.Value

Prepared by Sann Sothearath 52


'Close logon form and open splash screen

DoCmd.Close acForm, "frmLogon", acSaveNo


DoCmd.OpenForm "frmSplash_Screen"

Else
MsgBox "Password Invalid. Please Try Again", vbOKOnly, "Invalid Entry!"
Me.txtPassword.SetFocus
End If

'If User Enters incorrect password 3 times database will shutdown

intLogonAttempts = intLogonAttempts + 1
If intLogonAttempts > 3 Then
MsgBox "You do not have access to this database. Please contact your system
administrator.", vbCritical, "Restricted Access!"
Application.Quit
End If

End Sub

Prepared by Sann Sothearath 53

You might also like