Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 599
Default Recordset object loses validity in call between function & subrout

quartz

The problem is that the Workspace object is falling out of scope, so all of
its child object's fall out of scope. You need to preserve the Workspace
object either by creating a module level variable for it, or by passing it
into a sub. You might rewrite it as follows

Sub DAORecordsetQuery(ByRef rs As DAO.Recordset, _
ByRef ws As DAO.Workspace, _
argConnectionString As String, _
argSQL As String, _
argTimeout As Integer)

Dim cn As DAO.Connection

Set ws = DAO.CreateWorkspace("", "", "", dbUseODBC)
Set cn = ws.OpenConnection("", , , argConnectionString)
cn.QueryTimeout = argTimeout
Set rs = cn.OpenRecordset(argSQL, dbOpenDynaset)

End Sub

Sub test()

Dim rs As DAO.Recordset
Dim ws As DAO.Workspace

DAORecordsetQuery rs, ws, myConn, mysql, 100

Do While Not rs.EOF
Debug.Print rs.Fields(0).Value
rs.MoveNext
Loop

End Sub

You pass rs and ws ByRef because ws is the top level object and you need to
preserve it and rs is the object you need. cn will stay in scope because ws
does.

If you use ADO instead of DAO, you don't have to jump through all those
hoops. In ADO, Recordset is a top level object, so you don't need to
preserve the connection. You do, however, have to remove the ODBC; from the
start of the connection string (which I do with the Right() function).

Function ADORecordsetQuery(sConn As String, _
sSql As String) As ADODB.Recordset

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

cn.Open sConn
rs.Open sSql, cn

Set ADORecordsetQuery = rs

End Function

Sub test2()

Dim rs As ADODB.Recordset

Set rs = ADORecordsetQuery(Right(myConn, Len(myConn) - 5), mysql)

Do While Not rs.EOF
Debug.Print rs.Fields(0).Value
rs.MoveNext
Loop

End Sub

If you switch to ADO, you'll have to mess with some of the properties of rs
in the function to mirror the dbOpenDynaset that you use now. I think
you'll need to change the CursorLocation property, but I'm not sure about
that.

--
Dick Kusleika
MVP - Excel
Excel Blog - Daily Dose of Excel
www.dicks-blog.com

"quartz" wrote in message
...
I wrote a function that uses DAO to retrieve data from an Oracle database
into a recordset. The query runs fine and retrieves data into the

recordset,
but when I try to use the recordset in the calling subroutine, the

recordset
object is "no longer valid". My function appears below. Can someone please
correct this code? (please correct for line wrapping)

Function DAORecordsetQuery(argConnectionString As String, argSQL As

String,
argTimeout As Integer) As DAO.Recordset

Dim ws As DAO.Workspace
Dim cn As DAO.Connection
Dim rs As DAO.Recordset

Set ws = CreateWorkspace("", "", "", dbUseODBC)
Set cn = ws.OpenConnection("", , , argConnectionString)
cn.QueryTimeout = argTimeout
Set rs = cn.OpenRecordset(argSQL, dbOpenDynaset)
Set DAORecordsetQuery = rs

End Function

Thanks in advance.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default Recordset object loses validity in call between function & subrout

"Dick Kusleika" wrote ...

The problem is that the Workspace object is falling out of scope

In ADO, Recordset is a top level object, so you don't need to
preserve the connection.


That's right. The Recordset has an ActiveConnection property which
retains a reference to the Connection object.

If you switch to ADO, you'll have to mess with some of the properties of rs
in the function to mirror the dbOpenDynaset that you use now.


The 'dynaset' is known to the non-DAO world as a keyset cursor e.g.
CursorType=adOpenKeyset in ADO.

I think
you'll need to change the CursorLocation property, but I'm not sure about
that.


To use a keyset cursor in ADO, the cursor location must be server
side.

Jamie.

--
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
call Module from MS Excel Object - Probably very easy VB questions Kevin Excel Discussion (Misc queries) 4 January 4th 08 02:32 AM
Recordset or Object is closed HELP PLEASE! DBAL Excel Programming 10 July 28th 04 09:36 AM
Recordset Problem - object is closed DBAL Excel Programming 0 June 4th 04 11:55 PM
call the object of scanner... sal21[_15_] Excel Programming 1 March 2nd 04 01:47 PM


All times are GMT +1. The time now is 08:09 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"