【问题标题】:Do I need to close a recordset in MS Access if I don't create a recordset object?如果我不创建记录集对象,是否需要在 MS Access 中关闭记录集?
【发布时间】:2013-01-27 07:23:28
【问题描述】:

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

Dim ClientName As String

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

我看不到像 CurrentDB.CloseRecordset 这样的东西,而且我认为 CurrentDB.Close 不是一个好主意。在这种情况下我需要关闭任何记录集,还是它会自动关闭?

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

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

【问题讨论】:

    标签: database vba ms-access-2007 recordset


    【解决方案1】:

    您的代码创建了一个临时记录集;它在语句完成后立即超出范围。所以你不能.Close记录集,因为它不再存在。

    情况类似于这个立即窗口会话...

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

    【讨论】:

    • 酷,这就是我的怀疑。我会在下周上班的时候测试一下,然后告诉你。
    【解决方案2】:

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

    此代码是否有效,是否表明您已添加到记录集集合中:

    Dim ClientName As String
    
    msgbox CurrentDB.Recordsets.Count
    
    ClientName = CurrentDB.OpenRecordset([some SQL]).Fields(0).Value
    
    msgbox CurrentDB.Recordsets.Count
    

    四肢走动,这行得通吗:

    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
    

    【讨论】:

    • 这是一个很棒的想法。让我下周上班的时候测试一下,我会告诉你的。
    • 根据下面的评论,我想我会删除这个答案,因为它完全不正确!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    相关资源
    最近更新 更多