Introduction.
Last week, we explored an example where a Base Class Object was passed through a Set Property Procedure, allowing it to become part of another object in memory. The passed object essentially became an extension or child object of the main object. In that earlier program, we passed the child object to the target object during the instantiation phase of our test program and then assigned values to the child object’s properties later in the code.
In the next example, we will take a slightly different approach.
For those who would like to go through the earlier Articles on the MS-Access Class Module, the links are given below:
This time, we will open both objects—ClsArea (the base class) and ClsVolume2 (the target class)—separately in our test program. We will assign values to the ClsArea base class properties before passing it to the ClsVolume2 target class object. Remember, the ClsVolume2 class has only one property, p_Height, and its Volume() method requires the Length and Width values from the base class ClsArea to calculate the volume.
Copy and paste the following sample Test Code into a Standard Module.
The SetNewVol2_2 Procedure.
Public Sub SetNewVol2_2() 'Method 2/2 Dim CA As ClsArea Dim Vol As ClsVolume2 Set CA = New ClsArea Set Vol = New ClsVolume2 CA.strDesc = "Bed Room" CA.dblLength = 90 CA.dblWidth = 10 Stop 'Here ClsArea class Object CA is passed to the ‘Property procedure Set CArea of ClsVolume2 object Vol Set Vol.CArea = CA 'Pass ClsArea obj to ClsVolume2 Vol.dblHeight = 10 'assign height to ClsVolume2 Debug.Print "Description", "Length", "Width", "Area", "Height", "Volume" With Vol.CArea Debug.Print .strDesc, .dblLength, .dblWidth, .Area(), Vol.dblHeight, Vol.Volume() End With Stop Set CA = Nothing Set Vol = Nothing End Sub
VBA Code Review.
In the first Dim statement, CA is declared as a ClsArea object and Vol as a ClsVolume2 object. The next two statements instantiate these objects in memory.
The following three statements assign values to the properties of the ClsArea object.
A Stop statement is then used to pause code execution, allowing us to inspect the property values of the object in the Locals window.
Next, the statement Set Vol.CArea = CA assigns the ClsArea object (CA) as a child object of the ClsVolume2 object (Vol).
After that, the dblHeight property of the ClsVolume2 object is assigned the value 10.
The subsequent statements, placed before the next Stop statement, print the property values from memory to the Debug window.
Finally, the last two Set statements release both objects from memory before the program ends.
Display the Locals Window.
Inspecting the Locals Window
-
Open the Locals Window
From the View menu in the VBA Editor, select Locals Window. -
Run the Code
-
Click anywhere in the middle of the code window.
-
Press F5 to run the program until it pauses at the Stop statement.
-
Alternatively, press F8 to run the code step by step, which lets you observe the changes in the Locals Window at each step.
-
-
Expand the Objects
Click the [+] symbol next to the object names in the Locals Window to expand and display their properties and current values. -
Observe Object References
-
Check the CArea and p_Area object references under the Vol object.
-
At this point, their values will show as Nothing because we have not yet passed the
CA
object to theVol
object.
-
-
Continue Running the Code
-
After reviewing the Locals Window, run the code until it pauses at the next Stop statement.
-
Now, the CArea
Set
Property procedure assigns the p_Area object reference to the ClsArea object, linking it into the ClsVolume2 object.
-
Next, we will try another variation of this example using the same two classes — ClsArea and ClsVolume2 — to demonstrate a slightly different approach.
-
New Class Module ClsVolume3.
1. Insert a new Class Module and change its name Property Value to ClsVolume3.
2. Copy and Paste the following VBA Code into the ClsVolume3 Class Module:
Option Compare Database Option Explicit 'Method three Private p_Height As Double Public p_Area As ClsArea Public Property Get dblHeight() As Double dblHeight = p_Height End Property Public Property Let dblHeight(ByVal dblNewValue As Double) p_Height = dblNewValue End Property Public Function Volume() As Double Volume = p_Area.dblLength * p_Area.dblWidth * Me.dblHeight End Function Private Sub Class_Initialize() Set p_Area = New ClsArea End Sub Private Sub Class_Terminate() Set p_Area = Nothing End Sub
In the code, p_Height
is declared as private property, while p_Area
is declared as a public ClsArea
object within the ClsVolume3
class. This means p_Area
appears as a property of the ClsVolume3
class, with its own accessible properties and methods for direct Get/Let operations in the user program (from a standard module). Although the ClsArea
object is exposed as a public property of ClsVolume3
, its internal properties and methods remain encapsulated within the ClsArea
class itself.
It is important to ensure that the ClsArea
class is fully developed and free of errors before using it inside other classes.
The Class_Initialize()
and Class_Terminate()
routines handle the lifecycle of the embedded object: The ClsArea
object is instantiated in Class_Initialize()
when a ClsVolume3
object is created, and released from memory in Class_Terminate()
When the user program ends.
The Testing Program.
The sample Test VBA Code is given below.
Copy and paste the code into the Standard Module.
Public Sub SNewVol3() 'Here ClsArea class is declared as a Public Property of ClsVolume3 Dim volm As ClsVolume3 Set volm = New ClsVolume3 volm.p_Area.strDesc = "Bed Room" volm.p_Area.dblLength = 15 'assign length volm.p_Area.dblWidth = 10 'assign width in clsArea volm.dblHeight = 10 'assign height to ClsVolume2 Debug.Print "Description", "Length", "Width", "Area", "Height", "Volume" With volm.p_Area Debug.Print .strDesc, .dblLength, .dblWidth, .Area, volm.dblHeight, volm.Volume End With Set volm = Nothing End Sub
Display the Locals Window (View -> Locals Window), if it is not already open.
Click somewhere in the middle of the code and press F8 to execute the VBA Code one line at a time and watch the Local Window to track what happens at each step.
All the above variants of the ClsVolume Class have been written with less Code, except the first example of the ClsVolume Class.
Working with the Recordset Object.
Next week, we will work with a built-in Object 'DAO.Recordset' and build a Class Module to:
Calculate and update a Field,
Sort the Data,
Print the sorted data in the Debug Window,
And create a Clone of the Table with sorted data.
That is a lot of action next week.
List of All the Links on this Topic.
Earlier Post Link References:
- MS-Access Class Module and VBA
- MS-Access VBA Class Object Arrays
- MS-Access Base Class and Derived Objects
- VBA Base Class and Derived Objects-2
- Base Class and Derived Object Variants
- Ms-Access Recordset and Class Module
- Access Class Module and Wrapper Classes
- Wrapper Class Functionality Transformation
- Ms-Access and Collection Object Basics
- Ms-Access Class Module and Collection Object
- Table Records in Collection Object and Form
- Dictionary Object Basics
- Dictionary Object Basics-2
- Sorting Dictionary Object Keys and Items
- Display Records from Dictionary to Form
- Add Class Objects as Dictionary Items
- Update Class Object Dictionary Item on Form