Private Sub DataReport_Initialize()
Dim str As String
Dim cnnReport As New ADODB.Connection
Dim rstReport As New ADODB.Recordset
'连接数据库,假设为Access
Set cnnReport = New ADODB.Connection
With cnnReport
.Provider = "MSDataShape.1" '一定要加这句
.Open "Data Source=C:\dbName.mdb;"& _
"Data Provider=Microsoft.Jet.OLEDB.4.0"
End With
str = "SELECT * FROM `TableName` "& _
"Where where ID='" & Form1.txtID & "'"
Set rstAgmRpt = New ADODB.Recordset
With rstAgmRpt
Set .ActiveConnection = cnnReport
.CursorLocation = adUseClient
.CursorType = adOpenStatic
.LockType = adLockReadOnly
.Source = str
.Open
End With
Set Me.DataSource = rstAgmRpt
End Sub
3.在窗体Form1上放一个TextBox,用来输入ID,一个按钮,代码:
Private Sub Command1_Click()
DataReport1.Show
End Sub