Visual Basic Concepts

Creating Objects at Run Time with the OLE Container Control

See Also

To create a linked or embedded object at run time, you use methods and properties in code. The OLE container control has a variety of properties and methods that you can use for manipulating linked or embedded objects. For a complete list of the properties and methods that apply to the OLE container control, see "OLE Container Control."

Using the Object Property

By using the OLE container control's Object property, you can also use the properties and methods of the linked or embedded object. The Object property is a run-time, read-only property that holds a reference to the object in an OLE container control. Use this property to perform Automation tasks with the OLE container control, including programmatically manipulating the properties and methods an object supports:

strObjName = oleObj1.Object.Name

To use this property, the OLE container control must contain an object that is programmable. For more information on programmable objects, see "Types of ActiveX Components."

Creating Linked Objects at Run Time

You can create a linked object from a file at run time with the OLE container control's CreateLink method. This method takes one argument, sourcedoc, which is the file from which the object is created, and an optional argument, sourceitem, which specifies the data you want to link from within the source file. The following code fragment creates a linked object at run time:

oleObj1.CreateLink "C:\Excel\Test.xls"

Note   If you use CreateLink to create a linked object, you do not have to set the Class, SourceDoc, and SourceItem properties in the Properties window.

For More Information   See "CreateLink Method."

Creating Embedded Objects at Run Time

To create an embedded object from a file at run time, you can use the CreateEmbed method. This method has two arguments, sourcedoc and class (which is optional if SourceDoc is specified). Sourcedoc determines the template for the object, and class determines the object type. When you use CreateEmbed, you do not need to set the SourceDoc and Class properties.

The following code fragment creates an embedded object using an existing file as a template for the object.

oleObj1.CreateEmbed "Q1profit.xls"

For More Information   See "CreateEmbed Method."

When you create an empty embedded object, it is a good idea to activate the ActiveX component that will provide data for the object. You can do this with the DoVerb method. This allows the user to enter any data into the application at run time. The user can then show this newly entered data in the OLE container control by choosing the ActiveX component's Update command (this menu command should appear on the component's File menu).

To create an empty embedded object at run time

  1. Use the CreateEmbed method without specifying a source document to create an empty embedded object. For example, this code fragment inserts a file template for a Microsoft Excel Worksheet in the OLE container control:
    oleObj1.CreateEmbed "","Excel.Sheet"
    
  2. Use the DoVerb method. The default verb for the DoVerb method depends on the application. With Microsoft Excel, the default verb is Edit.

For example, the following code fragment creates an empty embedded object and then activates the application that created it using the default DoVerb action.

oleObj1.CreateEmbed "", "Excel.Sheet"
oleObj1.DoVerb -5  ' Activate

Providing empty embedded objects is useful when creating a document-centered application that uses a variety of information from different applications. For more information, see "Letting the User Specify Objects at Run Time."

Binding a Database to the OLE Container Control

You can bind the OLE container control to data stored in the Microsoft Jet database engine or Microsoft Access database. You may want to do this, for example, if you have a database with a table of employee pictures. If the pictures are stored as objects, you can bind them to the OLE container control and display them on a form as each record is accessed with the data control. To bind data to one of these databases, specify the source of data (recordset name) in the DataSource property and the field name from that data source in the DataField property of the OLE container control. When displaying an object from a database, the OLE container control allows the user to activate, edit, and update the object. As with any bound control, the updated object is automatically written back to the database when the record position is changed.

For More Information   See the Data Access Guide.