Learn Microsoft Access Advanced Programming Techniques, Tips and Tricks.

VBA Base Class and Derived Object-2

Last week we have created a Derived Class ClsVolume Object, using Class ClsArea as Base Class.  We have created Property Procedures in the Derived Class to expose the Base Class’s Properties and Function to the Object user programs.  This method demands repetition of all the property procedures of the Base Class in the derived class too.  Here, we explore how to create the same Derived ClsVolume Class without repeating the Property Procedures of the Base ClsArea Class.

We have learned the usage of Get and Let Property Procedures in Classes.  There is one more Property Procedure used in Classes:  The Set Property Procedure.  The Set Property Procedure directly assigns an Object to a Class Object of the same Type.

Before continuing further you may visit the earlier pages on this topic, if you have not already done so, the links are given below:

We shall create a different variant of the same ClsVolume Class Module, that we have created last week, using ClsArea as Base Class, with a different approach. 

Create a new Class Module and change it’s Name Property Value to ClsVolume2.

Copy and Paste the following Code into the Class Module ClsVolume2 and Save the Module:

Option Compare Database Option Explicit 'Method two-1 Private p_Height As Double Private 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 = CArea.dblLength * CArea.dblWidth * Me.dblHeight End Function

‘New Get and Set Property Procedure for ClsArea Objects Public Property Get CArea() As ClsArea Set CArea = p_Area End Property Public Property Set CArea(ByRef AreaValue As ClsArea) Set p_Area = AreaValue End Property

Select Compile Project Name from Debug Menu to compile the VBA Code in the Database to ensure that all VBA Project Code is Error Free.  If you have encountered any Error in your other VBA Programs, please track down the error, correct it and recompile your Project.  Otherwise VBA  intellisense that displays  list of properties and functions of Objects will not work.  That will not prevent us from assigning/retrieving values to/from Object Properties.  But, while learning it is important to see the Properties of an Object pops up and displays the list, as an assistant in Coding.

We have omitted all property procedures of ClsArea, created in the last version of ClsVolume Class, and replaced with a Get/Set Property Procedures.

Take note of the Set CArea() procedure. It’s ByRef parameter AreaValue is declared as ClsArea Object. It will accept the ClsArea Class Object, when passed to the Property Set CArea(ByRef AreaValue as ClsArea), in object variable AreaValue and assigns directly to the p_Area Property of ClsVolume2 Object.

The Get CArea() Property procedure returns the Object or it’s Property Values to the calling program.

In our earlier programs we have written Property procedures for individual elements (Length, Width, Height) of an object to assign/return  values To/From them.  Here, the difference is that we are passing an Object as Parameter to the Let Procedure and to retrieve an individual element (say lblHeight) we must address, like Vol.CArea.dblLength. The Get/Set Procedure name CArea become child object of the main Object when declared in the Main Program.

We will write a small program in the Standard Module to test our new derived Class Object ClsVolume2.

Insert a new Standard Module in your Project. Copy and paste the following Code into the Module and Save the code:

Public Sub SetNewVol2_1()
'Method 1/2
Dim Vol As New ClsVolume2

'ClsArea Object instantiated and passed to the
'Property Procedure Set CArea in ClsVolume2.

Set Vol.CArea = New ClsArea

Stop

Vol.CArea.strDesc = "Bed Room"
Vol.CArea.dblLength = 90
Vol.CArea.dblWidth = 10

Vol.dblHeight = 10 'assign height to ClsVolume2

Stop

Debug.Print "Description", "Length", "Width", "Area", "Height", "Volume"
Debug.Print Vol.CArea.strDesc, Vol.CArea.dblLength, Vol.CArea.dblWidth, Vol.CArea.Area, Vol.dblHeight, Vol.Volume

Set Vol.CArea = Nothing
Set Vol = Nothing

End Sub

.Let us take a quick look at the VBA Code above. First line instantiates the Class ClsVolume2 with the name Vol. After the next two comment lines the Set statement with the Vol.CArea Property Procedure is called and passes the New instantiated ClsArea Object as Parameter.

I put a Stop statement in the next line to give a pause in the Program to see how the object is being assigned to the Set CArea Object.  How? we will explore that in a minute?

Next four lines assigns values to the ClsArea Object and to the Height property of the ClsVolume2 Object.

The next Stop creates a pause in the Program so that we can inspect the memory how the values are kept in memory.

Next line prints the Headings in the Debug Window for the values printed on the next line.

Next line prints the values of Object Properties from memory into the Debug Window.

Let us run the Code and inspect the memory to see what happens there at each stage, where I put the Stop statement.

  1. Click somewhere in the middle of the code and press F5 to run the code and pause the program at the first Stop Statement.
  2. Select Locals Window from View Menu to open a new window below the Code Window, to display as how the ClsArea and ClsVolume2 Objects, their Properties and their member property procedures  are held in memory.  Sample image of the Locals Window is given below.


  3. Drag other Windows' sizing handles up to reduce their height to give more space for the display of Locals Window.  Better, close Debug Window for the time being, use Ctrl+G to bring it back when needed later.

    We can have a graphical view of all the objects and their Properties in the Locals Window.  The first name with the plus [+] symbol shows the name of the Standard Module, from where our program is running.

    The next plus [+] symbol with the name Vol is the ClsVolume2 instantiated Object in memory.

  4. Click on the [+] symbols to expand and display the details.

    You will find the next level of Objects and Properties.

    The [+]CArea indicates that this Object have next level of Properties and their Values.

    The dblHeight Get property Procedure comes directly under the Vol Object.

    The [+]p_Area is the Private Property declared as ClsArea Class in the ClsVolume2 Class.

    The p_Height is also the Private Property declared in the ClsVolume2.

  5. Click on the plus [+] symbols to expand the objects to show their Properties and Values.

    The expansion of  [+]CArea gives us the view of the ClsArea Object we have passed to the Set CArea() property procedure.

    The expansion of [+]p_Area gives the view of the ClsArea Property declared as Private.

    Note: The p_Area Private Property, of ClsVolume2 Class Object, and all it’s elements are accessible only through the CArea Object Property Get/Set Procedures to the outside world.

    In the Second column of the Locals window will show the values assigned to the Object Properties and currently no values in them.

    The Third Column shows the Data Type or Object Class Module Names.

  6. Press F5 to run the program further, till it is paused at the next Stop statement, to assign some values into the Object Properties.  The program will pause at the next Stop statement.  Check the Locals Window for change of Values.

Inside the CArea the first two lines with values 90, 10 and the last strDesc with value "Bed Room" are the Get Property Procedures. The p_Desc, p_Length and p_width are values assigned through Set Property Procedures to p_Area Property of ClsVolume2 Class Object.

The p_Area Object of ClsArea Class, declared as Private Property of ClsVolume2 is seen with its Get/Set Property Procedures and assigned values.

Check the Type Column of [-]CArea and [-]p_Area both Objects are derived from ClsArea Base Class.

Next Week we will try another approach with the same two object.  If you want to try yourself, here is the clue as how to try it out yourself.

  1. Instantiate ClsVolume2 and ClsArea Class as two different Object in the Standard Module Program.
  2. Assign values into both Object Properties.
  3. Assign the ClsArea instantiated Object to the CArea Object in ClsVolume2 Class Object, before printing the Values to the Debug Window.

In this example we can achieve the same result as we did in the above example, without repeating the Get/Let Property Procedures as we did in the ClsVolume Class Module.

  1. MS-Access Class Module and VBA
  2. MS-Access VBA Class Object Arrays
  3. MS-Access Base Class and Derived Objects
  4. VBA Base Class and Derived Objects-2
  5. Base Class and Derived Object Variants
  6. Ms-Access Recordset and Class Module
  7. Access Class Module and Wrapper Classes
  8. Wrapper Class Functionality Transformation
  9. Ms-Access and Collection Object Basics
  10. Ms-Access Class Module and Collection Object
  11. Table Records in Collection Object and Form
  12. Dictionary Object Basics
  13. Dictionary Object Basics-2
  14. Sorting Dictionary Object Keys and Items
  15. Display Records from Dictionary to Form
  16. Add Class Objects as Dictionary Items
  17. Add Class Objects as Dictionary Items
  18. Update Class Object Dictionary Item on Form
Share:

No comments:

Post a Comment

Comments are subject to moderation, before publishing.

Translate



PageRank
Subscribe in a reader
Your email address:

Delivered by FeedBurner

Search

Popular Posts

Search This Blog

Blog Archive

Powered by Blogger.

Follow by Email

Labels

Forms How Tos Functions MS-Access Security Reports msaccess forms Animations msaccess animation Utilities msaccess controls Access and Internet Class Module MS-Access Scurity MS-Access and Internet Queries msaccess reports Array External Links msaccess tips Accesstips Menus and Toolbars Objects Downloads MsaccessLinks Process Controls Property Art Work Event Collection Object Controls WithEvents msaccess How Tos Graph Charts VBA msaccessQuery Combo Boxes List Boxes Command Buttons Data Emails and Alerts Form Query Report Calculation Command Button Custom Wizards DOS Commands Data Type Dictionary Object ms-access functions msaccess functions msaccess graphs msaccess reporttricks Item Object Reference RaiseEvent msaccessprocess security advanced Access Security Add Custom Functions Field Type Fields Form Instances Key Macros Menus SubForm Top Values Variables msaccess email msaccess menus progressmeter Access2007 Auto-Number Copy Expression Join Methods Microsoft Numbering System Records Recordset Security Split Table Time Difference Utility Workgroup Wrapper Classes database ms-access msaccess wizards text tutorial vba code Access2003 Accounting Year Action Animation Attachment Binary Numbers Bookmarks Budgeting ChDir Color Palette Conditional Formatting Data Filtering Defining Pages Diagram Disk Dynamic Lookup Error Handler Excel Export External Filter Formatting Groups Hexadecimal Numbers Import Labels List Logo Macro Mail Merge Main Form Memo Monitoring Octal Numbers Operating System Paste Primary-Key Product Rank Reading Remove Rich Text Sequence SetFocus Summary Tab-Page Tables Union Query User Users Water-Mark Word automatically commands function hyperlinks iSeries Date iif msaccess msaccess alerts pdf files reference restore switch toolbar updating upload

Featured Post

WithEvents and Report Line Highlighting

Introduction This is really a re-run of an earlier Post: Highlighting Reports published during August, 2007.  The full Code was written on ...

Labels

Blog Archive

Recent Posts