Private Sub GetData()
'声明数据库连接对象
Dim conn As New ADODB.Connection
'声明结果集对象
Dim res As New ADODB.Recordset
'创建SQL语句
Dim sql As String
sql = "select * from Chiller where Data between $A$5 and $A$4"
'打开数据库连接
conn.Open "provide=Microsoft.jet.OLEDB.4.0; data source = " & ThisWorkbook.Path & "E:\Access.mdb"
With res
'运行SQL并获取结果集
.Open sql, conn, adOpenKeyset, adLockOptimistic
If .RecordCount > 0 Then
For i = 1 To .RecordCount
'向Sheet1中写入查询结果
Sheet1.Cells(i + 3, 2) = .Fields(Date)
Sheet1.Cells(i + 3, 3) = .Fields(Loading_1)
.MoveNext
Next i
End If
End With
'关闭结果集和连接
res.Close
conn.Close
Set res = Nothing
Set conn = Nothing
End Sub
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.Path & "\TDSJ.MDB;" & _
"Jet OLEDB:Engine Type=4"
Set conn = New ADODB.Connection '创建一个连接和打开 Cnn 连接
Set rst = New ADODB.Recordset '创建一个记录集
Dim conn As New ADODB.Connection
Dim rst As New ADODB.Recordset