0

I am trying to execute a SQL Server stored procedure from Excel VBA. The procedure returns rows into a result set object. However, while running the code, it throws an error:

3704 Operation is not allowed when the object is closed

Note:
There is no problem with the database connection because Select query running on the same connection object are working fine.

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rst As New ADODB.Recordset

Set cn = New ADODB.Connection
Set cmd = New ADODB.Command
ThisWorkbook.initialize
cn.Provider = "sqloledb"
cn.Properties("Data Source").Value = ThisWorkbook.server
cn.Properties("Initial Catalog").Value = ThisWorkbook.db
cn.Properties("User ID").Value = "xxxxx"
cn.Properties("Password").Value = "xxxxx"
cn.Open

Set cmd = New ADODB.Command
cmd.CommandText = "Generate_KPI_Process_Quality_Check_RunTime"
cmd.CommandType = adCmdStoredProc
cmd.ActiveConnection = cn

Set prm = cmd.CreateParameter("@currentMonth", adChar, adParamInput, 255, cmb_month.Value)
cmd.Parameters.Append prm

Set prm = cmd.CreateParameter("@center", adChar, adParamInput, 255, cmb_center.Value)
cmd.Parameters.Append prm

rst.CursorType = adOpenStatic
rst.CursorLocation = adUseClient
rst.CursorLocation = adUseServer
rst.LockType = adLockOptimistic

rst.Open cmd

If (rst.BOF And rst.EOF) Then
'Some Code
End If
4
  • At what line does it throw this error? Commented Aug 28, 2013 at 12:46
  • Hi @jerussell, It throws an error at the If (rst.BOF And rst.EOF) Then... statement at the end of the Code segment. Commented Aug 28, 2013 at 12:48
  • @Abhishek Panda Did any of these answers fix your problem? If so, upvote and/or mark as answered. Commented Aug 28, 2013 at 13:14
  • I am positive that you need to use the set keyword though or delete the first statement and use the solution proposed by @mehow Commented Aug 28, 2013 at 14:32

4 Answers 4

3

Put

SET NOCOUNT ON

in the stored procedure -- this will prevent output text generation like "1 record(s) updated".

Sign up to request clarification or add additional context in comments.

3 Comments

welcome to stackoverflow! this answer is a little terse, can you give a little explanation to how this solves the problem?
Corley, looks like stored procedure output interfering with result recordset so OLEDB provider can't receive the recordset. SET NOCOUNT ON prevents output text generation on INSERTs/UPDATEs and so on.
Another question asks about this same error (in VB6) and SET NOCOUNT was the suggested answer.
1

You have to provide more parameters for the Open method of Recordset Object

try rst.Open cmd, cn

4 Comments

The connection object is an optional argument. It might fix the problem caused by the incorrect assignment "cmd.ActiveConnection = cn" though
Indeed, it would work. However, if he decides to adopt your solution, he would better cleanup his code by deleting the offending statement that would potentially become a source of confusion in the future.
@mehow - I tried your solution and I get this: Run-time error '3707': Cannot change the ActiveConnection property of a Recordset object which has a Command object at its source
@AbhishekPanda have you tried command.execute method?
1

Use the Set keyword to assign the object:

Set cmd.ActiveConnection = cn

otherwise, the default property of the Connection object (which happen to be the connection string) will be assigned in lieu of the Connection object itself.

2 Comments

Thanks for the help. However, that didn't work. It's still the same old 3704 error.
I am positive that this is a problem and that you have to use set or delete the whole statement and use @mehow solution.
0

Just put another recordset that will contain resultsets

Dim rst1 As New ADODB.Recordset
SET rst1=rst.NextRecordset 'this will return the first resultset

If rst1.BOF or rst1.EOF Then...
    'some code
End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.