MS-Access / Getting Started

Property Set

Use a Property Set procedure to define code that executes when the calling code attempts to assign an object to an object property of the object defined by your class module. You cannot define both a Property Let and a Property Set procedure for the same property.

Syntax

[Public | Private | Friend] [Static] Property Set propertyname
  ([<arguments>,] object [As objecttype])
  [ <property statements> ]
  [Exit Property]
  [ <property statements> ]
End Property

where <arguments> is

{[Optional][ByVal | ByRef][ParamArray] argumentname[()] [As datatype][ = default]},...

Notes

Use the Public keyword to make this property available to all other procedures in all modules. Use the Private keyword to make this property available only to other procedures in the same module. When you declare a property as private in a class module, you cannot reference the property from another module. Use the Friend keyword to declare a property that is public to all other code in your application but is not visible to outside code that activates your project via automation.

Include the Static keyword to preserve the value of all variables declared within the property procedure, whether explicitly or implicitly, so long as the module containing the procedure is open. This is the same as using the Static statement (discussed earlier in this tutorial) to explicitly declare all variables created in this property procedure.

You should declare the data type of all arguments in the property procedure's parameter list. Valid datatype entries are Byte, Boolean, Integer, Long, Currency, Single, Double, Date, String (for variable-length strings), String * length (for fixed-length strings), Object, Variant, or one of the object types described earlier in this tutorial. Note that the names of the variables passed by the calling procedure can be different from the names of the variables as known by this procedure. Also, the names and data types of the arguments must exactly match the arguments declared for the companion Property Get procedure. If you use the ByVal keyword to declare an argument, Visual Basic passes a copy of the argument to your property procedure. Any change you make to a ByVal argument does not change the original variable in the calling procedure. If you use the ByRef keyword, Visual Basic passes the actual memory address of the variable, allowing the procedure to change the variable's value in the calling procedure. (If the argument passed by the calling procedure is an expression, Visual Basic treats it as if you had declared it by using ByVal.) Visual Basic always passes arrays by reference (using ByRef).

Use the Optional keyword to declare an argument that isn't required. All optional arguments must be the Variant data type. If you declare an optional argument, all arguments that follow in the argument list must also be declared as optional. You can specify a default value only for optional parameters. Use the IsMissing built-in function to test for the absence of optional parameters. You can also use the ParamArray argument to declare an array of optional elements of the Variant data type. When you attempt to assign a value to this property in an object set to the class, you can then pass it an arbitrary number of arguments. The ParamArray argument must be the last argument in the argument list.

You must always declare at least one parameter, object, to be the variable that contains the object that the calling code wants to assign to your property. This is the object reference that appears on the right side of the assignment statement executed in the calling code. If you include an objecttype entry, it must match the object type declared by the companion Property Get procedure. Also, when you declare an object type, the caller receives a data type mismatch error if the assignment statement attempts to pass an incorrect object type. You can evaluate the properties of this object, set its properties, execute its methods, and save the object pointer in another variable that your Property Get procedure can later return.

Use the Exit Property statement anywhere in your property procedure to clear any error conditions and exit your procedure normally, returning to the calling procedure. If Visual Basic runs your code until it encounters the End Property statement, control is passed to the calling procedure, but errors are not cleared. If this procedure causes an error and terminates with the End Property statement, Visual Basic passes the error to the calling procedure.

Examples

To declare a ControlToUpdate property, accept a value from a caller, and save the value in an object variable defined in the Declarations section of your class module, enter the following:

Option Explicit
Dim ctlToUpdate As Control
Property Set ControlToUpdate(ctl As Control)
  ' Verify we have the right type of control
  Select Case ctl.ControlType
    ' Text box, combo box, and list box are OK
    Case acTextBox, acListBox, acComboBox
      ' Save the control object
       Set ctlToUpdate = ctl
    Case Else
      Err.Raise 3999
  End Select
End Property

Here is an example of how to establish a new instance of an object defined by a class module and then set its FileName property:

Public Property Set ctlToUpdate(Optional intD As Integer = 0, ctl As control)
' This procedure is an example of Property Set
' GetDateOCX opens this form by creating a new instance of the class
' and then sets the required properties via a SET statement.
    ' First, validate the kind of control passed
    Select Case ctl.ControlType
	' Text box, combo box, and list box are OK
	Case acTextBox, acListBox, acComboBox
	Case Else
	  MsgBox "Invalid control passed to the Calendar."
	  DoCmd.Close acForm, Me.Name
    End Select
    ' Save the pointer to the control to update
    Set ctlThisControl = ctl
    ' Save the date only value
    intDateOnly = intD
    ' If "date only"
    If (intDateOnly = -1) Then
	' Resize my window
	DoCmd.MoveSize , , , 3935
	' Hide some stuff just to be sure
	Me.txtHour.Visible = False
	Me.txtMinute.Visible = False
	Me.lblColon.Visible = False
	Me.lblTimeInstruct.Visible = False
	Me.SetFocus
    End If
    ' Set the flag to indicate we got the pointer
    intSet = True
    ' Save the "current" value of the control
    varDate = ctlThisControl.Value
    ' Make sure we got a valid date value
    If Not IsDate(varDate) Then
	' If not, set the default to today
	varDate = Now
	Me.Calendar1.Value = Date
	Me.txtHour = Format(Hour(varDate), "00")
	Me.txtMinute = Format(Minute(varDate), "00")
    Else
	' Otherwise, set the date/time to the one in the control
	' Make sure we have a Date data type, not just text
	varDate = CDate(varDate)
	Me.Calendar1.Value = varDate
	Me.txtHour = Format(Hour(varDate), "00")
	Me.txtMinute = Format(Minute(varDate), "00")
    End If
End Property
[Previous] [Contents] [Next]