Visual Basic Reference
The example uses a class module as a data source. When code to set the DataSource and DataMember properties of two Binding objects executes, the class module's Initialize event occurs; two ADO recordsets are created in that event, and the names of the recordsets are added to the DataMembers collection. The GetDataMember event and its arguments are used to return data to the data consumer.
To try the example, on the Project menu, click References, and set a reference to Microsoft Data Binding Collection and Microsoft ActiveX Data Objects. On the Project menu, click Add Class Module. Change the name of the class to MyDataClass, and set the DataSourceBehavior property to vbDataSource. Then draw two TextBox controls on a form. Paste the code into the Form object's code module.
Option Explicit ' Declare the object variables, one for a Class module named MyDataClass, ' and two more for each BindingCollection object one for each ' recordset). Private clsData As New MyDataClass ' Class module Private bndColProducts As New BindingCollection ' Bindings Collection Private bndColSuppliers As New BindingCollection ' Bindings Collection Private Sub Form_Load() ' Set DataSource and DataMember properties for each Bindings ' collection object. With bndColProducts .DataMember = "Products" Set .DataSource = clsData .Add Text1, "Text", "ProductName" ' Bind to a TextBox. End With With bndColSuppliers .DataMember = "Suppliers" Set .DataSource = clsData .Add Text2, "Text", "CompanyName" ' Bind to a TextBox. End With ' Change the Caption of Command1 Command1.Caption = "MoveNext" End Sub Private Sub Command1_Click() clsData.MoveNext End Sub
Paste the code below into the MyDataClass module. The DataSourceBehavior property must be set to vbDataSource in order to see the GetDataMember event. Run the project.
Option Explicit ' Declare object variables for ADO Recordset and Connection objects. Private WithEvents rsProducts As ADODB.Recordset Private WithEvents rsSuppliers As ADODB.Recordset Private cnNwind As ADODB.Connection Private Sub Class_Initialize() ' Add strings to the DataMembers collection. With DataMembers .Add "Products" .Add "Suppliers" End With ' Set Recordset objects. Set rsProducts = New ADODB.Recordset Set rsSuppliers = New ADODB.Recordset Set cnNwind = New ADODB.Connection ' Set the Connection object parameters. With cnNwind ' The Nwind.mdb that comes with Visual Basic must be installed on ' the computer or the code will fail. Otherwise alter the path to ' find the file on the computer. .Provider = "Microsoft.Jet.OLEDB.3.51" .Open "C:\Program Files\DevStudio\VB\Nwind.mdb" End With ' Open the recordset objects. rsSuppliers.Open "SELECT * FROM Suppliers", cnNwind, _ adOpenStatic, adLockOptimistic rsProducts.Open "SELECT * FROM Products", cnNwind, _ adOpenStatic, adLockOptimistic End Sub ' The GetDataMember occurs when the DataSource property of a data ' consumer is set. In this case, the Bindings collection object is ' the consumer. Private Sub Class_GetDataMember(DataMember As String, Data As Object) Select Case DataMember Case "Products" Set Data = rsProducts Case "Suppliers" Set Data = rsSuppliers Case "" ' Provide a default record source when no Data Member is specified. Set Data = rsProducts End Select End Sub Public Function MoveNext() If rsProducts.EOF Then rsProducts.MoveFirst Else rsProducts.MoveNext End If End Function Private Sub rsProducts_MoveComplete(ByVal adReason As _ ADODB.EventReasonEnum, ByVal pError As ADODB.Error, adStatus As _ ADODB.EventStatusEnum, ByVal pRecordset As ADODB.Recordset) ' Keep the two recordsets in sync. The first textbox displays ' the supplier of the product. If the SupplierID for both ' recordsets are equivalent, no change needed. Otherwise, ' move to first record and test for SupplierID. This example ' is for demonstration only as the method is not the most ' efficient. If rsSuppliers("SupplierID").Value = _ pRecordset("SupplierID").Value Then Exit Sub rsSuppliers.MoveFirst Do While Not rsSuppliers.EOF If rsSuppliers("SupplierID").Value = _ pRecordset("SupplierID").Value Then Exit Sub Else rsSuppliers.MoveNext End If Loop End Sub