Data persistence is the ability of a component to store and retrieve data. How an ActiveX document persists data depends greatly on the container application in which the document appears. Some applications allow you to persist data by writing to an interface of the application. For example, the Internet Explorer 3.0 (and higher) and Microsoft Office Binder allows you to write to a file using the PropertyBag. Other applications, however, do not provide any such intrinsic method of storing data. In those cases, you must use another method, such as writing data to files. Let's start with the PropertyBag.
The PropertyBag is an object that allows you to save data to a file. The file can be either the .vbd, .obd, or some other kind of file — it depends on the application. The PropertyBag has two methods, the WriteProperty method, and the ReadProperty method. The PropertyBag object is exposed as part of the WriteProperties and ReadProperties event declaration.
The process of saving data is outlined:
The following code shows a simple illustration of this process:
Private Sub Text1_Change()
PropertyChanged "Text" ' Notify container a
' property has changed.
End Sub
Private Sub UserDocument_WriteProperties _
(PropBag As VB.PropertyBag)
' Write the Property to the Property Bag.
UserDocument.WriteProperty "Text1", Text1.Text, _
"Hello"
End Sub
The next time the ActiveX document is opened, the ReadProperties event occurs. The PropertyBag is available in the event, and you can retrieve the persisted data using the ReadProperty method. The following code retrieves the data stored in the preceding code:
Private Sub UserDocument_ReadProperties _
(PropBag As VB.PropertyBag)
' Read the property back into the TextBox control.
Text1.Text = ReadProperty("Text1", "Hello")
End Sub
You may have noticed that two code samples included an extra argument: the default value "Hello." Why provide a default when saving the property value? Before saving the value, the WriteProperty method compares the property value with this default. If they are the same, the property value doesn’t have to be saved, because default values will be set automatically when the control is reloaded. This keeps the data file from being cluttered with hundreds of default entries.
It's also possible to write and read binary data to the PropertyBag. This becomes useful if you have data that is stored in binary form. For example, you may have a graphics file that is stored in a custom format.
The process of saving binary data differs slightly from that of saving control properties:
These steps are shown in the following simple implementation:
Private mbytMyData() As Byte ' Declare byte array.
Private Sub cmdSavePic_Click()
ReDim mbytMyData(1 to 5000)
' Code to move data into byte array not shown.
PropertyChanged
End Sub
Private Sub UserDocument_WriteProperties(PropBag As _
VB.PropertyBag)
PropBag.WriteProperties "myPic", mbytMyData
End Sub
If you are using a container that does not support the PropertyBag, or if you are saving User Defined Type (UDT) data, use the Open statement to read and write the data to disk. Instructions on using the Open statement can be found in "Using Sequential File Access" in "Processing Drives, Folders, and Files" of the Programmer's Guide.
For More Information To learn the do's and don'ts of adding properties to a components, see "Adding Properties And Methods to Classes," in "General Principles of Component Design."