Re: ADODB.Recordset: Operation is not allowed when the object is closed. (3704)



Randy Snyder wrote:

I have very little experience with ADO. I wrote the script below. I am
attempting to open a connection to a Microsoft SQL database, run a query
and then echo out the data that is returned.

I am able to establish a connection with the database; however, I get a
"ADODB.Recordset:Operation is not allowed when the object is closed."
(Error Number 3704)

What I am missing? I appreciate any assistance you can provide.

-Randy

------------8<------------------

sqlText = "USE IMYahooDB " & _
"select * " & _
"from messages " & _
"where (sender_im ='sip:joe_user@xxxxxxxxx') or " &_
"(recipient_im = 'sip:joe_user@xxxxxxxxx') " & _
"order by message_date"

Set cn = CreateObject("ADODB.Connection")

cn.Open = "PROVIDER=sqloledb;" &_
"DATA SOURCE=DBServer01;" &_
"DATABASE=IMYahooDB;" &_
"Integrated Security=SSPI"

If cn.State = 1 Then
WScript.Echo "Connection is open."
Else
WScript.Echo "There is a problem with the connection."
WScript.Quit(1)
End If

'Set rs = CreateObject("ADODB.RecordSet")
Set rs = cn.Execute(sqlText)
rs.ActiveConnection = cn
rs.Open sqlText,cn

If rs.EOF Then
WScript.Echo "No records returned."
Else
Do While NOT rs.EOF
WScript.Echo rs("sender_name")
WScript.Echo rs("message_date")
rs.MoveNext
Loop
End If

cn.Close
rs.Close
Set cn = Nothing
Set rs = Nothing
WScript.Quit(0)

------------8<------------------

You open the recordset object twice, once when you invoke the Execute method
of the connection object, then again when you invoke the Open method of the
recordset object. I believe that accounts for the error message. Try:

Set rs = CreateObject("ADODB.RecordSet")
rs.ActiveConnection = cn
rs.Source = sqlText
rs.Open

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--


.



Relevant Pages

  • Re: Recordset Errors / Friendly Message needed
    ... Loop ... 'sql statement to return input values drawn from html fields within ... 'zero out recordset object ... 'smack around the db connection until it lets go ...
    (microsoft.public.inetserver.asp.general)
  • Re: Online query to my database
    ... >> specified through the Provider property of the Connection object. ... >> SQL statement against a SQL Server data source. ... >> Use the Initial Catalog property to specify the database. ... >> Create a Recordset object if any rows are returned. ...
    (microsoft.public.sqlserver.connect)
  • RE: Online query to my database
    ... > specified through the Provider property of the Connection object. ... > instance of SQL Server, although existing applications can also use MSDASQL ... > Use the Initial Catalog property to specify the database. ... > Create a Recordset object if any rows are returned. ...
    (microsoft.public.sqlserver.connect)
  • RE: Online query to my database
    ... ADO can use any OLE DB provider to establish a connection. ... SQL statement against a SQL Server data source. ... Use the Initial Catalog property to specify the database. ... Create a Recordset object if any rows are returned. ...
    (microsoft.public.sqlserver.connect)
  • RE: Q. Problems with combo box NotInList event
    ... > 'connection and recordset object variables ... >'Dim cn As ADODB.Connection ... >Dim rs As DAO.Recordset ' DAO ... > 'open a connection to the connection object ...
    (microsoft.public.access.formscoding)