【问题标题】:Operation is not allowed when object is closed VBA对象关闭时不允许操作VBA
【发布时间】:2016-12-06 17:13:54
【问题描述】:

我的代码在到达以下代码的第 4 行时因上述错误而失败。我之前在我的程序中使用过同样的格式,它工作得很好。如果需要更多代码来解决错误,我很乐意发布。谢谢。

With rsTIP19
.ActiveConnection = cnTIP
.Open "exec usp_Service_Data_Query_QA"
ThisWorkbook.Sheets("Service_Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).CopyFromRecordset rsTIP19
.Close
End With

【问题讨论】:

  • 你的存储过程确定返回结果集吗?
  • @TimWilliams 是的,它在 sql 中完美运行

标签: sql vba excel stored-procedures


【解决方案1】:

执行的目的是什么?您应该测试.BOF.EOF

With rsTIP19

    .ActiveConnection = cnTIP
    .Open "usp_Service_Data_Query_QA"
    If Not .BOF or .EOF then
        ThisWorkbook.Sheets("Service_Data").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).CopyFromRecordset rsTIP19
    End If

    .Close

End With

【讨论】:

  • 我在尝试该代码时遇到与以前相同的错误。
  • 我不知道为什么你会在第 4 行得到那个错误。我认为代码应该在 .Open 上中断;如果连接已关闭。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多