In this section, we’ll walk step-by-step through the process of creating a data-aware class that acts as a data source. This example will bind a TextBox control to our data source class in order to display the data. The next section, "Creating a Data Consumer," demonstrates how to bind our data source class to a data consumer class.
The code examples in this section are taken from the Data-aware Classes (Dataware.vbp) sample. You'll find this application in the Samples directory.
Creating a data source is a two-step process. In the first step we’ll create the data source class; in the second step we’ll hook it up to a TextBox control in order to display the output.
The first step in creating a source class is to define a new class and give it the properties and methods necessary to provide data:
Property | Setting |
Name | MySource |
DataSourceBehavior | vbDataSource |
When DataSourceBehavior is set to vbDataSource, a new Sub procedure GetDataMember is added to the class module. You can see this by selecting Class from the Object list in the code editor, then selecting the Event list.
Option Explicit
Private rs As ADODB.Recordset
This declares an object variable for the ADO Recordset object.
Private Sub Class_Initialize()
Dim strPath As String, strName As String
Dim i As Integer
' Create an instance of the Recordset.
Set rs = New ADODB.Recordset
' Set the properties of the Recordset.
With rs
.Fields.Append "DirID", adInteger
.Fields.Append "Directory", adBSTR, 255
.CursorType = adOpenStatic
.LockType = adLockOptimistic
.Open
End With
' Loop through the directories and populate
' the Recordset.
strPath = "C:\"
strName = Dir(strPath, vbDirectory)
i = 0
Do While strName <> ""
If strName <> "." And strName <> ".." Then
If (GetAttr(strPath & strName) And _
vbDirectory) = vbDirectory Then
i = i + 1
With rs
.AddNew
.Fields.Item("DirID") = i
.Fields.Item("Directory") = strName
.Update
End With
End If
End If
strName = Dir
Loop
' Return to the first record.
rs.MoveFirst
End Sub
In this example we’re creating a ADO Recordset object on the fly and populating it with a list of directories. Alternatively, you could use an existing recordset by assigning to the Connect property of the ADO Recordset in the Initialize event.
Private Sub Class_GetDataMember(DataMember As String, Data As Object)
' Assign the Recordset to the Data object.
Set Data = rs
End Sub
The GetDataMember procedure sets the source of the data for the class. Your data source class can provide multiple data sources by adding a Select Case statement to the GetDataMember procedure and passing in a source name in the DataMember argument.
Public Sub Cycle()
' Cycle through the Recordset.
rs.MoveNext
If rs.EOF = True Then
rs.MoveFirst
End If
End Sub
In order to move through the recordset, we need to expose the navigation methods for our class. For simplicity, this example can only loop forward through the recordset. To make the class more useful, you might want to expose methods such as MoveFirst, MoveNext, Add, and Delete.
Now that the source class is defined, we can do something useful with it. In this example we’ll bind it to a TextBox control so that we can see its output; we’ll also use a CommandButton to execute our Cycle method.
Property | Setting |
Name | txtConsumer |
Text | (blank) |
Property | Setting |
Name | cmdCycle |
Caption | Cycle |
The DataBinding object provided by the Data Binding Collection is the "glue" that binds a data source to a data consumer.
Option Explicit
Private objSource As MySource
Private objBindingCollection As BindingCollection
We need to declare our source class (MySource) and the BindingCollection object using early binding.
Private Sub Form_Load()
Set objSource = New MySource
Set objBindingCollection = New BindingCollection
' Assign the source class to the Binding
' Collection’s DataSource property.
Set objBindingCollection.DataSource = objSource
' Add a binding.
ObjBindingCollection.Add txtConsumer, "Text", "Directory"
In the Load event we create instances of the source class and the BindingCollection object, then we assign the source object to the DataSource property of the BindingCollection. Finally, we add a binding by specifying the name of the consumer (txtConsumer), the Property of the consumer to be bound (the Text property), and the Field property of the source object that we are binding to (Directory).
Private cmdCycle_Click()
' Call the Cycle method of the data source.
ObjSource.Cycle
End Sub
This will execute the Cycle method of our source class.
As you click the Cycle button, directory names from the recordset created in our source class will appear in the TextBox. Congratulations — you’ve just bound a control to a data source class without using a Data control!
Save the source class as "MySource.cls".
Save the form as "Dataform.frm".
Save the project as "Dataware.vbp".
These files will be used later in "Creating a Data Consumer."
In the next section "Creating a Data Consumer," we’ll look at the process of creating a data-aware class that acts a consumer of data.