Microsoft Access 2010 Bible

(Rick Simeone) #1

Chapter 28: Object-Oriented Programming with VBA


989


Property Get


This is the basic syntax of the Property Get:


Public Property Get <PropertyName>() As <DataType>
<PropertyName> = <PrivateVariable>
End Property

Notice the similarities between a Property Get and a VBA function. The Property Get is
declared as a particular data type, and the property is assigned a value within the body of the prop-
erty. The syntax is identical to any VBA function.


This is the Property Get from the Employee class module in the example application accom-
panying this chapter:


Public Property Get LastName() As String
LastName = m_LastName
End Property

The Property Get executes whenever the property’s value is assigned to a variable or otherwise
used by the application. For example, the following VBA statement executes a Property Get
named LastName in the Employee class module (objEmployee has been declared and instan-
tiated from the Employee class):


strLastName = objEmployee.LastName

Notice that this statement doesn’t directly reference the Property Get. Because the obj
Employee object was created from the Employee class, the VBA engine knows to run the
Property Get because a variable is assigned the value of the LastName property. In other
words, the VBA engine gets the LastName property value from the class.


In this example, the Property Get is very simple and only returns the value of the private vari-
able. However, you could have a much more complex Property Get that performs data transfor-
mation on the value or retrieves the value from a database file, an .ini file, the operating system,
or some other source.


This example also illustrates the simplified programming possible with object-oriented techniques.
A single VBA statement in the application’s consumer code is enough to run whatever complex
operation is necessary to retrieve the value of the property. The consumer is never aware of the
logic supporting the property.


Exploring property-value persistence


At this point, you know that properties can be read/write, read-only, or write-only. What hasn’t
been explained is where the property persists the value when the property is written, and where
the property gets its value when the property is read.

Free download pdf