在VBA中关闭记录集 [英] Closing a recordset in VBA

查看:102
本文介绍了在VBA中关闭记录集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经读过你应该明确关闭你打开的每个记录集。在这种特殊情况下,我以下列方式打开记录集:

I have read that every recordset that you open should be explicitly closed. In this particular case, I have opened the recordset in the following way:

展开 | 选择 | Wrap | 行号

推荐答案

您的< b> *另一个想法* 基本上就是Seth的答案。您需要关闭记录集。将变量设置为 Nothing 不太重要,但需要关闭记录集。另一件要避免的事情是使用函数 CurrentDb()的返回值作为参考。这应该分配给DAO.Database变量和使用的变量。

Your *Another Thought* was basically the answer Seth. You do need to close recordsets. Setting the variable to Nothing is less important, but recordsets need to be closed. Another thing to avoid is using the return value of the function CurrentDb() as a reference as you have. This should be assigned to a DAO.Database variable and that variable used.

展开 | 选择 | Wrap < span class =codeDivider> | 行号


我的另一点值得一提的是,您可能不知道Recordset是否实际上是在第一时间创建的。这通常可以在具有错误捕获的本地过程中发生,而在实际创建Recordset之前或之后可能发生错误。在这种情况下,您需要检查Recordset对象本身是否存在,如:
Another point that I feel is worth mentioning is that you may not know if a Recordset was actually created in the first place. This can typically occur in a Local Procedure with Error Trapping whereas an Error can occur either prior to, or after, a Recordset was actually created. In this scenario, you need to check for the existence of the Recordset Object itself, as in:
展开 | 选择 | Wrap | 行号


@ADezii I实际上在尝试关闭记录集时遇到错误,由于代码运行的路径,If / Then语句从未打开过。我想不出一种方法来测试它是否被打开所以我只是将开始代码移动到它将始终运行的位置。你的代码可以帮我解决这个问题,所以非常感谢你指出这一点。


@NeoPa我认为你的第二篇文章将是我为我的数据库选择的途径,但我会选择你的第一个作为答案,因为它最好地回答了我的问题。


可能,在另一个线程中提出更好的问题,但为什么最好为CurrentDb声明一个变量而不是仅使用 CurrentDb.OpenRecordset ?我经常看到在线示例中使用的变量,但我只是认为如果需要多个位置(通常db或dbs是使用的变量),则必须输入较少的变量。我并不认为使用变量和使用直线值之间存在差异。
@ADezii I have actually run into having errors when trying to close a recordset that due to the path the code ran through If/Then statements was never opened. I couldn''t think of a way to test to see if it was opened so I just moved the opening code to where it would always run. Your code helps me with that angle, so thank you for point this out.

@NeoPa I do think that your second post will be the avenue that I choose for my database, but I will choose your first as the answer as it answers my question the best.

Possibly, better asked in a different thread, but why is it better to declare a variable for the CurrentDb instead of just using CurrentDb.OpenRecordset? I have often seen a variable used in examples online, but I just figured that it was so that less would have to be typed if it was needed multiple places (often db or dbs was the variable used). I didn''t think that there was a difference between using a variable and using the straight value.


这篇关于在VBA中关闭记录集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆