1

我在 Access 2010 db 中有一个表,其列名与 excel 表中的列名相同。在将数据从宏启用的 excel 2010 表中泵入其中之前,我必须删除 Access 表数据内容。现在我正在尝试查看/测试是否可以将我的 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

结束子

我添加了 Tools--> References 并选择了 Microsoft ActiveX Data Objects 6.0 Object Library。

我收到运行时错误'-2147467259(80004005)':无法识别的数据库格式'C:\Users\shress2\Documents\TSS_Certification\TSS_Certification.accdb

有什么理由吗?我该如何解决?谢谢。

4

1 回答 1

3

您正在连接到 .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;"

于 2012-05-16T16:13:25.600 回答