Section 16.3. Understanding Objects

16.3. Understanding Objects

You can actually do a whole lot more with the controls on your form. Rather than just changing their content, you can also change their color , font, position, visibility, and many other details. The secret to unlocking the magic is to realize that all controls are programming objects .

In the programming world, an object's nothing more than a convenient way to group together some related features. The Description field isn't just a single value, it's an entire text box object , and that means it has all sorts of built-in features. If you understand how text box objects work, then you have you a way to get to these other features.

Note: Access invites some confusion because it uses the word object in two different ways. Throughout this book, you've referred to all the ingredients of your database (things like tables, queries, and forms) as database objects . Programmers use the word "object" in a stricter sense to refer to a programming construct that brings together related features (and that's how this chapter uses the term ).

You can interact with objects in three ways:

  • Properties . Properties are pieces of information about an object. You change properties to modify the object or how it behaves. A text box object has a FontSize property that controls its text's size .

  • Methods . Methods are actions you can perform with an object. For instance, every form has a Requery method that lets you rerun the query that gets its data.

  • Events . Events are notifications that an object sends out, which you can respond to with your code. You can react to button clicks using the button control's On Click event.

The following sections take a closer look at these three object ingredients.

16.3.1. Properties

Properties aren't anything new. After all, you've spent considerable time tweaking them with the Property Sheet to get just the right formatting and behavior (see Section 13.1.2). However, properties show a whole different side when you change them using your code. With code, you can modify properties dynamically in response to various actions (like clicking a button or editing the text in a text box). This technique opens up a world of new possibilities.

The secret to accessing the built-in features of an object is the lowly period (which programming nerds call the dot operator ). Suppose you want to modify the background color of the Description text box. You can do this job by setting the BackColor property of the corresponding text box object. Here's how it's done:

 Description.BackColor = vbYellow 

This line of code takes the Description object, and then uses the dot operator to pick out its BackColor property. The BackColor is then set with the help of a specially-created keyword, called vbYellow. As you saw with events, the name of a property in code doesn't always match the name of the property in the Property Sheet. In code, property names never include spaces.

You can use this line of code in any subroutine in a form module, as long as that form really and truly has a text box control named Description.

Note: Access colors are set using cryptic numbers . VB simplifies life for the most common colors by giving you predefined names to use, which start with the letters vb . These names are just shorthand ways to refer to the corresponding color numbers. Behind the scenes, vbYellow is really the number 65535. (If you search the Access Help for "vbYellow," you'll find the full list of eight color constants. You'll also learn how to get more color choices in Section 16.4.1.)

If you don't include the dot, then you end up using the default property . For the text box object, the default property's Value, which represents the content of the field. That's why you can write code like this, which doesn't include the dot operator:

 Description = "A very fine product, indeed." 

So now that you know that all the controls on your forms are objects with a whole range of useful details that you can change, the important question is: How do you know what properties there are, and how do you find the ones you want? Several guidelines can help you out:

  • Identical controls have identical properties . Even though every text box on your form is represented by a distinct text box object, each object has exactly the same properties. Obviously, the property values will differ , but you can rest assured that if you find a BackColor property in one text box, you'll find a BackColor property in every other one as well.

  • Similar controls have similar properties . All controls have a BackColor property, whether it's a text box, button, or a lowly label. Even the objects that represent parts of the form (like Detail, FormHeader, and FormFooter) provide a BackColor property for setting background colors. This bit of standardization allows you to reuse what you learn with one control on another control.

  • You can look it up in the Property Sheet . The property names you use in code usually correspond to the property names you see in the Property Sheet. One difference is that the names you use in code never have spaces, so the BackColor property in the Property Sheet becomes the BackColor property in code.

  • You can find it with Visual Basic IntelliSense . The Visual Basic editor offers a great feature that can help you find the property you want. As soon as you type the period after the name of an object, it pops up a list with all the properties and methods you can use for that object (Figure 16-6).

Figure 16-6. When you type an object name, and then enter the period, Visual Basic pops up a list of choices. If you type a few letters, Visual Basic moves to the matching part of the list. If you see the property you want, then you can insert it by clicking it or pressing the Space bar.

Note: The IntelliSense list actually contains two types of elements: properties (which are separate characteristics that are associated with an object) and methods (which are actions you can perform on an object). Properties are far more common, and they're marked with an icon that looks like a hand holding a card. Methods have an icon that looks like a flying green eraser. You'll learn how to use methods in Section 16.3.2.

Table 16-1 lists some control properties that you may want to manipulate with Visual Basic code.

Table 16-1. Useful control properties


Type of Data




Stores the a control's value. Usually, each control's linked to a field, so the Value property lets you read or change a value from the current record. Depending on the field's data type, the property could be text, a number, a True/False value, and so on.


True or False

Determines whether a control value can be changed. If you set this property it to False, it locks the control so the person using the form can't edit the field (although your code can still change the Value property). Controls that are turned off look a little different from enabled controlstypically, they have a grayedout or "dimmed" appearance.


True or False

Determines whether the person using a form can see a control. If you set this property to False, then the control disappears from the form. This property's a handy way to hide fields that don't apply. If a customer lives in Zambia, you can hide the State box.

ForeColor and BackColor

A number

Determines the color that's used for text (the foreground color) and the color that's shown behind the text (the background color).

Left and Top

A number

Determines the position of a control on a form. The Left property provides the distance between the left edge of the form and the left edge of the control. The Top property gives the distance between the top of the form and the top edge of the control. Both values are in pixels (see Section 16.3.1).

Width and Height

A number

Determines the size of a control, in pixels.

FontName and FontSize

A text string and a number (respectively)

Determines the font that's used to show the text in a control. FontName is the name of the font (like "Arial") and FontSize is the point size (like 10).

FontBold and FontItalic

True or False

Determines whether text should be bolded or italicized.

Picture [1]

A text string

Lets you show a background picture on part of a form, or a tab, image, or button. You supply a path that points to a picture file.

Text [2]

A text string

Provides the current text inside a text box. In most cases, this property gives you the same information as the Value property. However, if someone's edited the text but hasn't yet moved to another control, these properties differ. In this situation, Value is the text that's stored in the table and Text is the newly edited information that hasn't been applied yet.

Caption [3]

A text string

Sets the text for a label or button control, or the title of a form. This property's important when you create labels that aren't linked to fields in a table. You could use a label to display a status message.

ItemsSelected [4]

A collection object

Provides a collection, which is a special type of object that contains zero or more subobjects. This collection holds the values of all the items currently selected in the list. The ItemsSelected property's useful only if you've created a list that supports multiple selections. Otherwise, use the Value property.

[1] These properties are more specialized, and they don't apply to most controls.

[2] These properties are more specialized, and they don't apply to most controls.

[3] These properties are more specialized, and they don't apply to most controls.

[4] These properties are more specialized, and they don't apply to most controls.

Interacting with Other Forms

As you learned in Chapter 15 (Section 15.5.2), you can retrieve values or set properties for the fields and controls in the current form, or in other forms that are currently open . The trick's that you need to explicitly tell Access what form you're trying to use.

Suppose you want to change the color of the Price control on a form named Product when you click a button on a form named PriceChanger. This code doesn't work, because Access looks for a Price control on the PriceChanger form, which doesn't exist:

 Price.BackColor = vbRed 

However, this code does the trick nicely , by directing Access to the right form:

 Forms("Product").Price.BackColor = vbRed 

Technically, this code tells Access to look in the Forms collection , which keeps track of all the currently open forms. (If Product isn't currently open, then this statement fails.) It grabs the form named Product from the collection, reaches into the form to get its Price control, and then digs into the Price control to find the BackColor property.

There are actually two ways to write this same line of logic. Old-school Access programmers use a wacky syntax with exclamation marks that looks like this:

 Forms!Product!Price!BackColor = vbRed 

Access interprets both lines of code in the same way. It's really just a matter of taste. However, you should be familiar with both approaches in case you see bizarre!code!with!exclamation!marks!

If you're a little troubled by the fact that this technique causes an error if the form you need isn't open, then two techniques can help you out. In Chapter 17 (Section 17.3.1), you'll learn how to open a form at will.

16.3.2. Methods

Methods let you perform actions with an object. In many cases, calling a method does more than setting a property. In fact, a single method may launch a complex operation that affects many properties. The Requery method tells your form to get the latest data from the database, and then refresh all its controls.

Tip: When you use controls, you'll spend most of time working with properties. In fact, controls have a whole lot of properties, but just a few odd methods.

To use a method, you type the object name, followed by a period, followed by the method name. However, you don't use the equal sign because you aren't setting the method. You're just calling it into action.

Here's an example that refreshes the current record on a form using the Refresh method:


In some cases, a method requires some extra information. If this is the case, you'll know, because Visual Basic's IntelliSense lets you know as you write your code (see Figure 16-7).

Figure 16-7. The rarely used Move method lets you reposition and resize a control in one blow. Once you type the name of this method, the Visual Basic editor shows you the four values you can supply. In this example, only the first value (Left) is requiredthe others are placed in square brackets, which means you can leave them out.

If you need to supply extra information for a method, you must add a space after the method name, followed by the appropriate value. If you need to supply several values, you separate each one with a comma. Here's an example that moves a control to the top left corner of a form:

 Description.Move 0, 0 

Table 16-2 lists the most important control methods.

Table 16-2. Useful control methods




Moves the cursor into the control, so that it becomes the currently active control. This technique's useful when you're performing validation. If you notice an error in a field, you can send the person back to the control that has the error.


Reverse any recent (uncommitted) changes in a control. You can also call this method on a form to abandon all changes and revert back to the original values. If the form isn't currently in edit mode, then this method does nothing.

Recalc [5]

Recalculates any expressions (Section 13.2.3) in the controls of a form.

Refresh [6]

Gets the latest values for this record from the table, and then refreshes the form accordingly . This method's useful if you've just triggered another task that may have modified the record, or if you're using a multiuser database (see Chapter 18), where several people might be changing a record at once.

Requery [7]

Re-runs the query that's used to get the data for the form, and then shows that data, starting at the first record. This method's like Refresh, but instead of affecting the current record, it refreshes them all. You can also use this method on a lookup list to refresh its contents.

[5] These methods apply only to form objects, not individual controls.

[6] These methods apply only to form objects, not individual controls.

[7] These methods apply only to form objects, not individual controls.

16.3.3. Events

As you know, events are the notifications that objects use to tell your code something important has just happened . You've already mastered events, and you've used them in this chapter to react to button clicks. For a list of the most common control events, refer to Section 15.5.1.

One topic that you haven't considered yet is how events can provide extra bits of information. As you may have already noticed, every subroutine includes a pair of parentheses. Here's a second look:

 Private Sub ButtonOfPower_Click() 

In the examples you've seen so far, those parentheses don't contain anything at all. However, they exist for a reason. Some events provide your code with additional event information, and this information's sandwiched between the two parentheses.

Consider the On Key Press event of a text box, which occurs every time someone types a character. It provides a special numeric code that represents the key that was pressed. (Programmers call this the ASCII code .)

If you add a subroutine to respond to the On Key Press event, then Access generates code like this:

 Private Sub MyTextBox_KeyPress(KeyAscii As Integer) End Sub 

This code means that the On Key Press event's providing your code with another piece of information. It's an integer (whole number) named KeyAscii, and you can use it in your own code. Here's an example that simply shows the key code in a Message box:

 Private Sub MyTextBox_KeyPress(KeyAscii As Integer)     MsgBox "Your pressed the key with the code: " & KeyAscii End Sub 

Some events provide several pieces of information. In these cases, you'll spot a whole list between the parentheses. Each piece of information's separated by a comma, and called a parameter .

Note: Technically, parameters are a type of variable . Variables are handy containers that store some information. (This information can vary , which is what gives them their name.) You'll learn more about using variables in Section 17.1.

Here's an example for the On Mouse Move event (which occurs when you move the mouse pointer over the control). The opening declaration for the subroutine is so long that it had to be split over two lines using the underscore :

 Private Sub SomeControl_MouseMove(Button As Integer, _   Shift As Integer, X As Single, Y As Single) End Sub 

In this case, you get four pieces of information. The Button parameter indicates which mouse buttons are currently pressed. The Shift parameter indicates whether the Shift, Ctrl, and Alt keys were held down while the mouse was moved. Finally, the X and Y parameters indicate where the mouse pointer is (its coordinates).

Access 2007[c] The Missing Manual
Access 2007[c] The Missing Manual
ISBN: 596527608
Year: 2007
Pages: 153 © 2008-2017.
If you may any questions please contact us: