Visual Basic Concepts

Visual Basic Data Sources

See Also

As the term implies, a data source is a readily accessible object that provides data to any data consumer (any class or control that can be bound to a source of external data). In past versions of Visual Basic, data sources included the intrinsic Data control and the RemoteData Control. Visual Basic 6.0 introduces several new data sources that enable you to create rich applications to view and edit data. For example, the Data Environment designer allows you to create hierarchical recordsets, or data from several related tables in a tree-view format. In addition to new data sources, you can create your own data source by setting the class module's DataSourceBehavior property to vbDataSource.

New Data Sources

Visual Basic's new richer set of data sources includes:

Creating Data Sources Using Data-Aware Classes and User Controls

Visual Basic enables you to create your own data sources. Using new features such as the DataBindingBehavior and DataSourceBehavior properties of the class module, you can encapsulate the methods, properties, and events necessary to create a data source or data consumer that accesses data from any kind of database. For details on creating data sources and data consumers using a data-aware class, see Creating Data-Aware Classes.

In addition to creating data-aware classes, you can also create your own data-aware user controls. Such a control could resemble the new ADO Data Control and be customized to your needs. For a step-by-step example of creating a data-aware user control, see Creating the MyDataControl Project.

The Data Environment Designer

The Data Environment is a new feature that allows you to create hierarchical cursors. A hierarchical cursor is a unique structure of parent and child recordsets. In general, a hierarchical cursor mirrors the structure of related tables in a relational database. For example, the Northwind database has a table named "Products" that contains a field named "SupplierID." That field contains unique IDs from the related table named "Suppliers." Using the Data Environment designer, you can create a hierarchical cursor that can be displayed in a control such as the Hierarchical FlexGrid control.

For details about hierarchical cursors, see Hierarchical Cursors and Data Shaping. For details about the DataEnvironment, see About the Data Environment Designer.

Creatable ADO Recordsets

Using the ActiveX Data Object Recordset (ADOR) library, you can create ADO recordsets in memory, as shown below:

Private rs As New ADODB.Recordset ' variable for recordset

Private Sub CreateRecordset()
With rs
   .Fields.Append "ID", adInteger
   .Fields.Append "Item", adVarChar, 255
   .CursorType = adOpenStatic
   .LockType = adLockOptimistic
   .Open ' No connection object needed.
End With
Dim i As Integer

   For i = 1 To 100
      rs.AddNew
      rs!ID= i
      rs!Item = "thing " & i
      rs.Update
   Next i
   rs.MoveFirst
End Sub

Once such a recordset is filled with data, set the DataSource property of a data consumer to the recordset, as shown below:

' myControl is a data-bound user control and rsTempData is a temporary
' recordset created using ADODB, and filled from some data store.
Set myControl.DataSource = rs 

For an example of creating ADO recordsets in code, see Creating a Data Source. Another example of creating a recordset and using it as a data source for the DataGrid control can be found in Using the DataGrid Control with a Class Module.