[英]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: 我已经读过在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. 我看不到像CurrentDB.CloseRecordset这样的东西,我不认为CurrentDB.Close是个好主意。 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. 我通过ODBC连接使用MS Access 2007和SQL Server后端。
If any of my terminology or use of such is wrong, feel free to correct me! 如果我的任何术语或使用此类错误,请随时纠正我!
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
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. 所以你不能。
.Close
记录集,因为它不再存在。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.