Part IV: Professional Database Development
988
Property Let
The Property Let procedure assigns a value to a property. The property’s value is passed into the
procedure as an argument, and the value is then assigned to the class module’s private variable that
stores the property’s value.
The following example is a prototype for any Property Let procedure:
Public Property Let <PropertyName>(Value As <DataType>)
<PrivateVariable> = Value
End Property
Tip
The property’s argument can be named anything you want. I always use Value as the argument name.
Consistently using Value is simpler than assigning a meaningful name to the argument and is consistent with
how property values are assigned to built-in Access properties.
The following example is from the Employee class module:
Public Property Let LastName(Value As String)
m_LastName = Left$(Value, 20)
End Property
This small example hints at the power of property procedures. Notice that the Value argument is
a string. The statement within the property procedure assigns only the 20 leftmost characters of
the Value argument to the m_LastName variable. This is because the LastName field in the
Northwind Employees table only accepts 20 characters. Many database systems generate errors if
more characters are sent to a field than the field can hold.
Tip
Adding a little bit of logic to a property procedure can go a long way toward bulletproofing an application.
Property Set
The syntax of Property Set is parallel to the Property Let procedure. The only difference is
that the argument is an object data type, and the VBA Set keyword is used for the assignment
within the body of the Property Set. The following is an example of hypothetical Property
Set procedure that accepts a recordset object and assigns it to a private variable named m_
Products:
Public Property Set Products(Value As ADO.Recordset)
If Not Value Is Nothing Then
Set m_Products = Value
End If
End Property
In this small example, the argument is validated before it is assigned to the private variable.