Introducing Objects

 < Day Day Up > 

You've seen quite a few variables so far in this book. Recall that a variable is a named part of VBA code that holds a single value (for example, a number or a string of characters). Or in the case of an array variable, the variable can store multiple related values. But so far you've been exposed to only one type of variable, which you might call a simple variable (sometimes you'll see the technical term scalar variable used for this type). There's actually a second type called object variables, or just objects. An object variable is a variable that also includes behavior. This concept is often tough for beginners to grasp, so this section starts by exploring an analogy that will help you understand how objects work.

Digressing into the Real World

To understand objects, you're going to look at a real-world object. Specifically, this section discusses the sort of toy radio-controlled car that has been popular with kids (and software developers!) for many years. Here are three key facts about a typical such car:

  • The plastic cars are mass-produced from a single mold. One mold can make many cars.

  • Any given car is described by a set of adjectives such as "blue." Every car can be described by characteristics like its color, but different cars can have different colors.

  • When you manipulate the control box, the car does things such as go forward, turn, or go backward. You don't have to know anything about how the car does these things; you just send it the appropriate commands.

All that probably seems perfectly obvious, which was the point. If you keep these little cars in mind when learning about objects, it will help you sort out the key concepts.

An Object Example from Access

There are objects in VBA and in the applications that it manipulates, just like there are objects in the real world. For example, each Access form is actually an object. Here's how the analogy plays out:

  • Form objects are mass-produced from a single template called a class. Making an object from a class is called instantiating the class (an object is an instance of a class). One class can instantiate many objects.

  • Any given form is described by a set of properties such as a caption, for example, "Switchboard." Every form can be described by properties like its caption, but different forms can have different captions.

  • The form can do things like open or close. You don't have to know anything about how the form does these things; you just invoke the appropriate methods.

Of course, analogies break down after a while, and there are some aspects of objects (such as events, which you learn about later in this chapter) that don't fit neatly into this particular analogy. But on the whole, if you start to feel confused about VBA objects, thinking about real objects might help.

Creating Objects in Code

Although you can create some objects (such as Access forms) through a user interface, most VBA objects are completely abstract and can only be created in code. Before you can do anything else with objects, you need to know how to create them. Here's an example of VBA code that creates a new object, in this case an instance of the Projects form in the sample database:


 Sub CreateObject1()   ' Create and display a form object   Dim frm As Form_Projects   Set frm = New Form_Projects   frm.Visible = True   MsgBox "Click OK to continue" End Sub 

If you run this code, you'll discover that it creates an instance of the Projects form and displays it, along with a message box, as shown in Figure 8.1. When you click OK, both the message box and the form vanish.

Figure 8.1. Form object created with VBA code.


The code starts by declaring the object variable named frm. Rather than being a simple variable such as a string or an integer, this variable is declared to be of the Form_Projects type. For each form in your Access application, Access creates a matching class named Form_formname.

Remember, though, that the class is not itself an object. To actually use the object, you need to instantiate the class. That's what the second line of code (the Set statement) does. This line tells VBA to create a new instance of the Form_Projects class, and to use the frm variable to refer to that object in the future.

By default, new Access forms are hidden when they are created. The next line of code sets the Visible property of the form to True, so that it will be visible onscreen.

Next comes the MsgBox statement to generate the message box. Remember, when you create a message box, your code pauses until the user responds to the message.

After you click OK, you'll see the form vanish. That's because VBA destroys all the variables declared in a procedure at the end of the procedure. When the frm variable is destroyed in code, the corresponding form ceases to exist onscreen.

It is possible to declare variables slightly differently so that they are available outside of a single procedure. For details, see "Understanding Scope and Lifetime," p.131.

There's a second way to create an object that takes slightly less code:


 Sub CreateObject2()   ' Create and display a form object   Dim frm As New Form_Projects   frm.Visible = True   MsgBox "Click OK to continue" End Sub 

The line of code


 Dim variablename As New classname 

both declares and instantiates the variable in a single line.


When you declare and instantiate an object in a single statement, the object isn't actually created until the first time that you use the object variable in code. If you want to make it easy to tell when the object is created, use the two-statement format.

     < Day Day Up > 

    Automating Microsoft Access with VBA
    Automating Microsoft Access with VBA
    ISBN: 0789732440
    EAN: 2147483647
    Year: 2003
    Pages: 186 © 2008-2017.
    If you may any questions please contact us: