Home

Introduction to Objects

 

Objects

 

Introduction

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:

 

Introduction

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. Fom 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. 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, in these lessons, 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:

Properties

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

  • Right-click the button under the title bar and click Design View.
    You should see a window titled Property Sheet. If you don't see it, right-click the form and click Properties
     


    This is the Properties window in Microsoft Access. Notice that it is divided in five property pages

Programmatically Accessing the Properties of an Object

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

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

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
  4. Type NavigationButtons = False
     
  5. To return to Microsoft Access, on the toolbar, click the View Microsoft Access button View Microsoft Access
  6. Right-click the button under the title bar of the form Inter-Rulers and click Form View
     
    Sample Form
  7. After viewing the result, right-click the form (anywhere on it) and click Design View

With the Properties of an Object

Sometimes, you will need to access only one property of an object. In some other cases, you will need to change various properties to perform a specific task. To do this for each property, as we saw above, you can type the name of the object, followed by the period operator, followed by the name of the property, press Enter, and do the same on the next line. Here is an example:

Private Sub cmdManipulate_Click()
    boxEnvelop.BackStyle     = 1
    boxEnvelop.BackColor     = 979478
    boxEnvelop.SpecialEffect = 1
    boxEnvelop.BorderColor   = 234657
    boxEnvelop.BorderWidth   = 2
End Sub

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

The Types of Properties

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.

Text-Based Properties

Some objects use a text to display their value. On such controls, the property is called wither Text or Caption. To visually change the value of the property, click the name of the property, type the desired value, and press Enter.

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:

Text-Based Properties
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.

Numeric-Based Properties

A property is referred to as numeric when its value must be a number. To visually specify such a value, in the Properties window, click the property and type the desired value. 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 visually set the property on such a field, you can type in it or select from a list. 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

 
Boolean-Based Properties

A Boolean property is one whose value can be True or False. In the Properties window, such properties display Yes or No. To change their setting, you can either select from the combo box or double-click the property to toggle to the other value.

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. In the Properties window, such fields display an ellipsis button . When you click the button, a dialog box would come up and you can set the value for the field. For example, to fill the form with a background, you would click its Picture field in the Properties window, use a dialog box and select a picture.

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.

Action Fields

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

Field List

To programmatically specify or change the value of the 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
 
 

The Methods of an Object

 

Introduction

As opposed to being described, an object can perform actions. For example, a Person object can sing. A Car object can move. An Insect object can crawl. A Basket object can hold some clothes. Here are examples of actions:

Objects Person Car Dog Insect
  Person Car Dog Insect
Actions Move Move Move Move
Talk Aloud Protect (from rain) Protect (from intruders) Crawl
Sing Carry Sit Down Smell Food
Eat   Eat Eat
Walk Slowly   Bark Walk

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, in these lessons, 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().

Accessing the Methods of an Object

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 call a method, if it produces a result that can be applied to a property, if you are working in Microsoft Access, in the Properties window, locate the property that will use the result of the method. In the property value section, type the assignment operator "=", followed by the name of the object that owns the method, followed by a period operator, followed by the name of the method and followed by parentheses. In future lessons, we will see that there can be other issues involved with calling a method.

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.

Practical LearningPractical Learning: Calling a Method

  1. Once again, in the Controls section of the Ribbon, click Text Box TextBox and click the middle section of the form
  2. While the text box is still selected, in the Properties window, click the Other tab
  3. Click Name and type Company Name and press Enter
  4. To preview the form, in the Views section of the Ribbon, click the arrow of the View button and click Form View Form View.
    Notice that the caret is blinking in the top text box, indicating that it has focus
  5. To return to design view, on the Ribbon, click the arrow of the View button and click Design View Design View
  6. To return to Visual Basic, on the task bar, click Microsoft Visual Basic
  7. In the Code Editor, click the right side of the first line of code and press Enter
  8. Type [Company Name].SetFocus
     
    Private Sub Form_Load()
    NavigationButtons = False
    [Company Name].SetFocus
    End Sub
  9. To return to Microsoft Access, on the task bar, click Microsoft Access
  10. On the Ribbon, click the View button
  11. Notice that, this time, it is the middle text box that has the blinking caret because it has focus.
    After viewing the result, on the Standard toolbar of Microsoft Access, click the View button View

Messages and Events of Objects

 

Introduction to Messages

A typical application is made of various objects that a person uses to interact with the computer. To make this interaction possible without confusion, each object creates its own messages and sends them to the operating system. The messages are as varied as possible, so are the objects of an application. To reduce any type of confusion, each message must carry three to four pieces of information: the name of the object that composed the message, the type of message that must be dealt with, additional information provided by the object that sent the message.

Anatomy of a Message

Because the operating system cannot decide what type of action a particular object needs to perform at a particular time, each object is responsible to create its own messages. The first piece of information that an object must provide is its name. The reason is that there can be so many objects that are part of an application and there can be many applications opened at the same time.

The second piece of information that a message must carry is its type. There are many types of messages that a control can compose. Many controls can also send the same types of messages. The operating system is already aware of the various types of messages that are available and, most of the time, it knows the types of messages that a particular object can control, but the operating system cannot decide what type of message a control wants to compose at one particular moment. The types of messages are mostly known with particular names. In the operating system, the names of messages start with WM_. If you log to the MSDN web site and do a search on words that start with WM_, you would find out that there are many messages available but you will not need to memorize their names or to be aware of all of them. In fact, there are many messages you will hardly use.

Introduction to Events

After a control has composed a message, it must send it. The action of composing and sending a message is called an event. The action of actually sending a message is referred to as firing an event. To make it easy to identify an event, each event has a simplified name. When an object fires an event, the name of the event is appended to the name of the object. To distinguish the name of the object and the name of the event, there is an underscore between them. Examples are MainForm_Load or Customers_Dirty. As you will find out, the names of some events are made of more than one word. Examples are AfterUpdate or BeforeInsert. In this case, the name of the object and the name of the event are still added with an underscore between them. Examples are MainForm_AfterUpdate or Customers_BeforeInsert.

Additional Information to Carry an Event

After composing a message and firing its event, when the operating system receives it, in order to perform the necessary action, in some cases, it (the operating system) may need additional information that the object must provide. The additional information is also referred to as parameter.

The additional information that an object must provide is typed in the parentheses of the event. Normally, the operating system is aware of the type of additional information that the object must provide. If the object doesn't provide this information, the operating system would use some default value(s). Because the type of additional information of an event is already decided, when you generate an event, Microsoft Visual Basic includes this information in the parentheses of the event. If the message doesn't require additional information, its parentheses are left empty. If it may need parameters, they are automatically included in its parentheses.

Anatomy of an Event

Because the structure of messages can be difficult to cope with, Microsoft Visual Basic has a simplified mechanism to help you code an event. Since an event is treated as a private matter, its code starts with the Private keyword. An event is treated as an action, like the methods we reviewed earlier. For this reason, it is created as a sub procedure and it must use the Sub keyword. After the Private Sub expression, follow the combination of the name of the object that is firing the event, the underscore, and the name of the event, and its parentheses with optional parameters depending on the event.

There are various ways you can initiate an event on an object of your database. If you are working in Microsoft Access and if you open a form or a report, you should access its Properties window in Design View. If the form or report "carries" the control whose event you want to program, you should also first open the form or report in Design View.

  • First Technique
    1. If you want to write code that relates to the whole form or event, double-click its selection button which is at the intersection of the rulers .
      If you want to write code for a control, on the form or report, double-click the control to display its Properties window.

    2. Once the Properties window is displaying, you can click the Event property page and inspect the list of events:
       
      Form Properties

    3. After locating the event you want, double-click it. An [Event Procedure] string will be added to the field:
       
      Events
       
      Once the [Event procedure] is displaying, click the ellipsis button Ellipsis to launch Microsoft Visual Basic. The keyboard caret would be positioned in the event and wait for you.

  • Second Technique

    1. In the Properties window, click the name of the event in the Event property page to reveal its combo box

    2. Click the arrow of the combo box and click [Event procedure]

    3. Once [Event Procedure] is displaying for an event, click the ellipsis button of the event Ellipsis. This will launch Microsoft Visual Basic and positions the caret in the event's body, waiting for your coding instructions.

  • Third Technique

    1. Right-click the object whose event you want to write code for and click Build Event...
       
      Choose Builder

    2. On the Choose Builder dialog box, click Code Builder and click OK. This will launch Microsoft Visual Basic with the default event of the form, report, or control

  • Fourth Technique

    1. In Microsoft Access, display the object in Design View

    2. To launch Microsoft Visual Basic, in the Tools section of the Ribbon, click the View Code button View Code

    3. In the Object combo box, select the object to launch its default event

    4. In the Procedure combo box, select the event you want

Sometimes you or Microsoft Visual Basic will have inserted an event that you did not want or that you do not want to program. If this happens, simply ignore the event: you do not have to delete it because if an event has been initiated but no code was written for it, the application will ignore it and use a default behavior.

Practical LearningPractical Learning: Firing Events

  1. To return to Microsoft Visual Basic, on the task bar, click Microsoft Visual Basic - Exercise1
  2. Notice that the Object combo box is displaying the word Form.
    Click the arrow of the Procedure combo box to display the events of the form:
     
  3. Click Open.
     
  4. Click the arrow of the Procedure combo box again but this time, select ApplyFilter.
    Notice that the Load takes no parameter, the Open event takes one parameter, and the ApplyFilter takes 2 parameters
     
  5. Click the arrow of the Object combo box and notice that Company_Name is in one word
  6. Click Company_Name
  7. Click the arrow of the Procedure combo box and select Change to generate that event
 
   

Previous Copyright © 2005-2011 FunctionX, Inc. Next