简体   繁体   English

Excel VBA导出到Access:ADO错误

[英]Excel VBA Export to Access: ADO Error

I am trying to export a worksheet to Access where it will be paste appended into an existing table. 我正在尝试将工作表导出到Access,将其粘贴到现有表中。 I've referenced this post , this other post , and this other post . 我已经引用了这篇文章这篇文章 和另一篇文章 I'm getting an error that was not addressed in any of them that I think might be related to VBA libraries but could be something completely different. 我收到的错误在我认为可能与VBA库有关的任何一个中都没有解决,但可能是完全不同的错误。 Here is my code: 这是我的代码:

Sub ExcelToAccessAdo()

Dim cn As ADODB.Connection, rs As ADODB.Recordset, row As Long
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
        "Data Source=filepath\AccessDB.accdb;" 'this is a different filepath from the real one.

' open a recordset
Set rs = New ADODB.Recordset
rs.Open "Table Export", cn, adOpenKeyset, adLockOptimistic, adCmdTable

row = 2    ' the start row in the worksheet
Do While Not IsEmpty(Worksheets("Table Export").Range("A" & row))

    With rs
        .AddNew    ' create a new record
        .Fields("REPTNO") = Worksheets("Table Export").Range("A" & row).Value
        .Update
    End With
    row = row + 1
Loop

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

End Sub

The worksheet name and Access table name are the same. 工作表名称和Access表名称相同。 Also the .Fields("REPTNO") line is straight from the other person's post so I don't know if I'll have to change it or not. 另外.Fields("REPTNO")行直接来自其他人的帖子,所以我不知道我是否必须改变它。

The error is Run-time error '-2147217900 (80040e14)': Syntax error in FROM clause. 错误是Run-time error '-2147217900 (80040e14)': Syntax error in FROM clause.

on the line rs.Open "Table Export", cn, adOpenKeyset, adLockOptimistic, adCmdTable . rs.Open "Table Export", cn, adOpenKeyset, adLockOptimistic, adCmdTable

This is a strange error since it seems like an error I'd get when running SQL, but I'm assuming there's some background SQL happening with an INSERT INTO clause. 这是一个奇怪的错误,因为它似乎是我在运行SQL时遇到的错误,但我假设有一些背景SQL发生了INSERT INTO子句。 Which maybe this is some type of file path issue with Excel? 这可能是Excel的某种类型的文件路径问题? Any help? 有帮助吗?

Here are my library References: 这是我的图书馆参考文献:

  • Microsoft Excel 14.0 Object Library Microsoft Excel 14.0对象库
  • OLE Automation OLE自动化
  • Microsoft Office 14.0 Object Library Microsoft Office 14.0对象库
  • Microsoft Forms 2.0 Object Library Microsoft Forms 2.0对象库
  • Microsoft ActiveX Data Objects 6.1 Library Microsoft ActiveX数据对象6.1库
  • Microsoft ActiveX Data Objects Recordset 6.0 Library Microsoft ActiveX数据对象Recordset 6.0库

感谢@JNevill指出我只需要使用方括号:

rs.Open "[Table Export]", cn, adOpenKeyset, adLockOptimistic, adCmdTable

Try to stay away from spaces in any object name (Tables, Field_Names, Forms, Reports, etc.). 尽量远离任何对象名称中的空格(Tables,Field_Names,Forms,Reports等)。 You can do this, but as you can tell, it's going to cause more time and unnecessary frustration to normalize things. 你可以做到这一点,但正如你所知道的那样,它会导致更多的时间和不必要的挫折感来规范事物。 Also, don't use reserved words in your code. 另外,请勿在代码中使用保留字。

https://support.microsoft.com/en-us/kb/286335 https://support.microsoft.com/en-us/kb/286335

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM