Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Closing Open ADO Recordsets

Posted on 2000-05-09
14
Medium Priority
?
353 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 400 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

670 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