HOWTO: Bind Complex-Bound Controls at Run Time with Visual Basic 6.0

ID: Q189668


The information in this article applies to:
  • ActiveX Data Objects (ADO), versions 2.0, 2.1, 2.1 SP2
  • Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 6.0


SUMMARY

Visual Basic 6.0 offers a wide variety of ways to bind controls at run time. You can bind controls to a data source, such as the ADO Data Control, to the Data Environment, or directly to an ADO recordset object. You can also build classes and controls in Visual Basic that can act as data sources.

A simple-bound control reads one row or field at a time while a complex- bound control is one that looks at the entire recordset. For example, a textbox is a simple-bound control but a grid is a complex-bound control. The data bound listbox and combobox controls are complex-bound because they read the entire recordset to provide the list of available data.

Complex-bound controls require that the recordset be bound to support navigation and bookmarks. This functionality is required for the control to read the data in the recordset and know that it can later find that particular row. You may receive an error if you try to bind a complex-bound control to a data source whose data does not support this functionality.


MORE INFORMATION

Follow these steps to bind a DataGrid control to an ADO recordset.

  1. Create a new Standard EXE project. Form1 is created by default.


  2. Under Project, References, select the Microsoft ActiveX Data Objects Library.


  3. On the Project menu, click Components. On the Controls tab, select the check box next to the Microsoft DataGrid Control 6.0 (OLEDB) option. Then click OK to exit the Components window.


  4. Add a DataGrid to the form.


  5. Add the following code to the form. Be sure to change the value of the strConn variable so that it points to the location of the Northwind (NWind.MDB) database on your machine:


  6. 
       Dim cnNWind As New ADODB.Connection
       Dim rsOrders As New ADODB.Recordset
    
       Private Sub Form_Load()
           Dim strConn As String
    
           strConn = "Provider=Microsoft.Jet.OLEDB.3.51;" & _
                     "Data Source=D:\VS98\VB98\NWind.MDB;"
           'If using an Access 2000 mdb use "Microsoft.Jet.OLEDB.4.0"
           cnNWind.Open strConn
    
           rsOrders.Open "SELECT OrderID, EmployeeID, OrderDate FROM Orders", _
                         cnNWind, adOpenStatic, adLockOptimistic, adCmdText
           Set DataGrid1.DataSource = rsOrders
       End Sub
     
  7. Run the code and you will see data in the grid on your form.


  8. Modify the code by changing the adOpenStatic constant to adOpenForwardOnly.


  9. Run the code and you should see an error message that says "The rowset is not bookmarkable."


Additional query words: DataList DataCombo

Keywords : kbADO kbCtrl kbDatabase kbDataBinding kbVBp600 kbGrpVBDB kbGrpMDAC kbDSupport kbADO210sp2 kbMDAC210SP2
Version : WINDOWS:2.0,2.1,2.1 SP2,6.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: November 17, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.