将数据插入表时出现 ADODB VBA 自动化错误

Posted

技术标签:

【中文标题】将数据插入表时出现 ADODB VBA 自动化错误【英文标题】:ADOB VBA Automation error when inserting data into table 【发布时间】:2018-07-25 17:07:16 【问题描述】:

我正在尝试在名为 DataRange2$ 的 Excel 表中插入一行。来自同一个工作簿。我收到一个(运行时自动化错误 -214721793)。当我查询 Excel 工作簿(例如 select * from DataRange2$)时,我的代码有效。我想插入一行,但出现此错误。我正在使用 ADOB、VBA 和 excel 2010。

正在发送的 SQL 查询是:

SQL = "INSERT INTO [DataSheet2$] (Name, a, b, c, d, e) VALUES ('This is a name', 10, 20, 30, 40, 50)"
`Result` is the array which is populated with the returned rows 
runQuery SQL, result

代码:

Public Function runQuery(SQL As String, ByRef result() As String) As Integer
  Dim dataConection As New ADODB.connection
  Dim mrs As New ADODB.Recordset
  Dim DBPath As String
  Dim connectionString As String
  Dim size As Integer

  Set mrs = CreateObject("ADODB.Recordset")

  'Set cursor to start
  mrs.CursorLocation = adUseClient

  'Set database path
  DBPath = ThisWorkbook.FullName

  'You can provide the full path of your external file as shown below
  connectionString = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"

  'Open connection to database
  dataConection.Open connectionString

  'Open record set (query or table, connection)
  mrs.Open SQL, dataConection

  'Record set returned update data
  If mrs.EOF Then
      runQuery = 0
      Exit Function
  Else
      runQuery = mrs.RecordCount
  End If

  'Populate array with result
  ReDim result(size) As String
  Dim i As Integer

  For i = 0 To (size - 1)
      result(i) = mrs!Name
      mrs.MoveNext
  Next i

  'Close record set and connection
  mrs.Close
  dataConection.Close
End Function

工作表标题如下

Name | a | b | c | d | e | Status | Action

【问题讨论】:

Set mrs = CreateObject("ADODB.Recordset") 为什么要这样做?为什么不只是 Set mrs = New ADODB.RecordSet 来实例化它?当你单步执行你的代码时,它在哪里失败了?我不认为问题出在您的 VBA 中。您可能会发现它与表单和表单控件有关。至少,我遇到过这样的错误。 mrs.Open SQL,dataConection出现错误 这适用于像 select 这样的查询,但不适用于我认为很奇怪的 insert。另外,我刚刚声明了 createObject,因为我在网上看到的示例使用了它。 老实说,我从来没有使用过 VBA 通过 ADODB 读取或写入 excel 文件。不知道我在哪里可以看到一个很好的用途。话虽如此,请检查权限。 我正在为我的客户制作一个工具来将数据输入到外部工作簿中。我想对工作簿使用 sql,所以我不必手动编写所有 sql 代码 【参考方案1】:

尝试了两个函数 (1) 插入新行,以及 (2) 获取 x 行名称。这些需要两个单独的 ADODB.Recordset。以下是插入,但在通过 Get 时单步执行失败

Sub zz()
    Dim sSQL As String, sResult(3) As String
    sSQL = "INSERT INTO [DataSheet2$] (Name, a, b, c, d, e) 
            VALUES ('This is a name', 10, 20, 30, 40, 50)"
    'Result is the array which is populated with the returned rows
    runQuery sSQL, sResult
End Sub



Public Function runQuery(SQL As String, ByRef result() As String) As Integer
  Dim dataConection As New ADODB.Connection
  Dim mrs As New ADODB.Recordset
  Dim DBPath As String
  Dim connectionString As String
  Dim size As Integer

  Set mrs = CreateObject("ADODB.Recordset")

  'Set cursor to start
  mrs.CursorLocation = adUseClient

  'Set database path
  DBPath = ThisWorkbook.FullName

  'You can provide the full path of your external file as shown below
  connectionString = "Provider=MSDASQL.1;DSN=Excel Files;DBQ=" & DBPath & ";HDR=Yes';"

  'Open connection to database
  dataConection.Open connectionString

  'Open record set (query or table, connection)
  mrs.Open SQL, dataConection   '>> this is where the INSERT happens

  'Record set returned update data
  If mrs.EOF Then   ''>> rte 3704  Operation not allowed when object is closed
      runQuery = 0
      Exit Function
  Else
      runQuery = mrs.RecordCount
  End If

  'Populate array with result
  ReDim result(size) As String
  Dim i As Integer

  For i = 0 To (size - 1)
      result(i) = mrs!Name
      mrs.MoveNext
  Next i

  'Close record set and connection
  mrs.Close
  dataConection.Close
End Function

【讨论】:

我还不在家,但如果这解决了我的问题,你就是天使!

以上是关于将数据插入表时出现 ADODB VBA 自动化错误的主要内容,如果未能解决你的问题,请参考以下文章

Access 2007,VBA:将 BLOB 插入链接表时出现奇怪的错误

VBA 将整个 ADODB.Recordset 插入表中

将公式插入单元格 VBA Excel 时出现运行时错误 1004

在 MSAccess 中使用 VBA (ADODB) 将数据从远程 DB 附加到本地表,而不锁定远程 DB 中的记录

从 VBA 插入公式时出现不同的语言问题

错误 [08S01] 将数据插入到 netezza 中的外部表时出现通信链接故障