Option Explicit
Private s_cnnDB As ADODB.Connection
Private s_rsData As ADODB.Recordset
Private Const s_strConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Program Files\Microsoft Visual Studio\VB98\BIBLIO.MDB;Persist Security Info=False"
Private Sub Form_Load()
Dim objField As ADODB.Field
Dim intIndex As Integer
Dim strSQL As String
Set s_cnnDB = New ADODB.Connection
'Connect to the database. You might have to change the connect string to point to the BIBLO.MDB file.
s_cnnDB.ConnectionString = s_strConnectString
s_cnnDB.Open
'Instantiate the recordset.
Set s_rsData = New ADODB.Recordset
'The query has an order by clause in it. If you want a different sort order or if
'you want the fields in a different order, move the fields around or sort by a differnt
'column.
strSQL = "SELECT * FROM [All Titles]"
s_rsData.PageSize = 100
'Open the Query in Access. This is not a table, but a query.
s_rsData.CursorLocation = adUseClient 'Must be a client side cursor to use this grid.
s_rsData.Open strSQL, s_cnnDB
'Add columns to the grid.
For Each objField In s_rsData.Fields
'The grid already has 2 columns defined.
If intIndex < 2 Then
DataGrid1.Columns(intIndex).Caption = objField.Name 'Col Name.
DataGrid1.Columns(intIndex).DataField = objField.Name 'rsFileld name to bind to.
Else
DataGrid1.Columns.Add(intIndex).Caption = objField.Name 'Col Name.
DataGrid1.Columns(intIndex).DataField = objField.Name 'rsFileld name to bind to.
End If
intIndex = intIndex + 1
Next objField
'Bind the control to the recordset.
Set DataGrid1.DataSource = s_rsData
'Change these to allow the user to do what you want them to do.
DataGrid1.AllowAddNew = False
DataGrid1.AllowDelete = False
DataGrid1.AllowUpdate = False
End Sub
Private Sub Form_Resize()
'Adjust the grid if the form is resized.
DataGrid1.Width = Me.ScaleWidth - 250
DataGrid1.Height = Me.ScaleHeight - 250
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Tidy up before we leave.
s_rsData.Close
Set s_rsData = Nothing
s_cnnDB.Close
Set s_cnnDB = Nothing
End Sub