February 28, 2021
Hot Topics:

A Beginner's Guide to COM Interfaces

  • By Sam Huggill
  • Send Email »
  • More Articles »

The clever bit is now in the form's code. On the form add two command buttons (cmdGet and cmdSave) and a text box (txtName). In the General Declarations procedure declare these two variables:

Private Person As CPerson
Private DataObj As IDataObject

The best code is in the Form_Load procedure. What we are going to do is to create a new instance of the CPerson class, and set the DataObj variable to the new CPerson instance. What this does is it makes both variables point to the same object, that is IDataObject. But Person also points to CPerson. So if you want to interface with the database, use the DataObj variable and if you want to interface with the person information use the Person variable. To demonstrate this, copy the following code into the form:

Private Sub cmdUpdate_Click()

If DataObj.IsChanged Then
Person.ID = 1
Person.Name = txtName
End If

End Sub

Private Sub cmdGet_Click()
Person.ID = 1
txtName = Person.Name
End Sub

Private Sub Form_Load()
Set Person = New CPerson
Set DataObj = Person
DataObj.IsChanged = False
End Sub

Private Sub txtName_Change()
DataObj.IsChanged = True
End Sub

This reads in the first record (cmdGet) into txtName where you can change it (and if you do then it sets the IsChanged flag to True). Then the cmdSave button saves the new name if the IsChanged flag is True.

You can now create multiple classes that implement the IDataObject interface very quickly and easily. Take a look at the example below.

Page 5 of 6

This article was originally published on November 17, 2002

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date