Bind the DataGrid from database in VB 6

The following code snippet can be used for showing the data from the dataset into Datagrid control when you are working in vb 6.
We are using Datasource property of the DataGrid control for displaying the data. For it we get the Data from the database into ADODB recordset object. the program require to add the reference of the Microsoft Activex Data objects library so that you can use the ADODB.
See the example:

  1. Private m_strCnn  As String
  2. Private m_adoConn As ADODB.Connection
  3. Private Sub SetData()
  4. With m_adoConn
  5.         If .State = 0 Then
  6.         .Open m_strCnn
  7.         End If
  8.     End With
  9.     Dim gridrs As New ADODB.Recordset
  10.     With gridrs
  11.         .ActiveConnection = m_adoConn
  12.         .CursorLocation = adUseClient
  13.         .CursorType = adOpenKeyset
  14.         .LockType = adLockReadOnly
  15.         .Source = "Select * from yourtable"
  16.         .Open
  17.     End With
  18.     Set DataFrid1.DataSource = gridrs
  19.     Set gridrs = Nothing
  20. End Sub