Previous | Index | Next 

[PRB] Data classes might need manual adjustments

VB6 allows you to defined data source classes, i.e. classes or controls whose DataSourceBehavior property is set to 1-vbDataSource; such data source classes are typically used to bind controls to a data source. For example, you can use this feature to create a user control that behaves like the standard ADO Data control, possibly with a custom user interface and extended behavior.

VB Migration Partner fully supports VB6 data source classes and in most cases you don’t have to edit the original code in any way. There is a twist, though: if the class or ActiveX control supports rebinding to a different data source you need to include one or more statements in the converted VB.NET application.

For example, let’s say that the client application can assign a new value to the control’s DataSource property and then refresh the contents of all bound controls:

    ' in the client application
   MyDataControl1.DataSource = "Select * From Products Where ProductName Like 'A%'
   MyDataControl1.Refresh()

The Refresh method causes the binding infrastructure to fire the GetDataMember event, an event that all VB6 data source classes and controls must handle. When the event fires, the VB6 code is expected to recreate the data source – typically, an ADO Recordset – and pass it back to the COM binding infrastructure by means of the Data argument:

    Private Sub UserControl_GetDataMember(DataMember As String, Data As Object)
      Dim rs As New Recordset
      ' query public properties and then recreate and open the recordset
      ' ...
      ' pass the recordset back to the COM binding infrastructure
      Set Data = rs
   End Sub

As explained previously, if the data source class ever requeries the data source, you must insert a call to the VB6DataSourceWrapper.Create static method, immediately after the point where the Recordset is defined for the converted VB.NET code to work correctly:

    Private Sub UserControl_GetDataMember(DataMember As String, Data As Object)
      Dim rs As New Recordset
      ' query public properties and then recreate and open the recordset
      ' ...
      '## NOTE under .NET we must force re-binding
      '## InsertStatement VB6DataSourceWrapper.Create(rs, Me)
      ' pass the recordset back to the COM binding infrastructure
      Set Data = rs
   End Sub

 

Previous | Index | Next