VB6 Datagrid没有被填充 [英] VB6 Datagrid not getting populated

查看:75
本文介绍了VB6 Datagrid没有被填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试从VB6中的Access DB填充一个数据网格.
但是,它没有显示数据.

我的示例代码为-

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



任何指针.

TIA



Any pointers.

TIA

推荐答案

进一步到成员3996248 ,我建议进行如下更改:


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


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


''另一种解决方案

显式选项
''这些是表格生命中所必需的
私有cn作为ADODB.Connection
私人rs作为新的ADODB.Recordset
''------------------------------------
私人子Form_Load()
出现错误时GoTo trp
昏暗的cmd作为字符串
Dim Sql As String
设置DataGrid1.DataSource = Nothing''这行是非常必要的..

''-------------连接字符串"
cmd ="provider = Microsoft.jet.OLEDB.4.0; taSource = D:\ AccessDbsbooksdb.mdb"
设置cn =新的ADODB.Connection
cn.Open cmd
设置cn = New ADODB.Connection
''---------------------结束-----------
''---------- Rs-开始
Sql =从书中选择*"
rs.Open Sql,Conn,adOpenDynamic,adLockPessimistic
设置DataGrid1.DataSource = rs

如果要锁定此单元格,请单击DataGrid1.Columns(0).Locked = True"
DataGrid1.Columns(0).Width = 2250''如果您想要像元宽度大小
''-----------------错误陷阱
退出子
trp:
MsgBox"PleaseEnterCorrectValues" vbExclamation,"KaraikudiSelvaraj@gmail.com"

结束Sub
''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


我不知道您是否已经为您的解决方案
DBGrid问题,


我遇到过类似的情况,但是如果您右键单击DBGrid
然后单击检索字段",应连接您的
的所有字段 dbGrid对象的表.

希望对您有帮助.
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.


这篇关于VB6 Datagrid没有被填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆