Properties and Methods Made Easy
To modify an object's properties and execute its methods, you must refer to the object and then supply an appropriate property or method, as shown in this example:Forms.frmHello.cmdHello.Visible = False
This line of code refers to the Visible property of cmdHello, found in the frmHello form, which is in the Forms collection. Notice that you must identify the object name frmHello as being associated with the Forms collection. If you want to change the Caption property of frmHello to say "Hello World", you would use the following code:Forms.frmHello.Caption = "Hello World"
Telling the Difference Between Properties and Methods
You might be confused about whether you're looking at an object's property or method, but there are a couple of quick ways to tell. You will always use a property in some type of an expression. For example, you might be setting a property equal to some value:Forms.frmClients.txtAddress.Visible = False
Here, you're changing the Visible property of the txtAddress text box on the frmClients form from True to False. You also might retrieve the value of a property and place it in a variable:strFirstName = Forms.frmClients.txtFirstName.Value
You also might use the value of a property in an expression, as in the following example:MsgBox Forms.frmClients.txtFirstName.Value
The pattern here is that you will always use a property somewhere in an expression. You can set it equal to something, or something can be set equal to its value, or it's otherwise used in an expression.A method, however, is an action an object takes on itself. The syntax for a method is Object.Method . A method isn't set equal to something; however, you frequently create an object variable and then set it by invoking a method. A method looks like this:Forms.frmHello.txtHelloWorld.SetFocus
In this example, the text box called txtHelloWorld executes its SetFocus method.A method that returns an object variable looks like this:Dim cbr As CommandBar
Set cbr = CommandBars.Add("MyNewCommandBar")
In this example, the CommandBars collection's Add method is used to set the value of the CommandBar object variable named cbr. For more information, see the section "Declaring and Assigning Object Variables," later in this chapter.
Using a Bang Versus a Period
Many people are confused about when to use a bang (!) and when to use a period. You can use a bang whenever you're separating an object from its collection, as shown in these two examples:Forms!frmClients
Forms!frmClients!txtClientID
In the first example, frmClients is part of the Forms collection. In the second example, txtClientID is part of the Controls collection of the frmClients form.In most cases, you can also use a period to separate an object from its collection. This is because the expression Me!txtClientID is actually a shortcut to the complete reference Me.Controls!txtClientID. Because Controls is the default collection for a form, you can omit Controls from the statement. You can abbreviate the expression to Me.txtClientID. The advantage of using the dot over the bang is that the dot provides you with Intellisense. To test this, create a form and add a control called txtFirstName. Go to the code behind the form and try typing Me! . Notice that Intellisense is not invoked. Next type Me . and watch as Intellisense is invoked. Intellisense facilitates the development process by providing a list box containing valid properties, methods, constants, and so on, as appropriate.NOTEIntellisense is a tool that helps you when writing programming code. It provides you with auto-completion when writing your programming code.In addition to separating an object from its collection, the period is also used to separate an object from a property or method. The code looks like this:Forms.frmClients.RecordSource = "tblClients"
Forms.frmClients.txtClientID.Visible = False
The first example sets the RecordSource property of frmClients to tblClients, and the second example sets the Visible property of the txtClientID on the frmClients form to False.
Default Properties
Each object has a default property, and if you're working with an object's default property, you don't have to explicitly refer to it in code. Take a look at the following two code samples:Forms.frmHello.txtHello.Value = "Hello World"
Forms.frmHello.txtHello = "Hello World"
The Value property is the default property of a text box, so you don't need to explicitly refer to it in code. However, I prefer to explicitly state the propertyit is a practice that contributes to the code's readability and keeps novice Access programmers who work with my code from having to guess which property I'm changing.