Solved

Closing Open ADO Recordsets

Posted on 2000-05-09
14
323 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
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
 

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:
bhess1 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 32

Expert Comment

by:bhess1
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:bhess1
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…

863 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

30 Experts available now in Live!

Get 1:1 Help Now