2. Introduction
When can’t find a readymade macro action that
does the job you want, you can use Visual Basic
code.
Example:
− Modify a whole batch of records at once.
− Perform complex calculations.
− Interact with other programs on computer.
− Write even more complicated validation
routines that lock out bad data.
3. Understanding VBA Terminology
Some basic VBA terminology:
− Statement: A word or combination of words
that constitutes an instruction to be
performed by the VBA engine.
− Procedure: A collection of VBA statements
that are grouped together to perform a certain
task. There are two types of procedures:
Subroutines: perform a single task, no
return.
Functions: perform a task and then return a
value
4. Understanding VBA Terminology
− Module: Procedures live in modules. A
module consists of one or more procedures
− Variable: is nothing more than a name
applied to represent a data value. Variable is
used to hold values such as customer
names, dates, and numeric values
manipulated by the VBA code.
5. The Visual Basic Editor
Visual Basic code is stored in database, but it
need a different tool to view and edit it, called
the Visual Basic editor.
Open the Visual Basic editor:
In the Access ribbon, choose Database
Tools➝ Macro ➝ Visual Basic
6. The Visual Basic Editor
The Visual Basic editor window is divided into
three main regions.
− Project window: shows all the modules in
your database.
− Properties window: which shows settings for
the currently selected item in the Project
window.
− Document window: Edit code region.
7. Adding a New Module
In the Visual Basic editor’s menu, choose Insert
➝ Module.
8. Adding a New Module
When add a new module, the Visual Basic
editor automatically opens a code window that
shows the contents of that module.
Initially, a brand-new module has just one line
of code:
Option Compare Database
− This line is an instruction that tells Visual
Basic how to handle operations that compare
pieces of text.
9. Adding a New Module
Before write code you should add one more
instruction to the top of code file:
Option Explicit
This instruction tells Visual Basic to use stricter
error checking, which catches common typos
when using variables.
10. Simplest Possible Code Routine
Every module are one or more subroutines. A
subroutine is a named unit of code that
performs a distinct task.
Subroutines start with the word Sub followed
by the name of the subroutine and end with the
statement End Sub
Example:
Sub MyCodeRoutine()
MsgBox "Witness the power of my code."
End Sub
11. Module types
Access stores code in two places:
− Class modules: attached to the form or
report, it executes when the form or report is
opened, the class module does not appear in
the modules section.
− Standard modules: Code can also be stored
in modules section. Standard modules is
accessible to all objects in your database.
12. Responding to a Form Event
Open a form in Design mode.
Turn off Control wizard button.
Add a new button.
In the Property Sheet, choose the Event
tab, and then select the button’s On Click
event
Click the ellipsis (…) in the On Click Event box
Select Code Builder and click OK