Solved

Closing Open ADO Recordsets

Posted on 2000-05-09
14
322 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
  • 5
  • 4
  • 2
  • +3
14 Comments
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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
Comment Utility
I think i saw something at http://616.org
0
 

Expert Comment

by:flaviosousa
Comment Utility
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
 

Author Comment

by:MBrinkert
Comment Utility
Unlike DAO, ADO does not contain a Recordsets collection.
0
 

Author Comment

by:MBrinkert
Comment Utility
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
Comment Utility
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:
bhess1 earned 100 total points
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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:bhess1
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, didn't read carefully bhess1's comment.
0
 

Author Comment

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

Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Help on project with Soap 10 42
String manipulation in Visual Basic 7 43
using Access 8 52
How to make an ADE file by code? 11 31
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now