ADOB VBA 将数据插入 table 时出现自动化错误
ADOB VBA Automation error when inserting data into table
我正在尝试将一行插入到名为 DataRange2$ 的 excel table 中。来自同一个工作簿。我收到一个(运行 时间自动化错误 -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
工作表headers如下
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
我正在尝试将一行插入到名为 DataRange2$ 的 excel table 中。来自同一个工作簿。我收到一个(运行 时间自动化错误 -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
工作表headers如下
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