【问题标题】:Run time error Unrecognized database while exporting data from excel 2010 to Access table 2010 using excel vba使用 excel vba 将数据从 excel 2010 导出到 Access 表 2010 时运行时错误无法识别的数据库
【发布时间】:2012-05-24 03:39:54
【问题描述】:

我在 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

有什么原因吗?我该如何解决?谢谢。

【问题讨论】:

标签: vba excel excel-2010 ms-access-2010


【解决方案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;"

【讨论】:

  • 您好史蒂夫,感谢您的回复。当我将 Provider 更改为上述 Provider 时,我可能还必须将 Dim cn As ADODB.Connection, rs As ADODB.Recordset 更改为特定于 Excel/Access 2010 格式的内容。对吗?
  • 谢谢史蒂夫。我只需要更改提供者信息并将 .Fields("Role") = Range("A" & r).Value 更改为 ActiveSheet.Range("A" & r)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多