EXCEL通过DAO调用ACCESS的数据

lgzhorse 发表于 2005-8-22 13:49:39

Sub ADO_Access()'以Access为数据源使用ADO语句
Application.ScreenUpdating = False
Set cnnAccess = CreateObject("Adodb.Connection")'使用这种定义方法可以不必引用ActiveX控件,将文件Copy到不同机器上时比较方便(ActiveX版本不同问题比较烦人)
Set rstAnswers = CreateObject("Adodb.Recordset")'
Stpath = ThisWorkbook.Path & Application.PathSeparator & "Data.mdb" '设定路径
cnnAccess.Provider = "Microsoft.Jet.OLEDB.4.0"
cnnAccess.Open "Data Source =" & Stpath & ";Jet OLEDB:Database Password=" & ""
strSQL = "Select * From MyData"'Sql语句,最好买一本书来学习,本句意思是选择Access的myData表内的全部数据
rstAnswers.Open strSQL, cnnAccess, 1, 3
Sheet1.Range("A1").CopyFromRecordset rstAnswers'将数据粘贴到Excel的Sheet1表内(从A1格开始)

rstAnswers.Close
Set rstAnswers = Nothing
Set cnnAccess = Nothing
End Sub

发表评论:

    昵称:
    密码: (游客无须输入密码)
    主页:
    标题:
Powered by Oblog.