Collections, Objects, Properties, Methods, and Events

Access 2000 supports VBA, which facilitates object-oriented development. The following sections will introduce object-oriented development in the context of VBA and Access 2000. The information is targeted at power users migrating to programmatic application development and at mid-level developers who want to review object-oriented programming with VBA.

Collections and Objects

Access 2000 is an object-oriented development environment. Its Database window facilitates user access to tables, queries, forms, reports, modules, and macros. VBA makes these available along with a broad array of programmatic constructs, such as recordsets and TableDef objects. To get the most out of VBA in Access, you have to understand objects and a number of related concepts.

An object is a thing. Things can be as diverse as cars, phones, and videos. All objects have properties. Cars, for example, are defined by color, door, and engine properties, among others. Properties can define instances of generic objects. This object-oriented behavior allows you to specify unique instances of objects based on their properties. For example, a red car and a black car define two unique instances of the car object.

Object properties vary according to the object class to which they refer. A car has a different set of properties than a phone. Both have a color property, but a phone can also have a speaker property. Cars, on the other hand, have engines of different sizes. Some objects are containers for other ones. Contained objects can also have properties. Engines come in various sizes and configurations, for example, while speakers come with volume controls and fidelity ratings. Properties can also define unique instances of contained object classes. A speakerphone has a different set of properties than a standard phone.

In addition to properties, objects also have methods. An object's methods are the behaviors that it can perform. A phone makes connections. A car moves. Many objects have multiple methods. Phones let you make local calls and long-distance calls, for example.

Access developers do not manipulate physical objects. We manipulate programming constructs such as forms, tables, and queries that can represent objects and their behaviors. The Access 2000 Database window shows some of its database object classes on its Microsoft Outlook-style toolbar. (See Figure 11.) Clicking the Forms button on the toolbar opens a view of form objects and displays two options for creating new forms. Form objects can contain other objects called controls. Contained objects define an object just as controls on a form define its look and behavior.

click to view at full size.

Figure 1-1. The Database window with a selection of form objects and two options for creating new forms.

Figure 1-1 shows several form objects. These objects comprise a collection. Access applications typically have collections of forms, tables, queries, and other objects. The Database window automatically sorts selected objects into classes. Clicking the toolbar on the left reveals the objects in each collection.

Collections are like objects. All Access collections have a Count property, which specifies the number of instances in the collection. Collections can also have an Item property. You can use the read-only Item property to return an individual form from the AllForms collection. Since a collection's members are individual objects, they do not have a Count property. The objects of a collection serve different purposes. A convertible car can serve different purposes than a family sedan, but they can both belong to the car collection in a household.

Properties and Methods

Properties and methods characterize the appearance and behavior of objects. The syntax for referencing these is object.property or object.method. The term object can refer to either an individual object or a collection of objects. For example, txtInput1.BackColor specifies the background color property of a text box on a form, and AllForms.Item(0) refers to the first form in the forms collection. If this form has the name frmSample1, you can refer to the form as AllForms.Item("frmSample1").

You can see the properties of a database object by selecting it in Design view and clicking the Properties button on the toolbar. Figure 1-2 shows a form in Design view along with the form's property sheet. The property sheet shows a custom entry, My Default Caption, in the Caption property box. The Close Button property is selected. You can click the Close Button box and select No. The new Close Button property grays out the Close button when a form appears in Form view. Notice that the property sheet has multiple pages. Figure 1-2 shows the Format tab selected. These pages organize the properties into groups for fast retrieval.

click to view at full size.

Figure 1-2. A simple form in Design view with its property sheet.

NOTE
In Access 2000, the property sheet is available in both Design view and Form view instead of only in Design view. This means that you can quickly modify and refine a form's appearance as you work with its design in Form view.

The DoCmd object is a rich source of methods for all levels of Access developers, but beginners find it especially helpful for getting started with methods. The DoCmd object has many methods, including Close, OpenForm, GoToControl, FindRecord, and RunCommand. Many DoCmd methods require arguments that specify how the method performs. Other methods have required and optional arguments. If you do not specify values for an optional argument, the method uses the default settings. RunCommand is particularly attractive to power users who are upgrading to programmer status; you can use it to execute the commands that are available on the Access menus and toolbars.

You can close a form in Access using the Close method of the DoCmd object. This method has two required arguments and one optional one. The first required argument specifies the type of object to close. When you close a form, use acForm. (acForm is a built-in Access constant whose value tells the Close method that you want to close a form. See the section titled "The Object Browser" for more information about built-in Access constants.) The second argument is the form's name. This entry appears in the Name property of the form's property sheet. Enclose the name in quotes. The optional argument tells Access whether to save any changes to the form. The default setting is to prompt for whether to save. Use either acSaveYes or acSaveNo to close the form with or without saving any changes. You can invoke the Close method for a form using the following syntax:

 DoCmd.Close acForm, "formname", acSaveNo 

Many DoCmd methods apply directly to individual objects. For example, the GoToControl method assigns the focus to a specific control on a form. You can achieve the same result using the SetFocus method, which selects a control. Either method is convenient when your application needs to move the focus for entering new information or correcting faulty information.

Events

Events are very important in VBA programming. You can use events to make applications dynamic and interactive. Objects and collections have events, which serve as launching points for a developer's custom code. When you work with forms, you can use events for such tasks as data validation, enabling or disabling controls, changing the control that has the focus, opening a form, and closing a form.

You must understand when each event fires as well as the order in which the events fire. When a form opens, it triggers a sequence of events: Open, Load, Resize, and Current. The Open event occurs when a form starts to open but before it displays any records. The Load event occurs after the Open event. When the Load event fires, a form shows its records. Any code that causes a form to change its size or location by means of the MoveSize, Minimize, Maximize, or Restore DoCmd methods fires the Resize event. The Current event is the last one that normally occurs as a form opens. It marks the moment at which a particular record is current or available. It also fires when a user navigates to a new record or requeries or refreshes a form.

You access the events for forms and their controls by selecting the form or control in Design view and then clicking the Event tab of the property sheet. Clicking the Build button next to an event opens a dialog box that you can use to open the code module behind a form. Choosing Code Builder opens an event procedure in VBE. The event procedure is named Objectname_Eventname, where Objectname is the name of the object and Eventname is the name of the event. If you select a form and click the Build button for the Close event, for example, your event procedure will have the name Form_Close. If you create an event procedure for the On Click event of a label named lblTitle, VBA automatically names it lblTitle_Click.

The following are three event procedures for the form shown in Figure 1-2: Form_Open, Form_Load, and lblTtitle_Click. When you first open the form in Form view, a message box opens that reads, "The form opened." When you click OK, you see a second message that says, "The form loaded." After the form completes its loading cycle, clicking the label opens a third message box that says, "Hello from the label."

 Private Sub Form_Open(Cancel As Integer)     MsgBox "The form opened.", vbInformation,          "Programming Microsoft Access 2000" End Sub Private Sub Form_Load() 'This is s simple statement.     MsgBox "The form loaded.", vbInformation, _         "Programming Microsoft Access 2000" 'This sets a property.     Me.Caption = "New Caption" 'Here are two methods for giving a control focus. '    DoCmd.GoToControl "txtMyTextBox"     Me.txtMyTextBox.SetFocus 'Now that the method worked, VBA sets a property.     Me.txtMyTextBox.Text = "Hi, there!" End Sub Private Sub lblTitle_Click()     MsgBox "Hello from the label.", vbInformation, _         "Programming Microsoft Access 2000" End Sub 

The event procedures cause the message boxes to appear. Clicking the label invokes the lblTitle_Click event procedure. This procedure has a single statement that presents a message box. (The underscore at the end of procedure's first line is a continuation character.) The Form_Open event procedure also has a single statement. The Form_Load event procedure has several statements besides the one for its message box. This event procedure dynamically sets the caption for the form, which is particularly helpful when you have a form that has two or more roles in an application. It also sets the focus to the text box named txtMyTextBox and then assigns "Hi, there!" to the control's Text property. This event procedure demonstrates two different techniques for setting the focus. One relies on the SetFocus method, and the other uses the GoToControl method for the same purpose. The apostrophes at the beginning of some lines of code mark them as comments. Notice that one of the two techniques is a comment line.



Programming Microsoft Access 2000
Programming Microsoft Access 2000 (Microsoft Programming Series)
ISBN: 0735605009
EAN: 2147483647
Year: 1998
Pages: 97
Authors: Rick Dobson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net