Creating Custom Properties and Methods
Forms and reports are Class modules , which means they act as templates for objects you create instances of at runtime. Public procedures of a form and report become Custom properties and methods of the form object at runtime. Using VBA code, you can set the values of a form's Custom properties and execute its methods.
Creating Custom Properties
You can create Custom properties of a form or report in one of two ways:
- Create Public variables in the form or report.
- Create PropertyLet and PropertyGet routines.
Creating and Using a Public Variable as a Form Property
Figure 9.33. Creating a Public variable in the General Declarations section of a Class module.

Figure 9.34. Viewing the frmChangePublicProperty form.

![]() | To test the Custom property created in the preceding example, run the frmPublicProperties form, which is in the CHAP9EX.MDB database on your sample code CD-ROM. Click the Change Form Caption command button. Nothing happens, because the value of the Custom property hasn't been set. Open the frmChangePublicProperty form and click the Change Form Property command button. Return to frmPublicProperties and again click the Change Form Caption command button. The form's caption should now change. |
Form_frmPublicProperties.CustomCaption = _
"This is a Custom Caption"
Forms_frmPublicProperties.Visible = True
End Sub
This code modifies the value of the Public property by using the syntax Form_FormName.Property. If the form isn't loaded, this syntax loads the form but leaves it hidden. The next command sets the form's Visible property to True.
Creating and Using Custom Properties with PropertyLet and PropertyGet Routines
Figure 9.35. Starting a new procedure with the Add Procedure dialog box.

Figure 9.36. The PropertyGet and PropertyLet subroutines inserted in the module.

Notice that the Click event code for the cmdChangeCaption command button hasn't changed. The PropertyLet routine, which automatically executes whenever the value of the CustomCaption property is changed, takes the uppercase value of what it's being sent and places it in a Private variable called mstrCustomCaption. The PropertyGet routine takes the value of the Private variable and returns it to whoever asked for the value of the property. The following code is placed in the form called frmChangeWithLet:Private Sub cmdPublicFormProperty_Click()
Form_frmPropertyGetLet.CustomCaption = "This is a Custom Caption"
Forms!frmPropertyGetLet.Visible = True
End Sub
This routine tries to set the value of the Custom property called CustomCaption to the value "This is a Custom Caption". Because the property's value is being changed, the PropertyLet routine in frmPropertyGetLet is automatically executed. It looks like this:Public Property Let CustomCaption(ByVal CustomCaption As String)
mstrCustomCaption = UCase$(CustomCaption)
End Property
The PropertyLet routine receives the value "This is a Custom Caption" as a parameter. It uses the UCase function to manipulate the value it was passed and convert it to uppercase. It then places the manipulated value into a Private variable called mstrCustomCaption. The PropertyGet routine isn't executed until the user clicks the cmdChangeCaption button in the frmPropertyGetLet form. The Click event of cmdChangeCaption looks like this:Private Sub cmdChangeCaption_Click()
Me.Caption = CustomCaption
End Sub
Because this routine needs to retrieve the value of the Custom property CustomCaption, the PropertyGet routine automatically executes:Public Property Get CustomCaption() As String
CustomCaption = mstrCustomCaption
End Property
The PropertyGet routine takes the value of the Private variable, set by the PropertyLet routine, and returns it as the value of the property.You might wonder why this method is preferable to declaring a Public variable. Using the UCase function within PropertyLet should illustrate why. Whenever you expose a Public variable, you can't do much to validate or manipulate the value you receive. The PropertyLet routine gives you the opportunity to validate and manipulate the value to which the property is being set. By placing the manipulated value in a Private variable and then retrieving the Private variable's value when the property is returned, you gain full control over what happens internally to the property.Chapter 13, "Exploiting the Power of Class Modules."
Creating Custom Methods
Figure 9.37. Using the custom method ChangeCaption.

Figure 9.38. The Click event code behind the Execute Method button.

Figure 9.37 shows the Custom method ChangeCaption found in the frmMethods form. The method changes the form's caption. Figure 9.38 shows the Click event of cmdExecuteMethod found in the frmExecuteMethod form. It issues the ChangeCaption method of the frmMethods form, and then sets the form's Visible property to True.