Craig McCaffrey's Objects 101

 


t" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">

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 the object. 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:

Object Person Table Ball Insect Map
Name Person Table Ball Insect Map

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:

Common Characteristics Digital Camera Clothes Basket Car Tennis Ball
Name Digital Camera Basket Car Tennis Ball
External Color Black Blue Grey Yellow
Unit Price 899.95 24.55 19995.00 6.25

Some other characteristics can be applied to one object or one type of object that is mostly unique.

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:

Bethesda Car Rental - Order Processing

Practical LearningPractical Learning: Introducing Objects

  1. Start Microsoft Access
  2. From the resources that accompany these lessons, open the Exercise1 database
  3. To display an existing object, In the Navigation Pane, double-click Sample Form
     
    Sample Form

The Properties of an Object

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 second table we saw above:

Common Characteristics Digital Camera Clothes Basket Car Tennis Ball
Name Digital Camera Basket Car Tennis Ball
External Color Black Blue Grey Yellow
Unit Price 899.95 24.55 19995.00 6.25

The properties of each object are: its name, its external color, and its unit price. This can be illustrated as:

Object
Property Name
Name
External Color
Unit Price

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 External Color, and its Unit 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:

Object
Property Name Property Value
Name Digital Camera
External Color Black
Unit Price 899.95

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 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:

Object
Name DigitalCamera
ExternalColor Black
UnitPrice 899.95

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.

Visually Accessing the Properties of an Object

Each object has properties. During the design of an object, you can use the Properties window that represents its properties. The Properties window depends on whether you are using Microsoft Access or Microsoft Visual Basic. For a form, the Properties window appears as a resizable horizontal window with 5 tabs labeled Format, Data, Event, Other, and All:

Properties

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 on 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:

Properties

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:

Properties

Practical LearningPractical Learning: Accessing the Properties of an Object

Programmatically Accessing the Properties of an Object

To programmatically change a property, in your code, 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 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

Using the IntelliSence

When writing code, the Code Editor is equipped to assist you with the names of available properties. When you type the name of a object followed by the period operator, the available properties would appear in a list:

This feature is called IntelliSense. It works on most objects but not on all of them.

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 as:

Private Sub cmdHide_Click()
    [Rectangular Box].Visible = False
End Sub

There is no penalty if you always include the name of an object in square brackets when referring to it 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.

Practical LearningPractical Learning: Introducing Properties

  1. To open Microsoft Visual Basic, on the Ribbon, click Database Tools and click the Visual Basic button
  2. If the Properties window is not visible, on the main menu, click View -> Properties Window.
    In the Projects window, expand Microsoft Office Access Class Objects and double-click Form_Sample Form.
    Notice that the Properties window displays the characteristics of the form
  3. In the Object combo box of the Code Editor, select Form
     
    The Object Combo Box in the Code Editor
  4. Type NavigationButtons = False
     
  5. To return to Microsoft Access, on the Standard toolbar, click the View Microsoft Access button View Microsoft Access
  6. Right-click the title bar of the form and click Form View
     
    Sample Form
  7. After viewing the result, right-click the form (anywhere on it) and click Design View

The Types of Properties

 

Introduction

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:

Property Name and Value

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.

Text-Based Properties

To programmatically specify or change the text property of an object, if the property is called Text, it is considered the default property of the control. This means that, to specify the value of that property, simply type the name of the control and assign the desired string. Here is an example:

Private Sub cmdSpecifyFullName_Click()
    txtFullName = "Patrick Nguema Ndong"
End Sub

If the property is named Caption, type the name of the object, followed by a period, followed by Caption, and assign the desired string. Here is an example:

Private Sub cmdAction_Click()
    cmdProperties.Caption = "Make Things Happen"
End Sub

During design, the text or caption you provide to the control is static. This means that you can only provide a fixed value for the control. If you want the text to change in response to something, you must write code. Here is an example:

Private Sub cmdCurrentDate_Click()
    Caption = Date
End Sub

With this example, every time the button is clicked, the form would display today's date.

Practical LearningPractical Learning: Adding a Control to a Form

Numeric-Based Properties

A property is referred to as numeric when its value must be a number. To programmatically specify the value, access the object's property and assign the desired value:

Private Sub cmdChangeHeight_Click()
    Detail.Height = 1450
End Sub

Empty Fields

In the Properties window, some fields appear empty. The values of most of those properties depend on other characteristics of an object. To programmatically set the property, use the approach we reviewed earlier: the name of the property, the assignment operator, and the desired value.

Boolean-Based Properties

A Boolean property is one whose value can be True or False. To programmatically specify or change a Boolean property, access it by its name and assign True or False to it. Here is an example:

Private Sub cmdHideRecordSelector_Click()
    RecordSelectors = False
End Sub

Action-Based Properties

Some properties use intermediary values. Programmatically changing the value of an action field depends on the type of property. If the property requires a file, you can assign the name and/path of the file to the property. Here is an example:

Private Sub cmdAction_Click()
    Picture = "C:\My Documents\My Pictures\business.gif"
End Sub

In some other cases, you may have to provide more values than that.

List-Based Properties

To programmatically specify or change the value of a list-based property, you must know either the name of the value or its equivalent constant integer. Here is an example:

Private Sub cmdAction_Click()
    txtFullName.FontWeight = 600
End Sub