Visual Basic Concepts
Before you can use the properties, methods, and events of an object provided by an ActiveX component, you must first declare an object variable. The way you declare an object variable depends on whether or not the ActiveX component supplies a type library.
To declare a variable for an object defined in a type library
Dim variable As [New] class
The class argument can be composed of two parts, in the form component.class.
Part | Description |
component | The name of the component that supplies the object. Choices are displayed in the Project/Library list of the Object Browser. |
class | The object's class name (provided by the component's type library). Choices are shown in the Classes/Modules box of the Object Browser. |
For example, you can declare a variable for a Microsoft Excel Chart object in either of the following ways:
Dim xlChart As Chart
Dim xlChart As Excel.Chart
If you declare an object variable using the New keyword, Visual Basic will automatically create an object and assign an object reference the first time you use the variable. For example, the following statements assign a reference to a new DAO table object to the variable tdfOrders, setting the table's Name property to "Orders":
Dim tdfOrders As New TableDef
tdfOrders.Name = "Orders"
Note Using variables declared using the New keyword can slow your application. Every time Visual Basic encounters a variable declared using New, it must test whether or not an object reference has already been assigned to the variable.
To declare an object variable for an object not defined in a type library
Dim variable As Object
For example, the variable objAny
in the following declaration can be used for a Microsoft Excel Chart object or any other object provided by an ActiveX component:
Dim objAny As Object
The main difference between declaring a variable of a specific class and declaring a variable of the generic Object class is in how ActiveX binds the variable to the object. When you declare a variable of the generic Object class, ActiveX must use late binding. When you declare an object variable of a specific class, ActiveX uses early binding, which can speed object references. For more information, see "Speeding Object References" later in this chapter.
For More Information For more information on declaring object variables, see "Dim Statement." For more information on assigning an object reference to a variable, see "Assigning an Object Reference to a Variable."