Previous Page

Working with Properties and Methods

To describe an object's characteristics, you use its properties. You can set properties to change their values, or read properties to get information about the object. To control how an object behaves, you use its methods. An object's methods determine what operations you can perform on that object.

Because a collection is also an object, each collection in Microsoft Access has its own properties and methods. You can set or read the properties of a collection, or apply its methods, in the same manner that you would for any object.

See Also   For information on the properties and methods an object supports, search the Help index for the name of the object. You can also search the Help index for the name of a property or method.

Setting and Reading Properties

Visual Basic provides a standard syntax for setting and reading properties in code. When you set a property, you give it a new value. You can use the following syntax to set a property for any type of object:

object.property = setting

The following line of code sets the Caption property of the Employees form:

Forms!Employees.Caption = "Employees Form"

When you read the value of a property, you determine its current value. In order to read the property, you can assign its value to a variable or to another property, or you can display it in the Debug window, in a dialog box, or in a control on a form or report. The following example assigns the value of the Caption property to a variable and then displays the value of that property in a dialog box.

Dim strCaption As String
strCaption = Forms!Employees.Caption
MsgBox strCaption

Properties That Return Objects

Sometimes you may want your code to refer to whatever object happens to be in a particular state at the time a procedure is running, rather than to a specific object. Writing code in this way can make your application more flexible. For instance, you may want to change the caption of the active form, without knowing the form's name. Or you may want to hide the control that has just lost the focus.

Rather than determining an object's characteristics, some properties of an object represent another object that is related in some way. These properties return an object reference that you can work with directly or assign to an object variable, just as you would any object reference. The following table lists several properties that return objects.

Property

Applies to

Returns a reference to

ActiveControl

Screen, Form, or Report object

The Control object that has the focus.

ActiveForm

Screen object

The Form object that has the focus or that contains the control with the focus.

ActiveReport

Screen object

The Report object that has the focus or that contains the control with the focus.

Application

Numerous objects

The Microsoft Access Application object.

DBEngine

Application object

The DBEngine object.

Form

Subform Control object

The Form object associated with the subform control.

Me

Form or Report object

The Form or Report object in which code is currently running.

Module

Form or Report object

The Module object associated with a Form or Report object.

Parent

Numerous objects

The object or collection that contains an object.

PreviousControl

Screen object

The Control object that had the focus immediately before the currently active control.

RecordsetClone

Form object

A clone of the form's underlying recordset.

Report

Subreport Control object

The Report object associated with the subreport control.

Section

Form, Report, or Control object

A section on a form or report.

See Also   For information on properties that return objects, search the Help index for the specific property name.

The Section Property

The Section property returns a reference to a section of a form or report. For example, you can use the Section property to return a reference to the detail section of a form. Once you've returned a reference to a section, you can set the section's properties. The following example uses the Section property to set a property of the detail section on an Employees form.

Forms!Employees.Section(acDetail).Visible = False

See Also   For more information on setting properties of sections, search the Help index for "Section property."

The Me Property

The Me property returns an object reference to the Form or Report object in which the code is currently running. You can use the Me property to refer to a Form or Report object from within that object, without needing to know the name of the form or report. You can also use it to pass a Form or Report object to a procedure that takes an argument of type Form or Report.

For example, the following code uses the Me property to return a reference to the Employees form, the form in which the code is running. It then passes this reference to the ChangeDetailColor procedure which it calls when the form's Current event occurs. It also uses the Me property to return references to the Employees form in order to set a property and to return the values of the FirstName and LastName controls on the form. Note that the . (dot) operator is used to set the property, and the ! operator is used to refer to the controls on the form.

' Place this procedure in a standard module.
Sub ChangeDetailColor(frm As Form)
   frm.Section(acDetail).BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)
End Sub
' Place this procedure in the form module associated with the Employees form.
Private Sub Form_Current()
   ChangeDetailColor Me
   Me.Caption = Me!FirstName.Value & " " & Me!LastName.Value
End Sub

In most cases, the form or report represented by the Me property is the same form or report represented by the ActiveForm or ActiveReport property of the Screen object. However, the ActiveForm and ActiveReport properties represent the active form or report, whereas the Me property represents the form or report in which the code is running. For example, a Timer event can occur on a form called Customers, even if the Customers form isn't active. In a Timer event procedure for the Customers form, Screen.ActiveForm represents the active form, whatever it is, and Me always represents the Customers form.

Using Methods

Methods are built-in operations that you can perform on an object. There are two kinds of methods: those that return a value or an object, as a function does, and those that perform a specific operation, as a statement does. To apply a method to an object, you use the following syntax:

object.method [[(] arg1, arg2...[)]]

Many methods take one or more arguments. An argument provides the method with additional information for its operation. If the method returns a value or an object, you must enclose its argument list in parentheses; otherwise you should omit the parentheses.

The following example shows the syntax for several different methods. The OpenRecordset method creates a new Recordset object and returns a reference to the new object. You can assign this object reference to an object variable by using the Set statement. Because the OpenRecordset method returns a value, you must enclose its arguments in parentheses. The FindFirst method, on the other hand, doesn't return a value. It simply sets the current record pointer to the first record that matches the criteria given in the FindFirst argument. Since this method doesn't return a value, you don't need to enclose its arguments in parentheses. The same is true for the Print method of the Debug object. Finally, the Close method of the Recordset object doesn't take any arguments.

Sub FindEmployee()
   Dim dbs As Database, rst As Recordset
   
   Set dbs = CurrentDb
   ' Requires parentheses.
   Set rst = dbs.OpenRecordset("Employees", dbOpenDynaset)
   rst.FindFirst "[HireDate] >= #1-1-93#"      ' No parentheses needed.
   Debug.Print rst!LastName                    ' No parentheses needed.
   rst.Close                                   ' Doesn't take arguments.
   Set dbs = Nothing
End Sub

Performing Multiple Actions on an Object

You'll often need to perform several different actions on the same object. For example, you may need to set several properties for the same object within a single procedure. Instead of using many separate statements to do this, you can use the With...End With statement. The following example uses the With...End With statement to set several properties for a command button named HelpButton in a form's Load event.

Private Sub Form_Load()
   With Me!HelpButton
      .Caption = "Help"
      .Visible = True
      .Top = 200
      .Left = 5000
      .Enabled = True
   End With
End Sub

© 1996 Microsoft Corporation. All rights reserved.

Next Page


Casa de Bender