Microsoft Access VBA
Transcript of Microsoft Access VBA
-
8/14/2019 Microsoft Access VBA
1/18
Microsoft Access-VBA
isualBclusfBorABpttsflnsr(B)icpBm n nt fr(Bfgl isualBclusfB( -vfs ffBd( M( nAr( OsfAruronBeoosf2B0
VBA l ngn ng hng s, kien (event-driven)
Muc ch:
Dung lam d dang viec nhap lieu (phm tt, gi tri ngm inh)
By loi: lam cho ng`i dung hi u ro hn v loi ang xy ra
Ki m tra tnh hp le ca d lieu ng`i dung nhap vao
Mrng kh nng ca Access, cung cp them nhng chc nng maAccess khng c
Th,c hien c nhng thao tc phc tp ma Access khng cung cp
Xy d,ng c nhng ng dung hoan chnh cho ng`i dung khngphi la cng nghe thng tin cng c th s dung c
-
8/14/2019 Microsoft Access VBA
2/18
Microsoft Access-VBA
Object or collection Description
Application object Represents the Microsoft Accessapplication.
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, orsection, or within another control.
Controls collection Contains all controls on a form or report.
Module object Represents a standard module or a classmodule.
Modules collection Contains all currently open modules.
Reference object Represents a reference to an objectlibrary.
References collection Contains all references that are currentlyset.
DoCmd object Runs a macro action in Visual Basic.
Screen object Represents the current arrangement ofobjects on the screen.
Objects in Access
-
8/14/2019 Microsoft Access VBA
3/18
Microsoft Access-VBA
objects hierarchy
-
8/14/2019 Microsoft Access VBA
4/18
Microsoft Access-VBA
Object orcollection Is contained by Contains
Form object Forms collection Controls collection
Properties collection
Module object
Forms collection Application object Form objects
Forms and Form object
-
8/14/2019 Microsoft Access VBA
5/18
Microsoft Access-VBA
Dim frm As Form
Set frm = Forms!Employees
Other operations to this object. .
-
8/14/2019 Microsoft Access VBA
6/18
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
-
8/14/2019 Microsoft Access VBA
7/18
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
-
8/14/2019 Microsoft Access VBA
8/18
Microsoft Access-VBA
Private Sub Form_Load()
' Passes reference to current form to ChangeBackColorprocedure.
ChangeBackColor Me
End Sub
' Add this procedure to standard module.
Public Sub ChangeBackColor(frm As Form)
Randomizefrm.Section(acDetail).BackColor = RGB(Rnd * 256, Rnd *
256, Rnd * 256)
End Sub
-
8/14/2019 Microsoft Access VBA
9/18
Microsoft Access-VBA
Object or
collection Is contained by Contains
Report object Reports collection Controls collectionProperties collection
Module object
Reports collection Application object Report objects
-
8/14/2019 Microsoft Access VBA
10/18
Microsoft Access-VBA
Dim rpt As Report
Set rpt = Reports!Invoice' Returns a reference to the Invoicereport.
Set rpt = Reports("Invoice") ' Returns a reference to theInvoice report.
Set rpt = Reports(0) ' Returns a reference to thefirst report in
' the collection.
-
8/14/2019 Microsoft Access VBA
11/18
Microsoft Access-VBA
Object orcollection Is contained by Contains
Control object Controls collection Controls collection, ifthe control is either anoption group or a tabcontrol
Properties collection
Hyperlink object
Control objects
Controls collection Form objects
Report objects
Control objects, if the
control is an optiongroup, tab control, text
box, option button,
toggle button, check
box, combo box, list
box, command button,
bound object frame, or
unbound object frame
-
8/14/2019 Microsoft Access VBA
12/18
Microsoft Access-VBAControl Description
BoundObjectFrame Displays a picture, chart, or OLE objectstored 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 clicksit.
Image Displays a picture.
Label Displays descriptive text.
Line Displays a horizontal, vertical, or diagonalline.
ListBox Displays a list of values.
ObjectFrame Displays a picture, chart, or OLE objectthat 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 tabcontrol.
PageBreak Marks the start of a new screen or printedpage.
Rectangle Displays a rectangle.
SubForm/SubReport Displays a form within another form or areport within another report.
TabControl Displays multiple pages, each of whichcan contain controls.
TextBox Displays text data.
ToggleButton Indicates whether an option is on or off.
-
8/14/2019 Microsoft Access VBA
13/18
Microsoft Access-VBAControl Description
Animation Displays animations stored in .avi files.
TabStrip Displays multiple pages, each of whichcan contain multiple controls.
ListView Displays data items in one of four listviews.
TreeView Displays data in an expandable treeformat.
ImageList Contains a set of images for use withother ActiveX controls.
ToolBar Displays a custom toolbar with buttons.
StatusBar Displays status information associatedwith a form.
ProgressBar Shows the progress of a lengthyoperation by filling a rectangle withblocks from left to right.
Slider Reflects a value or a range of values witha movable slider.
RichTextBox Displays text with rich text formattingfeatures.
CommonDialog Displays one of a standard set of dialogboxes for operations such as opening,saving, and printing files or selectingcolors and fonts.
UpDown Increments or decrements numbers, or
scrolls through a range of values or a listof items.
Winsock Provides easy access to Transfer Control
Protocol (TCP) and User DatagramProtocol (UDP) network services.
-
8/14/2019 Microsoft Access VBA
14/18
Microsoft Access-VBA
Set txt = Me!LastName ' Returns reference toLastName control on
' form in which code isrunning.We can now manipulate this control, such as get the text value
-
8/14/2019 Microsoft Access VBA
15/18
Microsoft Access-VBAThe DoCmd Object
You can use the DoCmd object to carry out macro actions
Macro actions perform common operations that aren't supportedby other objects.
For example, you can use methods of the DoCmd object to open,save, or close tables, forms, queries, reports, macros. You can alsouse methods of the DoCmd object to maximize, minimize, orrestore a window.
-
8/14/2019 Microsoft Access VBA
16/18
Microsoft Access-VBA
Data Access Objects (DAO) provides interface for manipulatingdatabase
-
8/14/2019 Microsoft Access VBA
17/18
Microsoft Access-VBA
Private Sub Form_Close()
If Quyen = 0 Then
DoCmd.Quit
Else
DoCmd.OpenForm "Main"
End If
End Sub
-
8/14/2019 Microsoft Access VBA
18/18