Objects and Collections
An object is anything that can be described. In the real world, examples of objects are the moon, a ball, a hand, a book, a head, a song, a box. As different as objects are, they follow some basic rules used to describe them. A characteristic of an object is a word or a group of words used to describe it. Some characteristics are applied to all objects. For example, every object must be identified with a word or a group of words referred to as its name:
Some characteristics apply to a group of objects but don't apply to another group. For example, a characteristic called width can be used to describe a car, a medical pill, a piece of paper, or a computer monitor. Here are examples of characteristics applied to different objects:
Some other characteristics can be applied to one object or one type of object that is mostly unique while those characteristics cannot be applied to some other objects
When creating a database, you will also use objects but these are referred to as Windows controls or simply controls. In a typical application, you will choose the objects, that is, the controls that you judge necessary and you will make them part of your application. Here is an example of a form with various controls:
As we move, we will indicate how to choose controls and populate a form with them.
In the programming world, a characteristic of an object is referred to as a property of that object. For example, as mentioned above, every object must have a name. The name is used to identify the object. In the same way, the other characteristics that we reviewed above are in fact the properties of that object.
Because every object has properties, they can be created as a list. Consider the table we saw abve:
The properties of each object are: its name, its external color, and its unit price. This can be illustrated as:
To represent an object, that is, to describe it, you can give a value to each property. For example, the properties of the digital camera from the above table are its name, its color, and its price. The values of the properties of that camera are: Name: Digital Camera, External Color: Black, Unit Price: $899.95. This can be illustrated as:
From this illustration, it is important to make a distinction between a property and its value: a property is a word or a group of words used to define what constitutes an object. A value is the word or a group of words used to formally describe an object. In the programming world, the name of a property is always of one word only, as the Name property in the above table. If a name is made of more than one word, then they must simply be combined into one. In the same way, the value of a property is made of only one word. Also, if the name is a combination of words, they must be concatenated (added) to produce one word. Based on this, the properties and their values from the above table would be:
Just as done in the real world, Microsoft Access also relies on objects to represent a database. One of the most regularly used objects of a database is called a table. Another regularly used object of an application is called a form. There are many other objects as we will find out in future lessons.
Each object has properties. To effectively use an object, you should be familiar with its properties or at least how to use them to describe an object.
During the design of an object, you will have a window that represents its properties. The window that displays the list of properties is different depending on the object and depending on whether you are working in Microsoft Access or in Microsoft Visual Basic. For a form, which is probably the most regularly used object of an application, on this site, the window that presents its properties will be called the Properties window. It appears as a resizable horizontal window with 5 tabs labeled Format, Data, Event, Other, and All:
As mentioned in the previous lesson, in Microsoft Visual Basic, the Properties window usually appears in the lower left section of the screen and appears with 2 tabs labeled Alphabetic and Categorized:
To visually configure a property, you must first locate it in the Properties window. If you are working in Microsoft Access, the properties are categorized in three tabs: Format, Data, and Other. All of these properties are also represented in the All tab. Each property appears with its name as in the real world: in different words. Examples are Caption, Default View, or Min Max Buttons. After locating the property, to see or change its value, you use the box to its right. This means that a property is made of two sections: a property name and a property value. This can be illustrated as follows:
If you are working in Microsoft Visual Basic, you can use the same approach to change a property using the Properties window. This time, the names of properties appear in one word and they are in their official format.
To programmatically change a property, in your code section, type the name of its object, followed by a period operator ".", followed by the official name of the property, followed by the assignment operator, and followed by the desired value. This means that you must know the name of the object whose property you want to change. You must know the (true) name of the property you want to change, and you must know the possible values that the property can receive. Here is an example of code that hides a rectangular box, named boxRectangle, when the user clicks a button:
Private Sub cmdHide_Click() boxRectangle.Visible = False End Sub
We mentioned earlier that the names of objects are usually in one word. In reality, Microsoft Access is very flexible and allows you to use more than one word to name an object (but the properties names are always in one word). If you have an object that is made of more than one word, when referring to it in an expression, whether in the Properties window or with your code, you must include it between the opening square bracket "[" and the closing square bracket "]". For example, suppose that you have a box named Rectangular Box instead of boxRectangle. The above code would be written:
Private Sub cmdHide_Click() [Rectangular Box].Visible = False End Sub
There is no penalty if you always include your name in square brackets when referring to them whether in the Properties window or with code, even if the name is made of one word. Here is an example:
Private Sub cmdHide_Click() [boxRectangle].Visible = False End Sub
This would produce the same result as above.
As an alternative, instead of typing the name of the control over and over again, you can use the With operator whose formula is:
With ObjectName Statements End With
On the right side of the With keyword, type the name of the control whose properties you want to access. Under the With ObjectName line, type your statements and expressions as you wish but start each property of the ObjectName with the period operator. At the end of the With statement, type End With. Based on this, the above code would have been written:
Private Sub cmdManipulate_Click() With boxEnvelop .BackStyle = 1 .BackColor = 979478 .SpecialEffect = 1 .BorderColor = 234657 .BorderWidth = 2 End With End Sub
Because properties are meant to accomplish various goals, they are also configured differently. In previous sections, we saw that the name of a property was represented on the left column of a tab in the Properties window and the value of the property was on the right side.
There are various kinds of fields you will use to set the properties. To know what particular kind a field is, you can click its name. To set or change a property, you use the box on the right side of the property’s name.
There are fields that expect you to type a value. Most of these fields have default text.
At design time, that is, in the Properties window, the text you provide will appear "as is" and cannot change. If you want the text to change in response to something, you must write code.
Some fields expect a numeric value. In this case, you can click the name of the field and type the desired value. If you type an invalid value, you would receive a message box notifying you of the error:
When this happens, click OK and type a valid value. If the value is supposed to be an integer, make sure you don't type it as a decimal number.
To change the value of some of the fields, you would use their combo box to display the available values. After clicking the arrow, a list would display. Here is an example:
There are various types of list-based fields. Some of them display just two items. To change their value, you can just double-click the field. Some other fields have more than two values. To change them, you can click their arrow and select from the list. You can also double-click a few times until the desired value is selected.
As opposed to being described, an object can perform actions. For example, a Person object can sing. A Car object can move. An insect can crawl. A basket can hold some clothes. Here are examples of actions:
Notice that some objects can perform similar types of actions. For example all objects of this table can move. Some objects can perform actions that some others can't. For example, from the objects in the above table, a person and a dog can eat.
In the programming world, an action that an object can perform is called a method. Like a property, a method must have a name. On the above table, notice that the name of a method usually (if not always) resembles a verb. Examples are Bark, Eat, or Drink. To make a distinction between a property and a method, on this site, we will always follow the name of a method with empty parentheses. Examples are Move() or Bark().
Like a property, the name of a method is always in one word. Examples are Walk() or Crawl(). If the name is a combination of words, each part starts in uppercase. Examples would be TalkAloud(), SmellFood(), EatGrass(), or ProtectFromRain().
Because a method is performed, it cannot be represented in a window such as the Properties window. Eventually, we will know how to identify and use the methods of an object. Using a method is referred to as calling it. When a method can produce a result, it can be assigned to a property.
To programmatically call a method, type the name of the object that owns the method, followed by a period operator, and followed by the name of the method.
|Previous||Copyright © 2005-2015, FunctionX||Next|