Object model in Visual Basic for Application (VBA)

 

Object-oriented environment

Concept

Example in Access

Object

A field in a table

A form

A control on a form

A query

Properties

Form controls have: color and font

Fields have: data type, size

Methods

Closing an application

Event

A form control gets focus (contain the cursor)

 

1.  2 types of objects

  • collection object/class
  • individual object

 

2.  Each application has its object model

  • Excel: Application à Workbooks à Worksheets à Range

 

  • Word

  • Access

 

  • PowerPoint: Application à Presentation à Slides

 

3.  Referring to objects

  • Location of the object in its hierarchy using dot member selection operator: item to its right is a member of the item to its left

e.g., Application.Presentations

  • Specific object can be referred to by its name enclosed in quotation marks or the object’s position number (or index) within the collection

e.g., Application.Presentations(“lecture6.ppt”).Slides(“slide3”) or Application.Presentations(1).Slides(3)

 

4.  Referring to an object’s properties (characteristics) or methods (actions)

  • Use the dot member selection operator to separate the object from its property/method

e.g., Application.Presentations.Count – returns the number of presentations currently open

e.g., Application.Presentations.Quitinstructs the application to close itself