Solved

Closing Open ADO Recordsets

Posted on 2000-05-09
14
338 Views
Last Modified: 2013-11-23
As a cleanup procedure, is there a simple way to close ALL open ADO recordsets when I'm ready to exit a program or sub that will no longer need those recordsets?

I don't want to have to refer to them all individually by name.
0
Comment
Question by:MBrinkert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +3
14 Comments
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2794411
The only way I know of is to make your recordsets an array, and assign the recordset values to members of the array.

You could also, I suppose, make a Class of ADO recordsets, and open/close them in the class with a For Each loop.
0
 

Expert Comment

by:visualfool
ID: 2794530
I think i saw something at http://616.org
0
 

Expert Comment

by:flaviosousa
ID: 2794580
If there is a Recordsets collection you may try:

sub CloseAllRecordSets
  dim rs as recordset

  for each rs in recordsets
    rs.close
  next
end sub

I'm not sure if the RecordSets is independent or is part of another object.
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:MBrinkert
ID: 2794693
Unlike DAO, ADO does not contain a Recordsets collection.
0
 

Author Comment

by:MBrinkert
ID: 2794706
visualfool:
Interesting site. Thanks for the pointer. However, I couldn't find anything there that would help.

bhess1:
Can you give me a short example?
0
 

Author Comment

by:MBrinkert
ID: 2794712
visualfool:
Interesting site. Thanks for the pointer. However, I couldn't find anything there that would help.

bhess1:
Can you give me a short example?
0
 
LVL 32

Accepted Solution

by:
Brendt Hess earned 100 total points
ID: 2794744
Here's an example using an array of recordsets.  A class takes far more code.

' For easy reference and reading of your code, create constant values
' that relate to your recordsets, e.g.

Const rsCustomers = 1
Const rsOrders = 2
Const rsCharges = 3
Const rsPayments = 4

' Then, create an array of recordsets
' I tend to use element 0 for short term, temporary recordsets.

Dim rs(0 to 4) as ADODB.RecordSet


'When you open a recordset, remember to create a NEW rs.
' Here's a procedure to open a recordset

Sub OpenRS(ByVal SQL as String, Conn as ADODB.Connection, ByVal CursorLocation as Long, ByVal CursorType as Long, LockType as Long, ByVal Options as Long, rs as ADODB.RecordSet)

  Set rs = NEW ADODB.RecordSet
  rs.CursorLocation = CursorLocation
  rs.Open SQL, Conn, CursorType, LockType, Options
End Sub

'  Call like this:

OpenRS "Customers", myConn, adUseServer, adOpenDynamic, adLockOptimistic, adCmdTableDirect, rs(rsCustomers)


' access the recordset like this:

rs(rsCustomers).MoveFirst
Do While Not rs(rsCustomers).EOF
   Debug.Print rs(rsCustomers)!CustomerID
   rs(rsCustomers).MoveNext
Loop


' Close all of the open recordsets like this:

Sub CloseAllRS()
Dim I as Integer

For I = LBound(rs) to UBound(rs)
    If rs(I).State = adStateOpen Then
       rs(I).Close
    End If
    Set rs(I) = Nothing
Next I

End Sub
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2794756
You could re-write this to use dynamic assignment for the rs numbers, e.g.:

Dim rs() as ADODB.RecordSet

Dim rsCustomers as Integer
Dim rsOrders as Integer
Dim rsCharges as Integer
Dim rsPayments as Integer


' Open code modified as follows:

Sub OpenRS(ByVal SQL as String, Conn as ADODB.Connection, ByVal CursorLocation as Long, ByVal CursorType as Long, LockType as Long, ByVal Options as Long, rsNum)

Dim I as Integer
  On Error Goto NoRecordSets
  For I = 1 to UBound(rs)
    If rs(I) Is Nothing Then Exit For ' can re-use
  Next I
  If I > UBound(rs) Then Redim Preserve RS(0 to I)

  Set rs(I) = NEW ADODB.RecordSet
  rs(I).CursorLocation = CursorLocation
  rs(I).Open SQL, Conn, CursorType, LockType, Options
  rsNum = I
  Exit Sub

NoRecordSets:
  ReDim rs(0 to 1)
  Resume
End Sub



' re-write the call to open like this:
OpenRS "Customers", myConn, adUseServer, adOpenDynamic, adLockOptimistic, adCmdTableDirect, rsCustomers


' Everything else should remain the same.
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2794872
You could re-write this to use dynamic assignment for the rs numbers, e.g.:

Dim rs() as ADODB.RecordSet

Dim rsCustomers as Integer
Dim rsOrders as Integer
Dim rsCharges as Integer
Dim rsPayments as Integer


' Open code modified as follows:

Sub OpenRS(ByVal SQL as String, Conn as ADODB.Connection, ByVal CursorLocation as Long, ByVal CursorType as Long, LockType as Long, ByVal Options as Long, rsNum)

Dim I as Integer
  On Error Goto NoRecordSets
  For I = 1 to UBound(rs)
    If rs(I) Is Nothing Then Exit For ' can re-use
  Next I
  If I > UBound(rs) Then Redim Preserve RS(0 to I)

  Set rs(I) = NEW ADODB.RecordSet
  rs(I).CursorLocation = CursorLocation
  rs(I).Open SQL, Conn, CursorType, LockType, Options
  rsNum = I
  Exit Sub

NoRecordSets:
  ReDim rs(0 to 1)
  Resume
End Sub



' re-write the call to open like this:
OpenRS "Customers", myConn, adUseServer, adOpenDynamic, adLockOptimistic, adCmdTableDirect, rsCustomers


' Everything else should remain the same.
0
 

Expert Comment

by:Buller
ID: 2795657
Why don´t you just close the ADO connection object ? This should close any active recordset created from the connection as well ?
0
 

Author Comment

by:MBrinkert
ID: 2797301
We're not using a connection object like that. Instead of creating a connection object and referencing it in the rs.open command, we've created a string constant that we use in the rs.open command. This allows us to open and close the connection whenever we need to use it, instead of opening it at the beginning of the program and leaving it open with a database lock the whole time. This may not be the fastest or most efficient approach, but it does seem to conserve resources on an already overloaded server.
0
 
LVL 2

Expert Comment

by:Davy070599
ID: 2797706
Remember that a recordset is only closed when set to Nothing.

ex.

Set rs = Nothing

Closing the recordset, puts it only on 'standby'
0
 
LVL 2

Expert Comment

by:Davy070599
ID: 2797714
Sorry, didn't read carefully bhess1's comment.
0
 

Author Comment

by:MBrinkert
ID: 2811800
A good replacement for the Recordsets collection that, sadly, is missing from ADO.

Thanks!
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month6 days, 22 hours left to enroll

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question