PROGRAMMING WITH OBJECTS

Earlier, the section “Defining the Life and Location of Variables” introduced you to the concept of variable and procedure scoping by using the Public and Private keywords. The following sections discuss how you also can create and reference your own custom properties and methods on an object. Custom properties are essentially variables for which you can write custom routines when they're assigned or retrieved. For example, the Visible property on the form functions like a variable, but when you assign a value to it, it either hides or shows the object. With VBA, you can now create your own variables that provide special behaviors.

Note

Method is essentially another term for a subroutine or function. Methods act on the object they're contained in. For example, the Repaint method on the form tells the form to repaint itself.


The Public Keyword

Variables and procedures can be made public with the Public keyword. Being public means that the variable is available anywhere within your application. If the variable or procedure is in a standard module, you don't have to qualify the reference. If it's behind a form or report, or part of another object, you first need to reference the object. If you expose a public variable intTest on a standard module, for example, you can reference it as follows:

Debug.Print intTest

However, if the same variable is exposed on a form named Form1, you must first reference the form, as follows:

Debug.Print Form1.intTest

Public variables may be declared only in the general declaration section of a code module. Variables declared within a procedure are available only to that procedure. Because variables and procedures are public in standard modules by default, the Public keyword is optional.

The Private Keyword

By default, all variables and procedures are public except for event handlers, which are located in a form's or report's class module. Private variables and procedures are available only within the procedure.

Note

Unlike the Public keyword, Private isn't an outright replacement for an existing keyword from Access 2. For module-wide variables (those in the declaration section), you can now use Private instead of Dim. However, variables still must be declared by using Dim when they're contained within subroutines.


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

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