How to connect datagrid from database using adodb connection. so that the records in the database will display in the datagrid. I have category here that i want to display to datagrid: ID, LName, FName, GLevel. I need a code for that. THANK YOU!

Dani AI

Generated

A minimal, reliable VB6 ADODB pattern to bind an ADODB.Recordset to an MSDataGrid and display the Category fields ID, LName, FName, GLevel. This complements 's pointer and answers 's "which database?" question by showing both Access and SQL Server connection-string options. Add a reference to “Microsoft ActiveX Data Objects 2.x Library” before running the code.

' At top of the form
Private cn As ADODB.Connection
Private rs As ADODB.Recordset

Private Sub Form_Load()
    Set cn = New ADODB.Connection
    ' Access .mdb:
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Path\MyDB.mdb;Persist Security Info=False;"
    ' .accdb (ACE) example:
    ' cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Path\MyDB.accdb;"
    ' SQL Server example:
    ' cn.ConnectionString = "Provider=SQLOLEDB;Data Source=SERVERNAME;Initial Catalog=DBNAME;Integrated Security=SSPI;"
    cn.Open

    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.Open "SELECT ID, LName, FName, GLevel FROM Category ORDER BY LName, FName", cn, adOpenStatic, adLockReadOnly

    Set DataGrid1.DataSource = rs
    DataGrid1.Refresh
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If Not rs Is Nothing Then If rs.State = adStateOpen Then rs.Close: Set rs = Nothing
    If Not cn Is Nothing Then If cn.State = adStateOpen Then cn.Close: Set cn = Nothing
End Sub

Quick troubleshooting and tips:

  • Ensure the ADODB reference is checked (Project -> References).
  • If the grid is blank: confirm the SQL returns rows (run query in Access/SSMS), check spelling of field/table names, and inspect rs.EOF/rs.RecordCount. adUseClient + adOpenStatic helps get a valid RecordCount.
  • For modern Windows, use the ACE provider for .accdb files; VB6 is 32-bit so provider bitness must match.
  • The MSDataGrid auto-generates columns; set captions or formats via DataGrid1.Columns after binding if needed.
  • As noted, the Data control is an alternative, but binding an ADODB.Recordset gives more control and works across providers.

Recommended Answers

All 3 Replies

How to connect datagrid from database using adodb connection. so that the records in the database will display in the datagrid. I have category here that i want to display to datagrid: ID, LName, FName, GLevel. I need a code for that. THANK YOU!

Connect to what database?

Simple code for Datagrid is:

Data1.visible=True

Using Data1 control is placed

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.