如果我不创建记录集对象,是否需要在MS Access中关闭记录集?

[英]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, e.g.:

我已经读过在Access中关闭记录集对象很重要,但是对于我的代码,我从不创建记录集对象,我总是只使用内联引用,例如:

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?

我看不到像CurrentDB.CloseRecordset这样的东西,我不认为CurrentDB.Close是个好主意。在这种情况下,我是否需要关闭任何记录集,还是自动执行此操作?

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

我通过ODBC连接使用MS Access 2007和SQL Server后端。

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

如果我的任何术语或使用此类错误,请随时纠正我!

2 个解决方案

#1


2  

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 

#2


2  

Apparently CurrentDB.OpenRecordset 'appends to the recordset collection'

显然,CurrentDB.OpenRecordset'附加到记录集集合'

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
智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2013/02/12/720392cd304d5633283ef9774863b362.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告