Subject: Scope of recordset variables
Posted By: James Diamond Post Date: 1/28/2004 5:33:33 AM
Hi,

I have some general functions in an ASP include file. One of them is:

------------------------------------------------------
Function GetTable(tablename)

    Dim d
    Dim strSQL

    strSQL = "SELECT * FROM " & tablename

    Set d = Server.CreateObject("ADODB.RecordSet")
    rsData.Open strSQL, dcnDB, adOpenStatic, adLockReadOnly

    Set GetTable = d

End Function
------------------------------------------------------


In my main page, I do this:

' Declare recordset variable
Dim rsData

' Call function
Set rsData = GetTable("Clients")

< code to read and display the records etc...>

' Cleanup
rsData.Close
Set rsData = nothing


Question:

I do the cleanup on the main page (to close the recordset). However, there is no cleanup in the function for the local d variable. I can't cleanup that because if I do, I cannot pass the contents of it back in the statement Set GetTable = d.

Does d cleanup automatically by virtue of going out of scope (because it is a local variable in the function)? Or do I end up with the recordset object d floating around on the server because I have not cleaned it up?

Please teach me about this area... cleanups / scoping / persistence of objects / how a recordset is "passed" from the function to my main page, etc.

Thanks.

James

//##



Reply By: bmains Reply Date: 1/28/2004 8:23:39 AM
I'm not as knowledgable as some of the other individuals that post to this site, but what I usually did is this:

Set GetTable = d
set d = nothing

I know in the .NET world, the garbage collector automatically recycles unused variables for you, but I think in the ASP/VB6 world, you have to do it.  I know VB6 you should set your variables to nothing; otherwise, resources will still be open (I had a problem with, when accessing the Word API's, the Word.exe file would never close, and I would be left with 50 instances!).

I'm not sure how crucial it is in ASP; personally, I make a note of it to always set my variables to nothing in the end, even in my .NET development.

Hope this helps,

Brian
Reply By: James Diamond Reply Date: 1/28/2004 8:37:44 AM
Thanks, Brian.

Can someone give me a second opinion on this issue, please?

James


Go to topic 9054

Return to index page 961
Return to index page 960
Return to index page 959
Return to index page 958
Return to index page 957
Return to index page 956
Return to index page 955
Return to index page 954
Return to index page 953
Return to index page 952