我在Access 2010数据库中有一个表,其列名与excel表中的列名相同 . 我必须删除Access表数据内容,然后从我的宏中将excelbed excel 2010表格中的数据泵入其中 . 现在我试图查看/测试我是否可以将我的Excel数据泵入Access中的空表 . 一旦我开始工作,我就可以在转储excel数据之前“删除内容” .
这是我的excel vba宏代码:
Sub ADOFromExcelToAccess()
'exports data from the active worksheet to a table in an Access database
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Users\shress2\Documents\TSS_Certification\TSS_Certification.accdb;"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "t_certification_051512", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 2 ' the start row in the worksheet
Do While Len(Range("A" & r).Formula) > 0
' repeat until first empty cell in column A
With rs
.AddNew ' create a new record
' add values to each field in the record
.Fields("Role") = Range("A" & r).Value
.Fields("Geo Rank") = Range("B" & r).Value
.Fields("Geo") = Range("C" & r).Value
' add more fields if necessary...
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
结束子
我添加了工具 - >引用并选择了Microsoft ActiveX Data Objects 6.0对象库 .
我得到运行时错误'-2147467259(80004005)':无法识别的数据库格式'C:\ Users \ shress2 \ Documents \ TSS_Certification \ TSS_Certification.accdb
有什么原因吗?我该如何解决?谢谢 .
1 回答
您正在连接到.accdb数据库文件 . 这是Access 2007/2010格式 .
Microsoft.Jet.OLEDB.4.0
提供程序是为Access 2003时代的mdb文件构建的 .我认为你不能与该提供商连接(它无法识别文件格式) .
尝试更改要使用的连接字符串
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\shress2\Documents\TSS_Certification\TSS_Certification.accdb;"