为什么要克隆 MS-Access 记录集?
我是 VBA 的新手,正在尝试理解别人的代码。
设置 rstClone = Me.RecordsetClone
rstClone.MoveFirst
为什么必须克隆记录集?为什么代码不能是Me.Recordset.MoveFirst?
I'm a newbie at VBA and attempting to understand someone else's code.
Set rstClone = Me.RecordsetClone
rstClone.MoveFirst
Why does the recordset have to be cloned? Why can't the code be Me.Recordset.MoveFirst?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能希望使用 recordsetclone,因为您不希望影响表单中显示的记录,而 me.recordset.movefirst 会这样做。
You may wish to use the recordsetclone because you do not wish to affect the records displayed in the form, which me.recordset.movefirst would do.
首先,记录集不会被克隆——只要存在记录源,表单的Recordsetclone就存在,即使它不包含任何记录。
其次,recordsetclone 是一个独立的记录集,您可以对其进行导航,并且不会影响表单的编辑缓冲区,该缓冲区具有一组独立的记录指针(即书签)。
也就是说,将记录集变量设置为 recordsetclone 是毫无意义的。相反,只需使用WITH 块:
使用设置记录集变量的替代方法如下所示:
另请注意,从 Access 2000 开始,除了 RecordsetClone 之外,表单还具有 Recordset 对象。该对象使您可以访问表单的实际编辑缓冲区,并且通过它进行导航会更改表单本身中的记录指针。不过,我会避免使用它,因为间接使用单独的相同对象(即相同数据的动态集)似乎是一个有用的保护层,可以防止人们做不应该做的事情。
First off, the recordset is not cloned -- the form's Recordsetclone exists as long as there is a recordsource, even if it contains no records.
Second, the recordsetclone is an independent recordset that you can navigate and not have an effect on the form's edit buffer, which has an independent set of record pointers (i.e., bookmarks).
That said, it's pretty senseless to set a recordset variable to the recordsetclone. Instead, just use a WITH block:
The alternative using setting a recordset variable looks like this:
Note also that since Access 2000, the form also has a Recordset object in addition to the RecordsetClone. That object gives you access to the form's actual edit buffer, and navigation through it changes the record pointer in the form itself. I would avoid using it, though, as the indirection of using a separate identical object that is a dynaset of the same data seems a helpful layer of protection from doing things one oughtn't.
请记住,记录集具有称为克隆方法的方法。这与表单记录集克隆不同。
在您的示例和问题中,我们讨论了表单所基于的基础数据。
如果您要使用表单所基于的代码来处理和移动记录,但您不希望表单显示或图形界面跟随您或跳跃,那么您的示例是正确且首选的方法来实现这一点。
因此,记录集克隆是表单数据的副本。它允许您移动或遍历该记录集中的记录,但表单(用户界面)不会跟随您在记录中的移动。
请记住,在某些情况下,如果您确实希望表单移动到下一条记录,那么您不会使用记录集克隆,而是使用实际记录集。
例如:
在上面,表格将移动到下一条记录。
典型的情况是当您使用子表单时。如果您想要合计或遍历该子表单中的 10 条记录,您可以这样做,而不会影响或导致子表单当前指向的当前显示记录发生更改。可以说,它可以让您在幕后做事情。
但是,如果您只想移动到表单的下一条记录,那么您不需要 reocrdset 或记录集克隆,只需执行将表单移动到下一条记录的命令。
您可以使用以下典型命令:
并且,如果您想查看放置在表单中的代码中的值,则不需要 recordset 或 recordsetClone,您可以直接执行以下操作:
Keep in mind that record sets have which called a clone method. This is different then the forms record set clone.
In your example and question we talking about the underlying data that the form is based on.
If you’re going to play with, and move through records using code that that a form is based on, but you don’t want the form display or graphical interface to follow you along or jump around then your example is the correct and preferred way to accomplish this.
So, record set clone is a copy of the forms data. It allows you to move or traverse records in that record set, but the form (the user interface) does not follow your moving through the records.
Remember, in some cases if you do in fact want the form to move to the next record, then you would not use records set clone, but Use the actual record set.
Eg:
In the above, the form would then move to the next record.
A typical situation is when you’re using sub forms. If you wanted to total up or traverse the 10 records in that sub-form, you can do so without affecting or causing the current display record the sub form is currently pointing to change. It lets you do things Behind the scenes so to speak.
However, if you just wanted to move to the forms next record, then you don’t need either reocrdset, or recordset clone, you could just execute a command that moves the form to the next record.
You could use the following typical command :
And, you don't need recordset, or recordsetClone if you wanting to look at values in code placed in a form, you can just go:
使用记录集属性可能会导致意外行为。具体来说(在 Access 2010 中),如果您引用 me.recordset,它可能会意外地导致保存表单的编辑缓冲区。
例如:
将导致保存表单的记录。使用 recordsetClone 不会出现这种[错误]行为。
Using the recordset property can cause unintended behavior. Specifically (in Access 2010), if you refer to me.recordset, it can unexpectedly cause the form's edit buffer to be saved.
Ex.:
will cause the form's record to be saved. Using recordsetClone will not exhibit this [mis]behavior.