Excel VBA Error Run time error '3709':" The connection cannot be used to perform this operation. "

I am trying to connect to Oracle DB and execute SQL dynamically, but getting following error at run time:
Run-time error '3709':
The connection cannot be used to perform this operation.  It is either closed or invalid in this context."

PLease find the logic below

Sub Button1_Click()
Dim con As ADODB.Connection
Dim rs As ADODB.Recordset
Dim sqlstr As String
Dim strconn As String
Dim ref_no As String

    ' open connection
    Set con = New ADODB.Connection
    strconn = "Driver={Oracle in OraClient 11g_home2}; Data Source = ARGPRO; uid=TCS_SUKAST; pwd=tcs_sukant;"
    con.Open strconn
    'MsgBox (con.State)
    ref_no = Sheet1.Cells(2, 1).Value
        sqlstr = "SELECT  cm.case_num, to_char(cm.init_rept_date,'DD-MON-YYYY') init_rept, cp.product_name,  "
        sqlstr = sqlstr & "         (SELECT  type_desc FROM    argus_app.lm_ref_types lrt WHERE   lrt.ref_type_id = cr.ref_type_id ) ref_typ, "
        sqlstr = sqlstr & "         cr.ref_no ref_n, (SELECT  'Code: ' || lat.action_type ||'   Description: '||ca.description  "
        sqlstr = sqlstr & "         ||'   Date Open: '||ca.date_open ||'   Date Completed: '|| ca.date_done "
        sqlstr = sqlstr & "         FROM    case_actions ca, lm_action_item_type lat WHERE   cm.case_id = ca.case_id (+) AND     lat.action_type_id = ca.code "
        sqlstr = sqlstr & "         AND     upper(lat.action_type) = 'DELETION - BEFORE SUBMISSION' ) act_item "
        sqlstr = sqlstr & "      FROM    case_reference cr, case_master cm, v$case_primary_suspect_product cp"
        sqlstr = sqlstr & "         WHERE   cm.case_id = cr.case_id (+) AND     cm.case_id = cp.case_id AND cr.ref_no like '%" & ref_no & "%' "
   
    Sheet1.Cells(1, 2) = sqlstr
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    'rs.CursorType = adOpenStatic
    'rs.LockType = adLockBatchOptimistic
    rs.Open sqlstr
    MsgBox (rs.MaxRecords)
    rs.Close
    con.Close
    'MsgBox (con.State)
 
End Sub


kindly help me resolve this issue ASAP.

Regards
Sukant
sukant_asthanaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saurabh Singh TeotiaCommented:
You need to change this line in your code...

rs.Open sqlstr

Open in new window


To this...

rs.Open sqlstr,con

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
Saurabh Singh TeotiaCommented:
Basis of the problem that he specified and the amendments which i gave to him..His code will work for sure as he wasn't using connecting string when he is running the code..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.