You are on page 1of 18

Microsoft Access-VBA

i sual B c lusf B
orABp t t sf lnsr( B
)i c p µBm é Ën nñt f r( B
f gl i sual B
c lusf B
Ç®( ­î v f
í¸s f øf Bd( Ç ôM( Ç nAr( Ç O sf AruronB eoosf 2B
0
VBA là ngôn ngư h˜ ơng s,kie n (event-driven)
Muc ích:
ù Du ng à ̉ lam d̃ dang vie c nhap lie u (phım t˘t, giõ tringẫm à inh)
ù Bỗy lo i: lam cho ng˜ `i du ng hỉ u ro h´n vả lo i à ang xy ra
ù Kỉ m tra tınh hủp le cởa dư lie u ng˜ `i du ng nhap vao
ù Mộ rấng kh n˘ ng cởa Access, cung cứp them như ng chạc n˘ ng ma
Access khóng câ
ù Th,c hie n à ˜ ủc như ng thao tõ c phạc tỉp maAccess khóng cung cứp
ù Xũy d,ng à ˜ ủc như ng ạng dung hoan chınh cho ng˜ `i du ng khóng
phi lacóng nghe thóng tin c ng câ th̉ s dung à ˜ ủc
Microsoft Access-VBA
Objects in Access

Object or collection Description


Application object Represents the Microsoft Access
application.
Form object Represents an open form.
Forms collection Contains all currently open forms.
Report object Represents an open report.
Reports collection Contains all currently open reports.
Control object Represents a control on a form, report, or
section, or within another control.
Controls collection Contains all controls on a form or report.
Module object Represents a standard module or a class
module.
Modules collection Contains all currently open modules.
Reference object Represents a reference to an object
library.
References collection Contains all references that are currently
set.
DoCmd object Runs a macro action in Visual Basic.
Screen object Represents the current arrangement of
objects on the screen.
Microsoft Access-VBA
objects hierarchy
Microsoft Access-VBA
Forms and Form object

Object or
collection Is contained by Contains
Form object Forms collection Controls collection

Properties collection

Module object

Forms collection Application object Form objects


Microsoft Access-VBA

Dim frm As Form


Set frm = Forms!Employees
‘Other operations to this object…..
Microsoft Access-VBA
Function SetFormCaption(strFormName As String)
Dim frm As Form
' Open the form.
DoCmd.OpenForm strFormName
' Return a reference to the Form object.
Set frm = Forms(strFormName)
' Change the form's caption.
frm.Caption = Date
End Function
This command will hide the form
Form_Employees.Visible = True
Microsoft Access-VBA

' Add this procedure to form module.


Private Sub Form_Load()
' Initializes random number generator.
Randomize
' Sets BackColor property of form section.
Me.Section(acDetail).BackColor = RGB(Rnd * 256, Rnd *
256, Rnd * 256)
End Sub
Microsoft Access-VBA
Private Sub Form_Load()
' Passes reference to current form to ChangeBackColor
procedure.
ChangeBackColor Me
End Sub

' Add this procedure to standard module.


Public Sub ChangeBackColor(frm As Form)
Randomize
frm.Section(acDetail).BackColor = RGB(Rnd * 256, Rnd *
256, Rnd * 256)
End Sub
Microsoft Access-VBA

Object or
collection Is contained by Contains
Report object Reports collection Controls collection
Properties collection
Module object
Reports collection Application object Report objects
Microsoft Access-VBA
Dim rpt As Report
Set rpt = Reports!Invoice' Returns a reference to the Invoice
report.
Set rpt = Reports("Invoice") ' Returns a reference to the
Invoice report.
Set rpt = Reports(0) ' Returns a reference to the
first report in
' the collection.
Microsoft Access-VBA
Object or
collection Is contained by Contains
Control object Controls collection Controls collection, if
the control is either an
option group or a tab
control

Properties collection

Hyperlink object

Control objects

Controls collection Form objects

Report objects

Control objects, if the


control is an option
group, tab control, text
box, option button,
toggle button, check
box, combo box, list
box, command button,
bound object frame, or
unbound object frame
Microsoft Access-VBA
Control Description
BoundObjectFrame Displays a picture, chart, or OLE object
stored in a Microsoft Access table.
CheckBox Indicates whether an option is selected.
ComboBox Combines a list box and a text box.
CommandButton Starts an operation when the user clicks
it.
Image Displays a picture.
Label Displays descriptive text.
Line Displays a horizontal, vertical, or diagonal
line.
ListBox Displays a list of values.
ObjectFrame Displays a picture, chart, or OLE object
that is not stored in a table.
OptionButton Indicates whether an option is selected.
OptionGroup Displays a set of options together.
Page Displays controls on a page of a tab
control.
PageBreak Marks the start of a new screen or printed
page.
Rectangle Displays a rectangle.
SubForm/SubReport Displays a form within another form or a
report within another report.
TabControl Displays multiple pages, each of which
can contain controls.
TextBox Displays text data.
ToggleButton Indicates whether an option is on or off.
Microsoft Access-VBA
Control Description
Animation Displays animations stored in .avi files.
TabStrip Displays multiple pages, each of which
can contain multiple controls.
ListView Displays data items in one of four list
views.
TreeView Displays data in an expandable tree
format.
ImageList Contains a set of images for use with
other ActiveX controls.
ToolBar Displays a custom toolbar with buttons.
StatusBar Displays status information associated
with a form.
ProgressBar Shows the progress of a lengthy
operation by filling a rectangle with
blocks from left to right.
Slider Reflects a value or a range of values with
a movable slider.
RichTextBox Displays text with rich text formatting
features.
CommonDialog Displays one of a standard set of dialog
boxes for operations such as opening,
saving, and printing files or selecting
colors and fonts.
UpDown Increments or decrements numbers, or
scrolls through a range of values or a list
of items.
Winsock Provides easy access to Transfer Control
Protocol (TCP) and User Datagram
Protocol (UDP) network services.
Microsoft Access-VBA

Set txt = Me!LastName ' Returns reference to


LastName control on
' form in which code is
running.
‘We can now manipulate this control, such as get the text value
Microsoft Access-VBA
The DoCmd Object
You can use the DoCmd object to carry out macro actions
Macro actions perform common operations that aren't supported
by other objects.
For example, you can use methods of the DoCmd object to open,
save, or close tables, forms, queries, reports, macros. You can also
use methods of the DoCmd object to maximize, minimize, or
restore a window.
Microsoft Access-VBA
Data Access Objects (DAO) – provides interface for manipulating
database
Microsoft Access-VBA
Private Sub Form_Close()
If Quyen = 0 Then
DoCmd.Quit
Else
DoCmd.OpenForm "Main"
End If
End Sub
Microsoft Access-VBA
Sub Test()
Dim Rs As Recordset
Set Rs = CurrentDb.OpenRecordset("MaVT", dbOpenDynaset)
Rs.AddNew
Rs!mavt = "New Item"
Rs!d_giai = "Ban ghi moi"
Rs.Update
Rs.Close
Set Rs = Nothing
End Sub

You might also like