Updating Linked Data


By default, Visual Basic updates linked objects whenever the user saves the source data while editing the linked object. However, Visual Basic currently does not automatically handle changes that other users make to the source file (such as storing a file on a network), nor do such changes currently trigger events in Visual Basic.

To control updating in code, use the OLE control's UpdateOptions property, the Updated event, and the Update method.

Table 18.5 describes the UpdateOptions property's three settings.

Table 18.5 - The UpdateOptions Property Settings

UpdateOptionsSetting Value Description
vbOLEAutomatic (default) 0 Updates the OLE control when the user changes or saves the data in the linked object. Not all applications update the OLE control on every edit, however.
vbOLEFrozen 1 The user can edit the object, but the displayed object does not reflect the edits. Calling the Update method does not update the object from its source.
vbOLEManual 2 The user can edit the object, but the displayed object doesn't reflect the edits. You can use the Update method to update the object from its source.

The Updated event occurs when the user edits, closes, or renames the source file while editing the object from within the OLE control. The Updated event also can occur when the OLE control first loads (as at startup) if the source file for the link has changed since the last time that the control loaded.

These events occur regardless of the UpdateOptions property setting. However, not all applications notify the OLE control as they are updated. For instance, Microsoft Word does not trigger the Updated event until the user saves or closes the file. Microsoft Excel, however, triggers the Updated event each time a cell of data on a worksheet changes.

The Updated event procedure has this form:


Private Sub OLE1_Updated(Code As Integer)
    '... your code here.
End Sub

The Code argument corresponds to the type of update that occurred. Table 18.6 describes the possible values.

Table 18.6 - Possible Values of the Code Argument

Code Constant Value Meaning
vbOLEChanged 0 The object's data has changed.
vbOLESaved 1 The application that created the object has saved the object's data.
vbOLEClosed 2 The application that created the linked object has closed the file that contains the object's data.
vbOLERenamed 3 The application that created the linked object has renamed the file that contains the linked object's data.

Some actions might cause more than one event to occur. For example, if the user closes a file and saves changes, the OLE control receives three Updated successive events: vbOLEChanged, vbOLESaved, and vbOLEClosed.

Use the Update method to update linked objects from their source files. In Visual Basic 4.0, Microsoft has changed this method, which in Visual Basic 3.0 used the Action property setting OLE_UPDATE to update links. In Visual Basic 4.0, the Action property still works, but using the Update method makes code clearer and easier to understand.