USING PROPERTIES AND METHODS

The capability to program an object's properties and methods isn't new to Access. Access forms and controls have always had both. For example, the Visible property on a control causes a control to be either visible or invisible. The Requery method on a form restarts the underlying query.

The following sections discuss the mechanics for setting and retrieving a property's value, call ing methods, and manipulating objects.

Using Existing Properties

Access provides a large number of properties. Almost all actions in Access involve setting a property. However, the user interface hides the complexities surrounding properties. For example, the form consists of many properties. The form has a caption displayed on the title bar, it might have Minimize and Maximize buttons, it has a background color, and it has a certain size. These are all set through properties.

Access supplies property sheets that let you view and set values for each property for the form and controls. In the beginning of this chapter, when you created the Hello World form, you used the form's property sheets. To display a property sheet, follow these steps:

1.
Select the Forms tab from the database window.

2.
Select the Hello World form and click the Design button to open the form in design view.

3.
Right-click the black box in the upper-left corner. From the pop-up menu, select Properties if it isn't already selected. Another way is to choose Select Form from the Edit menu.

You should now see a form onscreen with the title Form and a number of tabs. The property sheet is divided into a number of categories. You can view properties by categories, or click the All tab to view all the properties grouped together.

The property sheet provides an easy way to set properties. To change a property, all you need to do is find it on the property sheet and enter the new value. The new value takes effect immediately.

Setting Property Values

Setting properties of an object from code is very simple. You set properties by using the following syntax:

								ObjectName.PropertyName = expression
							

The dot separates the object from the property being called on the object. Therefore, to set the caption property of the form from within the form, you write the following line of code:

								Me.Caption = "My Form"
							

The Me operator is used to reference the form when you're writing code within the form. A more detailed discussion of the Me operator is in Chapter 4.

Getting Property Values

You retrieve property values when you want to perform actions depending on the state of the object. For example, you can check the state of a check box and then run code based on whether the check box is now selected.

You get the value of properties by using the following syntax:

								VariableName = object_name.property_name
							

If you want to run code based on the state of the check box every time it's clicked, you can do it as follows:

Sub CheckBox1_Click()
     If CheckBox1.Value Then
          ' Run code when checkbox1 is true
     Else
          ' Run code when checkbox1 is false
     End If
End Sub

In this example, the value of the check box isn't stored in a variable. Instead, the returned value of the property is being used directly. If you're writing code in which the value of the property is being used multiple times, it's more efficient to store the value in a variable.

Using the Default Property

Many objects also provide a default property, which can be used to simplify your code. The default property is returned when you reference the object without specifying a property. For example, what would happen if you had a text box named Text1 inserted on a form and wrote the following line of code?

Me!Text1 = "Hello Out There"

Assigning a value to the control actually is setting the Text property. This line of code is the same as typing

Me!Text1.Value = "Hello Out There"

For the TextBox control, the Value property is the default property.

Note

When I referenced the control, I used an exclamation point, although it's not required. The exclamation point is called a bang. Bang notation should be used when referencing an object you named, such as a control's name, and the dot notation should be used when referencing something Access named—for instance, a property or method.


Every object has a different default property. For most controls, the default property relates to the value being displayed. On the form, the default property is actually another object, the Controls collection. (Collections are discussed in greater detail later in the section “Using Collections.”)

Tip

You can determine the default property for any object by looking for the little icon with the blue dot in the Object Browser.


Setting Multiple Properties with the With Statement

VBA has support for writing more readable code for performing multiple actions on a control. For example, if you want to set three properties of a text box named text1 on your form, you would need to write the following code:

text1.Backcolor = 0
text1.Width = 200
text1.Height = 400

Rather than write all this code, you can write a With..End With code block to accomplish the same thing. For example,

With text1
     .Backcolor = 0
     .Width = 200
     .Height = 400
End With

This purely cosmetic feature doesn't change how your code is executed. The advantage to using the code block is that it becomes more obvious that you're modifying many properties of the same object.

Using Existing Methods

Objects are also composed of any number of methods. A method, as mentioned earlier, is similar to a function or subroutine. As with properties, methods have also existed for some time on forms and controls. Again, as with properties, VBA now gives you the capability of defining your own additional public methods.

Referencing methods is similar to referencing properties. The primary difference is that a method is either a subroutine or function and therefore can't be assigned a value. Calling methods follows this syntax:

							ObjectName.MethodName
						

For example, to call Requery on a form, type the following:

Me.Requery

Some methods might take parameters or supply return values. If you're calling a method that doesn't have a return value or you don't care about the return value, separate the arguments with a comma. For example, calling the GotoPage method on a form requires that you specify which page you want to move to; in addition, you can optionally specify a right and down off set. Calling this method on the form is as follows:

Me.GotoPage 2,100,100

If the method returns a value that you want to use, you must enclose the arguments in parentheses. This works similarly to calling a function.

Specifying Named Parameters

All methods provided in Access allow referencing of an argument by its name rather than its position. Methods that support named arguments can be identified in the Object Browser.

Normally, when you call a method, you pass the arguments in the order specified by the method. Now, you can provide the parameters in any order by providing the name of the argument. The syntax for using named parameters is as follows:

							ObjectName.MethodName Parameter1:=expression, _
							Parameter2:=expression,...Parametern:=expression
						

Tip

You can used named parameters and find their syntax with user-defined routines as well. You can use the Auto Statement Builder to take full advantage of this. For more information on the Auto Statement Builder, see Appendix A.


For example, the GotoPage method takes three parameters. Two of the parameters, offset right and down, are optional. By using named parameters, you can now omit the right parameter and still specify a down parameter. To call the GotoPage method to move to page 2, down 100 twips, you would write the following method call:

Me.GotoPage PageNumber := 2, Down:=100

You can used named parameters instead of leaving blank commas. Suppose that you have five parameters, of which you want to use only the first and last. By using named parameters, you use only the two instead of all five. Named parameters also help make your code self-documenting because it's a lot easier to remember what the parameters do when you can see their names right there.

Note

Access exposes all screen measurements through the programming language in twips. There are 1,440 twip units in an inch. A twip is a special unit of measurement that's screen-resolution independent. Therefore, regardless of your screen's resolution, elements are displayed correctly.


Assigning Objects to Variables

Assigning an object to a variable uses a slightly different convention than assigning a value to a variable. When using objects, you assign objects to a variable by using the Set statement:

Set variable = ObjectReference

To understand the difference between object assignment and variable copying, think back to the ByVal and ByRef discussion earlier in the section “Passing Arguments ByRef or ByVal.” Normally, you're copying variables. Consider the following code fragment:

Dim intX as integer
Dim intY as integer
intX = 10
intY = intX
intX = 20
MsgBox intY

The value of intY is 10 when it's displayed in the message box. When the line intY = intX is executed, the current value of intX is copied into intY. Any subsequent changes to intX aren't reflected in intY.

However, when the Set statement is used, a reference pointing to the object is made, not a copy. Consider the following code fragment:

Dim txtName as TextBox
Me!UserName = "Bill"
Set txtName = Me!UserName
Me!UserName = "Joe"
MsgBox txtName

The value txtName that's displayed in the message box is Joe. Furthermore, any changes made to the txtName variable are immediately reflected in the UserName text box. Assigning an object to a variable is useful when you want to perform a number of actions on a single object.

In summary, you always use the Set statement for object variables and never for data variables.

Using Generic Object Types

Access and VBA also supplies generic object types. You declare variables of this type when you're unsure of the object type you'll be assigning. The following table shows the generic object types supported by VBA.

Type Description
Object Supplied by VBA, this is a generic object. Any type of object can be assigned to this type.
Control Supplied by Access, this is a variable that can take any type of control.

The following example uses the generic control object type to align all the controls to the specified position. This example uses a special syntax for looping over all the controls. (This syntax is discussed in greater detail later in the section “Using Collections.”)

Sub AlignLeft(intLeft as Integer)
     Dim ctlCurrent as Control
     For Each ctlCurrent in Me
          ctlCurrent.Left = intLeft
     Next
End Sub

You must declare the variable as the type Control because the form might consist of a number of different control types.

Note

Using the generic objects such as Control can be convenient, but a performance hit is involved. Always use specific types where possible. If you know a control will be a text box, declare the variable as TextBox.


Identifying the Object Type

When using a generic object type, you often want to know what type of object is actually being referenced. There are three ways to determine an object's type. The first and second work for all objects; the third can be used only with control objects.

To identify an object's type, you can use the TypeOf operator. This operator works only within an If..Then statement, as follows:

If TypeOf Object is ClassName Then
     ...
End If

Note

You can't use the TypeOf operator in a Select..Case statement. You must use a nested If..Then statement if you want to check whether the variable contains one of a number of different objects.

There's a more convenient way to check the control type being referenced. Every control has a property, ControlType, that returns an integer identifying the control. This property can be used in a Select..Case statement. Identifying control types is very important because you might want to perform an action that only certain control types support.


The second way is using the TypeName (variable) function. When used in a statement, you will pass the variable to be tested; then a string representing the variable's type will be returned.

Lastly, in the following example, the code iterates over all the controls on a form and appends a colon (:) to all labels.

Sub FixLabels()
     Dim ctlActive as Control
     For Each ctlActive in Me
          If ctlActive.ControlType = acLabel Then
               ctlActive.Caption = ctlActive & ":"
          End If
     Next
End Sub

Access has defined constants that represent each built-in control. To find the list of constants, press F2 in the VBE and look up acControlType in the Classes list. You then see the constants in the Members list.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.79.60