Sql ADOB VBA将数据插入表时发生自动错误

Sql ADOB VBA将数据插入表时发生自动错误,sql,vba,excel-2010,ado,Sql,Vba,Excel 2010,Ado,我试图在名为DataRange2$的excel表中插入一行。来自同一工作簿。我得到一个(运行时自动化错误-214721793)。我的代码在查询Excel工作簿时起作用,例如从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

我试图在名为DataRange2$的excel表中插入一行。来自同一工作簿。我得到一个(运行时自动化错误-214721793)。我的代码在查询Excel工作簿时起作用,例如从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
代码:

工作表标题如下所示

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

正在尝试两个函数:(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

Set-mrs=CreateObject(“ADODB.Recordset”)
为什么这样做?为什么不直接设置mrs=New ADODB.RecordSet来实例化它呢?当您逐步检查代码时,它在哪里失败?我认为问题不在于你的VBA。您可能会发现它与表单和表单控件有关。至少,我遇到过这样的错误。错误发生在Open SQL夫人,DataConConnection。这适用于select之类的查询,但不适用于insert,我认为这很奇怪。另外,我刚刚声明了createObject,因为我在网上看到的示例使用了它。老实说,我从来没有使用VBA使用ADODB读写excel文件。我不知道我在哪里能看到它的用处。也就是说,检查权限。我正在为我的客户制作一个工具,以便将数据输入到外部工作簿中。我想对工作手册使用sql,这样我就不必手动编写所有sql了我还不在家,但如果这解决了我的问题,你就是天使!
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