运行VB,选择“工程_引用”命令,引用 Microsoft ActiveX Data Objects 2.5 Library .不要用DATA ENVIRMENT,直接添加DataReport,在上面布置好相应的控件,DataReport中添加代码: Private Sub DataReport_Initialize() Dim cn As ADODB.Connection Dim rs As ADODB.Recordset Dim Sql As String Dim x, y, z
'连接数据库(Access) Set cn = New ADODB.Connection cn.Open "Provider=Microsoft.Jet.OLEDB.4.0 ;" & _ "Data Source=" & App.Path & "\db1.mdb;" & _ "Persist Security Info=False" '给查询字段赋值 x = "id" y = "iName" z = "iCode"
'打开记录集 Set rs = New ADODB.Recordset Sql = "select " & x & "," & y & "," & z & " from tb where id='" & Trim(frmDialog.txtID.Text) & "'" rs.Open Sql, cn, adOpenKeyset, adLockOptimistic
'设置text控件属性 DataReport.Sections("Section1").Controls("text1").DataField = x DataReport.Sections("Section1").Controls("text2").DataField = y DataReport.Sections("Section1").Controls("text3").DataField = z Set DataReport.DataSource = rs End Sub
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim Sql As String
Dim x, y, z
'连接数据库(Access)
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0 ;" & _
"Data Source=" & App.Path & "\db1.mdb;" & _
"Persist Security Info=False"
'给查询字段赋值
x = "id"
y = "iName"
z = "iCode"
'打开记录集
Set rs = New ADODB.Recordset
Sql = "select " & x & "," & y & "," & z & " from tb where id='" & Trim(frmDialog.txtID.Text) & "'"
rs.Open Sql, cn, adOpenKeyset, adLockOptimistic
'设置text控件属性
DataReport.Sections("Section1").Controls("text1").DataField = x
DataReport.Sections("Section1").Controls("text2").DataField = y
DataReport.Sections("Section1").Controls("text3").DataField = z Set DataReport.DataSource = rs
End Sub