Getting the value of a property

The syntax for referring to an object (or collection) property follows this general form: where objectCollectionName is any valid object or collection name, and property is any valid property for that object. The dot (.) is the delimiter that separates the object name from the property name.

For example, all collections have a Count property that contains the number of items in the collection. Remember that the Forms collection is an object that contains all open forms in Access. Thus, Forms.Count returns the number of open forms in the database. You could see this for yourself by typing the following line in the Immediate window and pressing Enter:

? Forms.Count

As always in the Immediate window, the question mark asks "What is?" In this case, you're asking the Immediate window, "What is the forms count in this database?" (or, "How many forms are open right now in this database?").

If no forms are open in Access, Forms.Count returns 0 (zero). If you open a form (in Form view) in Access and then execute the ? Forms.Count statement again, it returns 1. In other words, the value returned by Forms.Count is equal to the number of forms that are open in Access — 0 (zero) if no forms are open, 1 if one form is open, 2 if two forms are open, and so forth.

To refer to specific objects in a database, VBA uses the same identifier syntax that's used in Access expressions. An identifier can use two different characters as delimiters (separators) between words: either an exclamation point (!) or a period (.). Programmer lingo for these characters is bang and dot, respectively. The general rules for using them are listed here:

I ! (bang): Use the bang character to precede any name you made up yourself, such as the name of a form you created or the name of a control you created on the form.

I . (dot): Use a dot to precede a property name or any "official" name that you didn't make up yourself.

For example, in Forms!myForm!myButton. Visible, both myForm and myButton are names that we made up. We did so while creating those objects in Access. Both names are preceded by a bang (!) character because they're both names we made up.

The final name in the identifier, Visible, is a reference to the object's Visible property. We didn't make up the name Visible ourselves: Rather, that's the Access name for the property, as you can see in the property sheet shown in Figure 5-6. Because Visible is an "official" property name, its name is preceded with a dot (.) rather than a bang (!).

For more information on identifiers, your best bet is to consult an Access book (as opposed to an Access VBA book, like this one). Or you can just search the Access Help (not VBA Help) for the word identifier.

To reexecute a statement in the Immediate window, just move the cursor back to the end of the statement that you want to execute and then press Enter. To quickly delete text in the Immediate window, drag the mouse pointer over it and press Delete.

Every control on every form has a Visible property that determines whether the control is visible on the form. When Visible equals True (Yes), the control is visible. Conversely, when the Visible property is False (No), the control is not visible.

When creating your own forms, you might find instances when you want a control to be visible to the user and instances when you don't want it to be visible. For example, on a form that allows a user to enter payment information for an order, you might want to make controls for entering credit card information visible only when the customer is paying by credit card. If the customer pays by check or cash, you might want to make those same controls invisible so that the user doesn't accidentally choose Cash or Check and then also type in credit card information.

The syntax for referring to the Visible property of a control named myButton is myButton.Visible. However, as with Access expressions, getting to a specific object from outside its container requires using an identifier, which provides the complete path to the object. For example, the line

FormslmyFormlmyButton.Visible refers specifically to the Visible property of a control named myButton on a form named myForm. The Forms! part at the beginning refers to the Forms collection, which contains all forms that are open in Access. Figure 5-6 illustrates how Forms!myForm!myButton.Visible refers to the Visible property of the myButton control.

Forms collection (all open forms)

Figure 5-6:

Forms! myForm! myButton. Visible from an Access viewpoint.

0 0

Post a comment