|

How to Use Objects, Properties, Methods, and Events in Visual Basic

This is an excerpt from Microsoft Excel 2000 Visual Basic Help.

1.        Objects and Collections

An object represents an element of an application, such as a worksheet, a cell, a chart, a form, or a report.  In Visual Basic code, you must first identify an object.  Only then can you apply one of the object’s methods or change the value of one of its properties.

A collection is an object that contains several other objects.  Usually, but not always, they will be of the same type. In Excel, for example, the Workbooks object contains all the open Workbook objects. In Visual Basic, the Forms collection contains all the Form objects in an application.

Items in a collection can be identified by number or by name. For example, in the following procedure, Workbooks(1) identifies the first open Workbook object.

Sub CloseFirst()
    Workbooks(1).Close
End Sub

The following procedure uses a name specified as a string to identify a Form object.

Sub CloseForm()
    Forms("MyForm.frm").Close
End Sub

You can also manipulate an entire collection of objects if the objects share common methods. For example, the following procedure closes all open forms.

Sub CloseAll()
    Forms.Close
End Sub

2.        Methods

A method is an action that an object can perform. For example, Add is a method of the ComboBox object, because it adds a new entry to a combo box.

The following procedure uses the Add method to add a new item to a ComboBox.

Sub AddEntry(newEntry as String)
    Combo1.Add newEntry
End Sub

3.        Properties

A property is an attribute of an object.  It defines one of the object’s characteristics, such as size, colour, or screen location.  Or it could be an aspect of its behaviour, such as whether it is enabled or visible. To change the characteristics of an object, you change the values of its properties.

To set the value of a property, use the format: “object.property =new property value”.  For example, the following procedure changes the caption of a Visual Basic form by setting the Caption property.

Sub ChangeName(newTitle$)
    myForm.Caption = newTitle$
End Sub

You can’t set some properties. The Help topic for each property indicates whether you can:

  • Set that property (read-write),
  • Only read the property (read-only), or
  • Only write the property (write-only).

You can retrieve information about an object by returning the value of one of its properties. The following procedure uses a message box to display the title that appears at the top of the currently active form.

Sub GetFormName()
    formName = Screen.ActiveForm.Caption
    MsgBox formName
End Sub

4.        Events

An event is an action recognized by an object, such as clicking the mouse or pressing a key, for which you can write code to respond.  Events can occur as a result of a user action or program code, or they can be triggered by the system.

5.        Returning Objects

Every application has a way to return the objects it contains. However, they are not all the same.  So, you must refer to the Help topic for the object or collection you’re using in the application to see how to return the object.

Similar Posts