Coding the myBoundClass Class Module

Visual Studio 6.0

The next thing we're going to do is start building our class module. New with VB 6.0 is the ability to set up a class as a data source. This is very cool because we can now encapsulate recordset management in a class.

Try It Out - Building the myBoundClass Class Module

1.  The first thing we need to do is let VB know that we want our class to be a data provider – that is, it's a resource that we use to get at some underlying data. We do this by setting the DataSourceBehavior property. VB is now smart enough to add a brand new sub to our class, Class_GetDataMember, as soon as we set the DataSourceBehavior property to vbDataSource. To set this up, bring up the property window for myBoundClass change the property to 1 as shown here:

2.  Click Project-References from the main VB menu and select the Microsoft ActiveX Data Objects 2.0 library and the ActiveX Data Objects Recordset 2.0 library. Also add the Microsoft Data Binding Collection type library:

Now our project will understand not only what an ADO connection and recordset is, but also how to deal with data binding collections.

3.  Let's add some code to our myBoundClass class module. In the General Declarations section, add the following:

Option ExplicitPrivate showInfo As BooleanPrivate WithEvents adoPublishers As ADODB.Recordset Private adoConnection As ADODB.Connection

What will this do for us? Well, first we declare a private variable, showInfo, that we will set with a property displayMoveComplete from the outside of our class. When the user checks or unchecks the check box on the form, we will set the variable showInfo to 'true' or 'false'. When it is true, we will show the status of the recordset after a move to the next record has been completed successfully.

Next, we declare an ADO recordset object variable called adoPublishers. Notice that we use the keyword WithEvents in the declaration. This is so that we can actually define ADO recordset events in our code. These events will then automatically fire because we have used the WithEvents keyword. Our code will include a MoveComplete ADO recordset event. Now, by simply using this keyword, this event will fire when the current record pointer of the recordset has moved to the next record.

Finally, we declare an object variable adoConnection. This will hold the connection to the database.

Now we're going to set up the GetDataMember sub that VB automatically placed in our class module. As mentioned, this was added for us when we told VB that the class is a data provider (by setting the class property). A data provider - such as our class - can have multiple sets of data that a data consumer (our form) can choose to bind to. Each set of data is called a data member. VB will set a DataMember to our recordset, adoPublishers. Add the highlighted code to the Class_GetDataMember subroutine:

Private Sub Class_GetDataMember(DataMember As String, data As Object)
Set data = adoPublishers
End Sub

When we use a class module as a data source, we code the GetDataMember to return an appropriate data member value. The event's DataMember argument contains the value of the DataMember property. This way our code can query the argument and determine which data member is being requested. In this case, we are using the recordset adoPublishers. This can be passed back to the caller using the data argument. We will see how this will be called next, in the class initialization event.

4.  Recall that when you open the code window for a class module, you must click the left hand drop down box to get to the class events – Initialize and Terminate. This code will execute exactly once in the life of the class. When the class is instantiated into an object in our form, this event fires. Add the following highlighted code to the Class_Initialize event now:

Private Sub Class_Initialize()

showInfo = FalseDataMembers.Add "Publishers"
Set adoPublishers = New ADODB.Recordset
Set adoConnection = New ADODB.Connection

With adoConnection
  .Provider = "Microsoft.Jet.OLEDB.3.51"
  .Open "C:\BegDb\Biblio.mdb"
End With

adoPublishers.Open "Select * FROM Publishers", _
                   adoConnection, _
                   adOpenStatic, _

End Sub

As we mentioned earlier, a data provider - such as our class - can have many sets of data that a data consumer (our form) could elect to bind to. These sets of data are called data members and each one is identified by a unique string. Here we are adding the string "Publishers". The DataMembers collection contains the names of all data members accessible to the data consumer. In our simple program, we only have a single entry in DataMembers, "Publishers":

DataMembers.Add "Publishers"

5.  We then go on to create new instances of our connection and recordset object variables. We are already familiar with how to do that. But here, we are using a slightly different method of setting up our connection; instead of setting a connection string that contains everything the connection needs to open itself, we chose to set the .Provider property separately. We then pass in the location and name of the database to the .Open method of the connection. This is just to illustrate how you can set up the connection in various ways. As long as it has enough information, the connection can open successfully:

With adoConnection  .Provider = "Microsoft.Jet.OLEDB.3.51"
  .Open "C:\BegDb\Biblio.mdb"
End With

Finally, we open the recordset to be managed by our class. For simplicity, we just select all records from the Publishers table:

adoPublishers.Open "Select * FROM Publishers", _                   adoConnection, _
                   adOpenStatic, _

So when the Initialization code is complete, we have a datamember added to the DataMembers collection, an open connection to the database, and an open recordset.

© 1998 by Wrox Press. All rights reserved.