lecture4_engi122_v1
DESCRIPTION
djdjTRANSCRIPT
Introducing Visual Basic for Applications (VBA)
Adaptation of “`Lecture 4. Introducing Visual Basic for Applications (VBA)” from Universidad de Turabo – Carolina Peña-2013
2
OutlineObject-Oriented ProgrammingThe Basic of Visual Basic for
Application (VBA)Introducing the Visual Basic Editor
(VBE)Exercises (ActiveObject, ActiveForm,
and With-End With instruction construct)
3
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 the problem at hand.
Object 1
data
Object 2
data
Object 3
data
Object 4
data
4
Basic Concepts in OOP 1. Defining objects: ClassesA 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.
5
Basic Concepts in OOP (2)2. ObjectsReal-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).
6
Basic Concepts in OOP (3)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.
7
Basic Concepts in OOP (4)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.
8
VISUAL BASIC FOR APPLICATIONS (VBA)
9
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.
Objects ready to use!!Beginner's All-purpose Symbolic
Instruction Code
10
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.
11
The Basic of VBA Code: You perform actions in VBA by
executing VBA code.
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.
12
VBE
13
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
14
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
15
About Objects and Collections 1. ObjectsVBA 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.
16
About Objects and Collections (2)Examples of objects in Access:
17
About Objects and Collections (3)2. The object hierarchyObjects 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)
18
About Objects and Collections (5)3. About collectionsA collection is a group of objects of the
same class, and a collection is itself an object.
19
Properties and Methods 1. Object propertiesEvery 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:
Forms!myForm!TextBox.Value
20
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.
21
Properties and Methods (3)
2. Object methodsIn 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
myForm.Undo
The VBE Windows22 VBE Menu
Bar
VBE Toolbars
Project Explore
r Window
Code Window
23
Declaring a Sub Procedure
Sub name ([arglist]) [instructions][instructions] ……
End Sub
Sub: (Required) The keyword that indicates the beginning of a procedure.
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.
A procedure declared with the Sub keyword must adhere to the following syntax:
24
Exercise 1: “Hello World”
1. Insert a new VBA module2. Write the following procedure:'Write "Hello World" in Text BoxSub prog1() miMensaje.Value = "Hello World"End Sub
25
Executing Sub Procedures1. Executing a procedure with the
Run Sub/UserForm commandThe 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.
26
Executing Sub Procedures (2)2. Executing a procedure from the
Macro dialog boxIf 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.
To more information you can read pages from 240 to 251 of the textbook.
27
Exercise 2Create a computer program that
writes the phrase "Welcome to IIS 2014-2" in a window.
28
ReferencesExcel 2007 Power Programming
with VBA, John Walkenbach. Chapter 7.
VBA Excel 2002/2000, Fermí Vilà.Basic Concepts in Object Oriented
Programming, Raúl Ramos-Pollánhttp://hep.fi.infn.it/JAVAmain.pdf