关闭对象时不允许使用Excel VBA ADO Oracle Operation

时间:2014-12-22 19:15:43

标签: oracle excel-vba ado vba excel

我有一个Excel 2013 VBA宏,需要在Oracle 12c数据库上调用SQL过程。执行Oracle过程(它将结果写入表)但在Excel中我收到“Set rs = cmd.Execute”的错误:

Operation is not allowed when the object is closed

代码下方:

Dim v_userpw As String

Dim cnn As ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cmd As New ADODB.Command
Dim l_userpw, l_reqid, l_pwhash, l_sighash As New ADODB.Parameter
Dim objErr As ADODB.Error

v_userpw = Cells(7, 1).Value

On Error GoTo err_test
'Set cnn = CreateObject("ADODB.Connection")
Set cnn = New ADODB.Connection
cnn.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=devdb;User ID=db1;Password=db1;"
cnn.Open
Set cmd = New ADODB.Command
Set cmd.ActiveConnection = cnn
Set l_userpw = cmd.CreateParameter("l_userpw", adVarChar, adParamInput, 1024, v_userpw)
cmd.Parameters.Append l_userpw
Set l_reqid = cmd.CreateParameter("l_reqid", adVarChar, adParamOutput, 1024)
cmd.Parameters.Append l_reqid
Set l_pwhash = cmd.CreateParameter("l_pwhash", adVarChar, adParamOutput, 1024)
cmd.Parameters.Append l_pwhash
Set l_sighash = cmd.CreateParameter("l_sighash", adVarChar, adParamOutput, 1024)
cmd.Parameters.Append l_sighash

'cmd.Properties("PLSQLRSet") = True
cmd.CommandText = "{CALL db1.genheader(?, ?, ?, ?)}"
Set rs = cmd.Execute
'cmd.Properties("PLSQLRSet") = False

Cells(8, 1) = rs.Fields("reqid").Value
Cells(9, 1) = rs.Fields("pwhash").Value
Cells(10, 1) = rs.Fields("sighash").Value

cnn.Close

err_test:
MsgBox Error$
For Each objErr In cnn.Errors
    MsgBox objErr.Description
Next
cnn.Errors.Clear
Resume Next

Oracle程序如下所示:

create or replace procedure genheader (
l_userpw in varchar2,
l_reqid out varchar2,
--l_pwhash out raw,
--l_sighash out raw
l_vpwhash out varchar2,
l_vsighash out varchar2
)

有人可以帮帮我吗?我需要返回预定义单元格中的值。

谢谢,

2 个答案:

答案 0 :(得分:0)

尝试测试连接状态以确保在将连接分配给命令对象的ActiveConnection属性之前将其打开。这可能会导致行为不稳定。如果您不想在代码中执行此操作,则可以在设置的代码行之前分配断点,并检查本地窗口中的连接状态。您还需要指定Oracle存储过程的名称

cmd.Name = "genheader"

干杯, 克林顿

答案 1 :(得分:0)

该过程是否实际返回结果集?看起来它只是使用输出参数返回数据,因此您无法从记录集中获取结果,您可以在命令执行后从命令参数中获取它们。

cmd.Execute

Cells(8, 1) = cmd("l_reqid")
Cells(9, 1) = cmd("l_pwhash")
Cells(10, 1) = cmd("l_sighash")