Declares the name, arguments, and code that form the body of a Property procedure, which gets the value of a property.
[Public | Private][Static] Property Get name [(arglist)][As type]
[statements]
[name = expression]
[Exit Property]
[statements]
[name = expression]
End Property
The Property Get statement syntax has these parts:
Part |
Description |
Public |
Indicates that the Property Get procedure is accessible to all other procedures in all modules. If used in a private module (one that contains an Option Private statement) the procedure is not available outside the project. |
Part |
Description |
Private |
Indicates that the Property Get procedure is accessible only to other procedures in the module where it is declared. |
Static |
Indicates that the Property Get procedure’s local variables are preserved between calls. The Static attribute doesn’t affect variables that are declared outside the Property Get procedure, even if they are used in the procedure. |
name |
Name of the Property Get procedure; follows standard variable naming conventions, except that the name can be the same as a Property Let or Property Set procedure in the same module. |
arglist |
List of variables representing arguments that are passed to the Property Get procedure when it is called. Multiple arguments are separated by commas. The name and data type of each argument in a Property Get procedure must be the same as the corresponding arguments in a Property Let procedure (if one exists). |
type |
Data type of the value returned by the Property Get procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (except fixed length), Object, Variant, or user-defined type. Arrays of any type can’t be returned, but a Variant containing an array can. |
The return type of a Property Get procedure must be the same data type as the last (or sometimes the only) argument in a corresponding Property Let procedure (if one exists) that defines the value assigned to the property on the right-hand side of an expression. | |
statements |
Any group of statements to be executed within the body of the Property Get procedure. |
expression |
Value of the property returned by the procedure defined by the Property Get statement. |
The arglist argument has the following syntax and parts:
[ByVal | ByRef] varname[( )][As type]
Part |
Description |
ByVal |
Indicates that the argument is passed by value. |
ByRef |
Indicates that the argument is passed by reference. |
varname |
Name of the variable representing the argument; follows standard variable naming conventions. |
type |
Data type of the argument passed to the Property Get procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (variable length only), Object, Variant, a user-defined type, or an object type. |
If not explicitly specified using either Public or Private, Property procedures are public by default. If Static is not used, the value of local variables is not preserved between calls.
All executable code must be in procedures. You can’t define a Property Get procedure inside another Property, Sub, or Function procedure.
The Exit Property keywords cause an immediate exit from a Property Get procedure. Program execution continues with the statement following the statement that called the Property Get procedure. Any number of Exit Property statements can appear anywhere in a Property Get procedure.
Like a Sub and Property Let procedure, a Property Get procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub or Property Let procedure, you can use a Property Get procedure on the right-hand side of an expression in the same way you use a Function or a property name when you want to return the value of a property.
Function Statement, Property Let Statement, Property Set Statement, Sub Statement.
You can use the Property Get statement to define a property procedure that gets the value of a property for a user-defined object. You will probably want to use the Property Let and Property Get statements together to create a property for an object. For example, you could define a Property Let procedure in a form module to set a new property for an object, and a Property Get procedure to return the value of that setting.
Usually you will define property procedures within a form or report module. Property procedures in a form or report module are public by default, and will be available to procedures in other modules in the current database. However, they will not be available to other databases.
You can use the property procedures to create new properties on a user-defined object. You can also use them to combine a set of properties for a custom form or report object. For example, you could create a custom property called CustomFormType. Using the Property Get statement, you could construct the CustomFormType property procedures so that one setting would display a white form with scroll bars, navigation buttons, and caption “Type1”, while another setting would display a blue form with neither scroll bars nor navigation buttons, and caption “Type2”. Then you could set the CustomFormType property for new form objects that you create for this form. Using the Property Get statement, you could create a property procedure to return the value of this property once it has been set.
This example uses the Property Get Statement to define a property procedure that gets the value of a property. The property identifies, as a string, the current color of a pen in a drawing package.
Dim CurrentColor As IntegerBLACK = 0, RED = 1, GREEN = 2, BLUE = 3 ' Returns the current color of the pen as a string.Get PenColor() As String Select Case CurrentColor Case RED PenColor = "Red" Case GREEN PenColor = "Green" Case BLUE PenColor = "Blue" End SelectProperty ' The following code gets the color of the pen ' calling the Property Get procedure.= PenColor()
The following example shows how to use the Property Let and Property Get statements to create a custom property, CustomFormType, for a user-defined object. By setting the CustomFormType property, you apply a set of predefined characteristics to a user-defined form object. You can also read the property to determine its current setting.
To test this example, create a new form and save it as CustomFormTemplate. In the Declarations section of the form’s module, enter the following code.
Public intCurrent As IntegerconType1 = 1conType2 = 2
Enter the following procedures in the form module. The Property Let procedure defines the characteristics that will be applied to the form when the CustomFormType property is set. The Property Get procedure returns the current setting of the CustomFormType property.
Static Property Let CustomFormType(intCustomType As Integer) Select Case intCustomType Case conType1 With Me .Section(0).BackColor = RGB(255, 255, 255) .ScrollBars = 3 .NavigationButtons = True .Caption = "Type1" intCurrent = conType1 End With Case conType2 With Me .Section(0).BackColor = RGB(127, 127, 127) .ScrollBars = 0 .NavigationButtons = False .Caption = "Type2" intCurrent = conType2 End With Case Else MsgBox "Error!" End SelectProperty Property Get CustomFormType() As Integer Select Case intCurrent Case conType1 CustomFormType = conType1 Case conType2 CustomFormType = conType2 Case Else MsgBox "Error!" End SelectProperty ' Define custom method to display form.Show() Me.Visible = TrueSub
Create a new instance of the CustomFormTemplate object in a standard module. Now you can set and read the value of the CustomFormType property just as you would any built-in property.
Sub SetFormProperty() Dim frmObject as New Form_CustomFormTemplate ' Call Show method to display form. frmObject.Show ' Set value of property. frmObject.CustomFormType = 1 ' Return value of property. Debug.Print frmObject.CustomFormTypeSub