简体   繁体   中英

Do I need to close a recordset in MS Access if I don't create a recordset object?

I've read that it's important to close recordset objects in Access, but with my code, I never create a recordset object, I always just use an inline reference, eg:

Dim ClientName As String

ClientName = CurrentDB.OpenRecordset([some SQL]).Fields(0).Value

I can't see anything like CurrentDB.CloseRecordset, and I don't think CurrentDB.Close is a good idea. Do I need to close any recordset in this case, or does it do that automatically?

I'm using MS Access 2007 with an SQL Server backend via an ODBC connection.

If any of my terminology or use of such is wrong, feel free to correct me!

Apparently CurrentDB.OpenRecordset 'appends to the recordset collection'

So does this code work, and does it indicate you have added to the recordsets collection:

Dim ClientName As String

msgbox CurrentDB.Recordsets.Count

ClientName = CurrentDB.OpenRecordset([some SQL]).Fields(0).Value

msgbox CurrentDB.Recordsets.Count

and going out on a limb, does this work:

Dim ClientName As String

msgbox CurrentDB.Recordsets.Count

ClientName = CurrentDB.OpenRecordset([some SQL]).Fields(0).Value

msgbox CurrentDB.Recordsets.Count

msgbox CurrentDB.Recordsets(0).Close

Your code creates an ephemeral recordset; it goes out of scope immediately after the statement has completed. So you can't .Close the recordset because it no longer exists.

The situation is similar to this Immediate window session ...

? CurrentDB.Recordsets.Count
 0 
strSelect = "SELECT Count(*) FROM Dual;"
MyVar = CurrentDB.OpenRecordset(strSelect)(0)
? MyVar
 1 
? CurrentDB.Recordsets.Count
 0 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM