Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi folks,

I am trying to populate a datagrid from Access DB in VB6.
However, its showing no data.

My sample code is as -

Private Sub Form_Load()

Dim str As String
Dim cmd As String
Dim gql As String
Dim cn As ADODB.Connection
Dim grs As New ADODB.Recordset

cmd = "provider=Microsoft.jet.OLEDB.4.0;Data Source=D:\AccessDbs\booksdb.mdb"

Set cn = New ADODB.Connection
cn.Open cmd

gql = "select * from book"
grs.CursorLocation = adUseClient
grs.Open gql, cn, adOpenDynamic
Set MyDataGrid.DataSource = grs

grs.Close
Set grs = Nothing
cn.Close
Set cn = Nothing

End Sub


Any pointers.

TIA
Posted
Comments
DaveAuld 7-Jun-11 3:32am    
Any pointers you ask? Yeh, use decent variable names for starters!
1) Have you tried adding some debug statement after each operation to check the various states etc.
2) Have you the correct dbs filelocation/name; Have you the correct table name e.g. books and not book etc.
rajivpande86 7-Jun-11 4:47am    
yes DaveAuld. I had already debugged it and the names n locations of the database are correct.
Julio_uy 7-Jun-11 16:50pm    
The recordset/connection must remain open while working with the datagrid, you can close it on form unload.

Try moving the following lines to form_unload:
grs.Close
Set grs = Nothing
cn.Close
Set cn = Nothing

And move to General-Declarations
Dim cn As ADODB.Connection
Dim grs As New ADODB.Recordset

Excuse my bad english.

Further to Member 3996248, I'd recommend changing as follows:


VB
Option Explicit
' These are needed for the life of the form
Private cn As ADODB.Connection
Private grs As New ADODB.Recordset

Private Sub Form_Load()
    Dim str As String
    Dim cmd As String
    Dim gql As String

    cmd = "provider=Microsoft.jet.OLEDB.4.0;Data Source=D:\AccessDbs\booksdb.mdb"

    Set cn = New ADODB.Connection
    cn.Open cmd

    gql = "select * from book"
    grs.CursorLocation = adUseClient
    grs.Open gql, cn, adOpenDynamic
    Set MyDataGrid.DataSource = grs
End Sub

Private Sub Form_Unload()
    ' close open cursor and connection
    grs.Close
    Set grs = Nothing
    cn.Close
    Set cn = Nothing
End Sub
 
Share this answer
 
'Another solution

Option Explicit
' These are needed for the life of the form
Private cn As ADODB.Connection
Private rs As New ADODB.Recordset
'------------------------------------
Private Sub Form_Load()
On Error GoTo trp
Dim cmd As String
Dim Sql As String
Set DataGrid1.DataSource = Nothing ' this line is very essential..

'-------------Connection string "
cmd = "provider=Microsoft.jet.OLEDB.4.0;taSource=D:\AccessDbsbooksdb.mdb"
set cn = New ADODB.Connection
cn.Open cmd
Set cn = New ADODB.Connection
'---------------------end-----------
'----------Rs -- starts
Sql = "Select * from book"
rs.Open Sql, Conn, adOpenDynamic, adLockPessimistic
Set DataGrid1.DataSource = rs

DataGrid1.Columns(0).Locked = True ' if you want lock thiscell
DataGrid1.Columns(0).Width = 2250 'if you want the cell width size
'----------------- error traps
Exit Sub
trp:
MsgBox "PleaseEnterCorrectValues"vbExclamation,"KaraikudiSelvaraj@gmail.com"

End Sub
 
Share this answer
 
I don't know if you already have a solution for your
DBGrid problem,


I was having a similar situation but if you right click on the DBGrid
and then click on Retrieve Fields, should connect all the fields of your
table with the dbGrid object.

I hope this helps you.
 
Share this answer
 
v2
Comments
Member 12558429 23-Sep-16 8:06am    
My Name is Ramesh(RG) and i am working on a small project and my problem is : i want to display the records in the datagrid from MS Access97, Based on Datacombo box value so how it can be done .......... please help in this

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900