0
我正试图在VBA中编写/采用宏来将SQL查询复制到Excel中。 我有它连接和执行。当我在SQL手动运行它,这是SQL的错误代码:VBA宏将SQL错误代码复制到Excel中
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table '#MSP_History3', because it does not exist or you do not have permission.
(8661 row(s) affected)
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table '#MSP1', because it does not exist or you do not have permission.
(8661 row(s) affected)
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table '#MSP_PBP', because it does not exist or you do not have permission.
(8661 row(s) affected)
Msg 3701, Level 11, State 5, Line 1
Cannot drop the table 'dbo.MSP_History', because it does not exist or you do not have permission.
Msg 262, Level 14, State 1, Line 1
CREATE TABLE permission denied in database 'master'.
看起来像它不是一个伟大的查询,但我感兴趣是受影响的行数。
下面是有人比我更熟练已经用于不同的查询工作粘贴在A1表结果VBA代码的后面部分:
'Open the connection.
cn.Open strConn
'
'Set and Execute SQL Command
Set cmd1.ActiveConnection = cn
cmd1.CommandText = SQLquery
cmd1.CommandType = adCmdText
cmd1.Execute
'Open Recordset
Set rs1.ActiveConnection = cn
rs1.Open cmd1
'Paste Column Headers into Spreadsheet
For Col = 0 To rs1.Fields.Count - 1
Range("A1").Offset(0, Col).Value = rs1.Fields(Col).Name
Next
'Copy Data to Excel
ActiveSheet.Range("A2").CopyFromRecordset rs1
Cells.Select
Cells.EntireColumn.AutoFit
此代码为正确执行的SQL查询。有没有办法复制错误信息?
理想情况下,我想要在Excel中的单元格A1只是“8661”。
谢谢!
编辑:目前已在执行“cmd1.Execute”行VBA错误消息是:
Run-time error '-2147217900 (80040e14)': Automation error
谢谢,太好了! 错误代码仅显示SQL错误代码的第二行:“-2147217865无法删除表'#MSP_History3',因为它不存在或者您没有权限。” 有关如何获取其余部分或下一行的任何想法?受影响的行数是我正在寻找的。 –
欢迎您!很高兴它对你有用!您似乎没有足够的权限执行该查询...您是否尝试使用其他用户登录? –
我不知道...我不能帮你更多...我发现了这个代码一次,我认为它可能对你有用... –