Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Closing Open ADO Recordsets

Posted on 2000-05-09
14
Medium Priority
?
358 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: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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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

963 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