1个回答
展开全部
1、“工程”-“引用”-选中“microsoft activex data objects 2.8 library”-“确定”
2、“工程”-“部件”-选中“microsoft datagrid control 6.0”-“确定”
3、窗体上添加datagrid控件datagrid1,设置datagrid,右键单击 datagrid,在“列”选项卡中 输入这个datagrid相应的列对应的数据库字段,如column 0 标题 输入“编号”
,数据字段 输入“Test_No”(这个例子连接的数据库名字为databasename.mdb,datagrid显示的是表test下的Test_No字段)
Public cnn As ADODB.Connection
Public Sub Setdb()
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
cnn.ConnectionString = "Data Source=" & App.Path & "\data\systemParams.mdb;Jet OLEDB:Database Password="
cnn.CursorLocation = adUseClient
cnn.Open
End Sub
'
4、连接数据库
Public cnn As ADODB.Connection
Public Sub Setdb()
On Error Resume Next
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
cnn.ConnectionString = "Data Source=" & App.Path & "\data\databasename.mdb;Jet OLEDB:Database Password="
cnn.CursorLocation = adUseClient
cnn.Open
End Sub
5、在添加datagrid的窗体的formload 事件代码如下:
Private Sub Form_Load()
On Error Resume Next
Dim rst As Recordset
Dim Strsql As String
Setdb '连接数据库
Set DataGrid1.DataSource = Nothing
Strsql = "select * from test order by Test_No"
Set rst = cnn.Execute(Strsql)
If Err Then
MsgBox "读取数据失败"
Exit Sub
End If
If rst.EOF Then
Set DataGrid1.DataSource = Nothing
Else
Set DataGrid1.DataSource = rst
End If
End Sub
6、运行程序,则打开窗体,DataGrid就显示出test表中的test_no字段里的值
2、“工程”-“部件”-选中“microsoft datagrid control 6.0”-“确定”
3、窗体上添加datagrid控件datagrid1,设置datagrid,右键单击 datagrid,在“列”选项卡中 输入这个datagrid相应的列对应的数据库字段,如column 0 标题 输入“编号”
,数据字段 输入“Test_No”(这个例子连接的数据库名字为databasename.mdb,datagrid显示的是表test下的Test_No字段)
Public cnn As ADODB.Connection
Public Sub Setdb()
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
cnn.ConnectionString = "Data Source=" & App.Path & "\data\systemParams.mdb;Jet OLEDB:Database Password="
cnn.CursorLocation = adUseClient
cnn.Open
End Sub
'
4、连接数据库
Public cnn As ADODB.Connection
Public Sub Setdb()
On Error Resume Next
Set cnn = New ADODB.Connection
cnn.Provider = "Microsoft.Jet.OLEDB.4.0"
cnn.ConnectionString = "Data Source=" & App.Path & "\data\databasename.mdb;Jet OLEDB:Database Password="
cnn.CursorLocation = adUseClient
cnn.Open
End Sub
5、在添加datagrid的窗体的formload 事件代码如下:
Private Sub Form_Load()
On Error Resume Next
Dim rst As Recordset
Dim Strsql As String
Setdb '连接数据库
Set DataGrid1.DataSource = Nothing
Strsql = "select * from test order by Test_No"
Set rst = cnn.Execute(Strsql)
If Err Then
MsgBox "读取数据失败"
Exit Sub
End If
If rst.EOF Then
Set DataGrid1.DataSource = Nothing
Else
Set DataGrid1.DataSource = rst
End If
End Sub
6、运行程序,则打开窗体,DataGrid就显示出test表中的test_no字段里的值
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询