banner



How To Create A Class In Vba

Working with Class Modules

Classes are the blueprints for an object. They contain the implementation details, which are hidden from users (programmers who use your custom objects). In object-oriented programming with VBA, classes are implemented as class modules.

Class modules do not exist in memory. Rather, the instance of the class known as the object does. Multiple instances of a single class can be created. Each instance (object) created from a class shares the same access to the class's methods and properties. Even though multiple objects created from one class share the same characteristics, they are different in two ways. First, objects instantiated from the same class can have different property values. For example, an object called Bob instantiated from the Person class may have its hairColor property value set to brown, whereas an object called Sue, also instantiated from the Person class, could have its hairColor property value set to blond. Second, objects instantiated from the same class have unique memory addresses.

In OOP terms, an instance refers to the object that was created from a class. The term instantiate means to create an object from a class.

To create a class module in Access VBA, simply open a Visual Basic window (VBE) and select the Class Module menu item from the Insert menu. Microsoft VBA automatically creates the class module for you as depicted in Figure 10.1.

A newly created class module.

A newly created class module.

Modulos Programacion

By default, VBA class modules contain two events called Initialize and Terminate. These events can be accessed through the Code window shown in Figure 10.2.

The Initialize and Terminate events are accessed through the VBE Code window.

The Initialize and Terminate events are accessed through the VBE Code window.

Vba Class Object

The Initialize event for a class module is triggered each time the class is instantiated (created) using the New or Set keywords. The class module's Initialize event is similar to that of a constructor in OOP languages such as Java and C++. It is used to execute code when the object is first created. For example, you may wish to initialize certain member variables each time an instance of your class is created.

The Terminate event is triggered each time the instance is removed from memory. You can place code in this event procedure to free up other objects from memory or finalize any necessary transactions.

Another common use of the Initialize and Terminate events is in debugging your applications. If you'd like to know each time your application creates and destroys one of your custom objects, simply use the Initialize and Terminate events, like I've done here.

Private Sub Class_Initialize()

Debug.Print "Object created." End Sub

Private Sub Class_Terminate()

Debug.Print "Object destroyed." End Sub

Microsoft recommends not using message boxes in the Initialize and Terminate events, which requires Windows messages to be processed.

Continue reading here: Property Procedures

Was this article helpful?

How To Create A Class In Vba

Source: https://www.engram9.info/access-vba-programming-2/working-with-class-modules.html

Posted by: mitchelltheinder1941.blogspot.com

0 Response to "How To Create A Class In Vba"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel