我可以在不同的表上使用相同的记录集变量吗?

发布于 2024-10-14 18:59:59 字数 59 浏览 7 评论 0原文

我是 Microsoft Access 的新手。

如何在不同的表上使用相同的记录集变量?

I am new to Microsoft Access.

How can I use the same recordset variable on different tables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Spring初心 2024-10-21 18:59:59

是的,只要您不希望变量指向两个记录集,就可以。

但问题是你为什么要这么做?您希望节省什么?资源?没什么大不了的。代码的可读性也会受到影响。如果您一周后或其他人稍后查看您的代码,他们可能不会意识到发生了什么。因此我建议每个表/查询一个记录集变量。

还有变量的范围。如果在子例程/函数中定义,则它仅在该子例程/函数中可见。如果位于顶部,则该表单/报告/模块中的所有 sou/函数都可以看到它。如果您声明它在模块中是全局的,那么它将在任何地方都可见。

所以问题是你为什么问?

Yes you can so long as you don't expect the variable to be pointing to two recordsets.

However the question is why would you? What would you expect to save? Resources? Not a big deal. Also code readability would suffer. If you a week from now or someone else later are looking at your code they might not realize what is going on. Thus I'd suggest a recordset variable per table/query.

Also there is the scope of the variable. If defined in a subroutine/function then it's visible only in that sub/function. If at the top it will be visible to all sou/function in that form/report/module. If you state that it's global while in a module then it will be visible everywhere.

So the question is why do you ask?

孤檠 2024-10-21 18:59:59

是的,但在将记录集变量重新分配给另一个记录集之前,必须确保关闭该记录集。这是一个例子:

Dim rs As DAO.Recordset
Dim bTimeToChangeRecordsets As Boolean

Set rs = CurrentDb.OpenRecordset("Contacts")

'add business logic here'

If bTimeToChangeRecordsets Then
    rs.Close
    'setting to Nothing is not necessary here,'
    'because setting to a new recordset instance has the '
    'same effect on the variable reference count '
    Set rs = CurrentDb.OpenRecordset("Comments")
End If

'more business logic'

rs.Close
Set rs = Nothing

关于这个问题的许多评论都集中在是否需要将记录集变量设置为Nothing,是否以及何时需要这样做,以及是否还需要在记录集上调用Close释放记录集引用之前的记录集。关于这个主题有一些精彩的评论,分布在有关堆栈溢出的相当多的问题上; 这个特别重要与您的情况相关。我还会引导您阅读这篇知识库文章这本深入的书籍摘录

冒着过度简化的风险,我可以这样总结问题:如果 Access 引用计数工作良好,则无需担心通过将引用设置为 Nothing 来显式释放引用,也无需显式关闭记录集。然而,实践经验告诉我们,考虑到 Access 的行为,这两个习惯都应该成为 VBA 最佳实践编码的一部分。

Yes, but you must be sure to close the recordset before you re-assign the recordset variable to another recordset. Here is an example:

Dim rs As DAO.Recordset
Dim bTimeToChangeRecordsets As Boolean

Set rs = CurrentDb.OpenRecordset("Contacts")

'add business logic here'

If bTimeToChangeRecordsets Then
    rs.Close
    'setting to Nothing is not necessary here,'
    'because setting to a new recordset instance has the '
    'same effect on the variable reference count '
    Set rs = CurrentDb.OpenRecordset("Comments")
End If

'more business logic'

rs.Close
Set rs = Nothing

Many of the comments on this question have focused on the need to set the recordset variable to Nothing, whether and when this is necessary, and whether it is also necessary to call Close on the recordset before releasing the recordset reference. There is some excellent commentary on this topic spread out accross quite a few questions on stack overflow; this one is particularly relevant to your situation. I would also direct you to this knowledgebase article and this in-depth book excerpt.

At risk of oversimplifying, I can summarize the issue this way: if Access reference counting worked well, it would not be necessary to worry about explicitly releasing references by setting them to Nothing, nor explicitly closing recordsets. Howerver, practical experience tells us that, given the behavior of Access, both of these habits should be part of best practice coding for VBA.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文