You are on page 1of 28

Introducing Visual Basic

for Applications (VBA)

Adaptation of `Lecture 4. Introducing Visual Basic


for Applications (VBA) from Universidad de Turabo
Carolina Pea-2013

Outline
Object-Oriented

Programming
The
Basic of Visual Basic for
Application (VBA)
Introducing the Visual Basic Editor
(VBE)
Exercises (ActiveObject, ActiveForm,
and
With-End
With
instruction
construct)

Object-Oriented Programming
(OOP)
In

the object-oriented programming,


we find objects entities that have
behaviors, that hold information, and
that can interact with one another.
Programming consists of designing a
set of
objects that model theObject
problem
Object
at hand.
1
4
data

Object
2
data

Object
3
data

data

Basic Concepts in OOP


Defining objects: Classes
.A Class is a set of objects that share
the same properties (state) and
behavior (methods). It is the intuitive
notion of a kind of objects.
.An object which follows the definition
of a class is said to be an Instance of
that Class.

1.

Basic Concepts in OOP (2)


Objects
.Real-world
objects
share
two
characteristics: They all have state
(properties) and behavior (methods).
.For example, dogs have state (name,
color, age, breed) and behavior
(barking,
fetching,
wagging
tail).
Students have state (name, student
number, courses they are registered
for, gender) and behavior (take tests,
attend courses, write tests, party).

2.

Basic Concepts in OOP (3)


Defining Objects: Property (State)
.Properties in a object are used to present
the structure of the objects: their
components and the information or data
contained therein. (e.g., name, owner,
weigth).
.Each property has a value or a reference to
another object. All objects of the same
kind have the same properties (although
may have different values). For example,
name, weight and breed are three
properties of the class Dog.

3.

Basic Concepts in OOP (4)


Defining
objects:
Method
(Behavior)
.Is how an object reacts, in terms of
state changes and interaction with
other
objects.
In
other
words,
methods described the behavior of
the objects.
.For example, Dog class defines three
methods: sit, beg and run which
indicate the behavior a dog can have.

4.

VISUAL BASIC FOR


APPLICATIONS (VBA)

About VBA
VBA

is a programming language
based on objects.
Visual Basic is the foundation on which
VBA was built.
The secret to using VBA with other
applications lies in understanding the
object model for each application.
VBA, after all, simply manipulates
objects.
Beginner's All-purpose Symbolic
Instruction Code

Objects ready
to use!!
9

About VBA (2)


Access

object model, for example,


exposes several very powerful data
analysis objects, such as forms,
reports, database connections, and
queries execution.
With VBA, you can work with these
objects
and
develop
automated
procedures.

10

The Basic of VBA


Code:

You perform
executing VBA code.

actions

in

VBA

by

Module: VBA modules are stored in an Access


file, but you view or edit a module by using the
Visual Basic Editor (VBE).
A VBA module consists of procedures.
11

VBE

12

The Basic of VBA (2)


Procedures:

A procedure is basically
a unit of computer code that performs
some action. VBA supports two types
of procedures: Sub procedures and
Function procedures.
Sub: A Sub procedure consists of a
series of statements and can be
executed in a number of ways.
Sub Test()
Sum = 1 + 1
MsgBox "The answer is " & Sum
End Sub

13

The Basic of VBA (3)


Function: A VBA module can also
have
Function
procedures.
A
Function procedure returns a single
value (or possibly an array). A
Function can be called from another
VBA procedure.
Here's an example of a Function
named AddTwo:
Function AddTwo(arg1, arg2)
AddTwo = arg1 + arg2
End Function
14

About Objects and


Collections

Objects
.VBA manipulates objects contained in
its host application. (In this case,
access is the host application.)
.Access provides you with more than
100 classes of objects to manipulate.

1.

15

About Objects and Collections


(2)
Examples

of objects in Access:

16

About Objects and Collections


(3)
The object hierarchy
.Objects can act as containers for
other objects. For example, Access is
an object called Application, and it
contains other objects, such as:
Forms objects (a collection of all
control objects). TextBox objects..
Reports objects (a collection of all
information objects)

2.

17

About Objects and Collections


(5)
About collections
.A collection is a group of objects of
the same class, and a collection is
itself an object.

3.

18

Properties and Methods


Object properties
.Every object has properties. A property can
be thought of as a setting for an object. You
can use VBA to determine object properties
and also to change them.
.You refer to properties by combining the
object with the property, separated by a
period.
.For example, a TextBox object has a property
called Value. Therefore, you can refer to the
value in TextBox on myForm as:

1.

Forms!myForm!TextBox.Value
19

Properties and Methods


(2)
Also,

you can write VBA code to set


the Value property to a specific value:

Sub ChangeValue()

Forms!myForm!TextBox.Value = 123
End Sub

The above procedure changes the


value displayed in TextBox by
changing the Value property.
20

Properties and Methods


(3)
Object methods
.In addition to properties, objects also
have methods. A method is an action that
you perform with an object.
.You specify methods by combining the
object with the method, separated by a
period.
.For example, one of the methods for a
Form object is Undo. This method resets a
form when its value has been changed

2.

21

The VBE Windows


22

VBE Menu
Bar

VBE
Toolbars
Project
Explore
r
Window

Code
Window

Sub: a
Declaring
Sub Procedure
(Required)
The keyword that

indicates the beginning of a


A procedure declared
with the Sub keyword
procedure.
must adhere to the following syntax:

Sub
name
([arglist])
[instructions]
[instructions]

End Sub

name:
(Required)
Any
valid
procedure name.
arglist: (Optional) Represents a list
of
variables,
enclosed
in
parentheses,
that
receive
arguments
passed
to
the
procedure.
instructions: (Optional) Represents
valid VBA instructions.
End Sub: (Required) Indicates the
end of the procedure.
23

Exercise 1: Hello World


1.
2.

Insert a new VBA module


Write the following procedure:

'Write "Hello World" in Text Box


Sub prog1()
miMensaje.Value = "Hello World"
End Sub

24

Executing Sub Procedures


Executing a procedure with the
Run Sub/UserForm command
.The VBE Run Sub/UserForm menu
command is used primarily to test a
procedure while you are developing it.
Choose Run
Run Sub/UserForm in
the VBE to execute the current
procedure (in other words, the
procedure that contains the cursor).
Or, use the Run Sub/UserForm button
on the Standard toolbar.
Or, press F5.
25

1.

Executing Sub Procedures


(2)

Executing a procedure from the


Macro dialog box
.If the cursor is not located within a
procedure when you issue the Run
Sub/UserForm command, VBE displays
its Macro dialog box so that you can
select a procedure to execute.
Choosing Excel's Developer Code
Macros command displays the Macro
dialog box
Or, you can also press Alt + F8 to
access this dialog box.
26

2.

Exercise 2
Create

a computer program that


writes the phrase "Welcome to IIS
2014-2" in a window.

27

References
Excel

2007 Power Programming


with VBA, John Walkenbach. Chapter
7.
VBA Excel 2002/2000, Ferm Vil.
Basic Concepts in Object Oriented
Programming, Ral Ramos-Polln
http://hep.fi.infn.it/JAVAmain.pdf

28

You might also like