如果我不創建記錄集對象,是否需要在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

注意!

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



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