Introduction.
Last week we tried an example to pass a Base Class Object, through the Set Property Procedure, to become part of the Object in memory. The passed object becomes an extension or Child-Object of the Main Object in memory. In our earlier program, passing the child Object to the Target Object was done in the instantiating phase of our test program. We have assigned values to the passed Object Properties in the later part of the program. The next example is slightly different.
For those who would like to go through the earlier Articles on MS-Access Class Module the links are given below:
This time we will open both Objects (ClsArea – the base class, ClsVolume2 – the target Class) separately in our test program. Assigning values in the Base Class ClsArea Properties, before passing them to the target Class ClsVolume2 Object. Remember the Volume2 Class has only one Property, the p_Height Property, and its Method Volume() needs the Length and Width Values of the Base Class ClsArea to calculate 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 defined as ClsArea Object and Vol as ClsVolume2 Object. The next two statements instantiate both objects in memory.
The next three statements assign values to the properties of the ClsArea Class Object.
The Stop statement gives a pause in the Code execution so that we can verify the Object Property values in the Locals Window.
The Set Vol.CArea = CA statement assigns the ClsArea Class Object CA, as a child object into the Vol (ClsVolume2) Object.
In the Next step dblHeight Property of ClsVolume2 Class Object is assigned with the value 10.
The following statements before the Stop statement print the Values from memory to the Debug Window.
The next two Set Statements remove the Objects from memory, before ending the program.
Display the Locals Window.
Select Locals Window Option from the View Menu.
Click somewhere in the middle of the Code and press F5 to run the code till the program pauses at the Stop statement. Alternatively, you can press F8 to run the code one step at a time to inspect the Locals Window for changes, at each step.
Click on the [+] Symbol to expand and display both Objects Properties and values.
Check the CArea and p_Area Object reference in the Value column of the Vol Object. The Value in there is showing as Nothing because we have not yet passed CA Object to the Vol Object.
If you have finished viewing the Locals Window contents, then run the code till the next Stop statement. Now, the CArea Get Property Procedure and p_Area Object are assigned to the ClsArea Class Object.
We will try another Variant example of both these two Classes ClsArea and ClsVolume2.
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
Check the Code from the beginning: p_Height declared as Private property. The p_Area Property of ClsVolume3 Class is declared as a Public ClsArea Object. That means p_Area will appear as a Property of the ClsVolume3 Class with its own displayable properties for direct Get/Let operations in the User Program, in the Standard Module. Even though ClsArea Class Object has been declared as Public Property of ClsVolume3 Class, its Properties and Methods are encapsulated in ClsArea Class itself.
The ClsArea Class must be a fully developed object, and be error-free to use it within other Objects.
Check the Class_Initialize() and Class_Terminate() Sub-Routines. The ClsArea Object is instantiated in the Class_Initialize() Code and removes the Object from memory in Class_Terminate() Code 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 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
No comments:
Post a Comment
Comments subject to moderation before publishing.