用VBA代码链接ACCESS数据库
Dim cnn As New ADODB.Connection '请先添加ADO引用,注意引用Excel控件MS ActiveX object 2.6和DAO 3.6
Dim rs As New ADODB.Recordset
Dim SQL$, i&, j&
[a1].CurrentRegion.Offset(1).ClearContents
cnn.Open "Provider=Microsoft.Jet.Oledb.4.0;data Source=" & ThisWorkbook.Path & "\TiKu.mdb;Jet OLEDB:Database Password=" & ""
SQL = "Select * from 小题"
rs.Open SQL, cnn, adOpenKeyset, adLockOptimistic
Dim Kk As Integer
For Kk = 1 To rs.Fields.Count
Cells(1, Kk) = rs(Kk - 1).Name
Next Kk
Do While Not rs.EOF
For i = 2 To rs.RecordCount + 1
For j = 0 To rs.Fields.Count - 1 'j为'列数'
Cells(i, j + 1) = rs(rs(j).Name) 'RecordSet(i) 传回RecordSet中的第i个字段数据
Next j
rs.MoveNext
Next i
Loop
MsgBox "共有" & rs.RecordCount & "条记录"
rs.Close
cnn.Close
Set rs = Nothing
Set cnn = Nothing
文章评论(0)