vba 为什么要克隆 MS-Access 记录集?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2366358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:19:08  来源:igfitidea点击:

Why clone an MS-Access recordset?

ms-accessvbaclonerecordset

提问by dmr

I'm a newbie at VBA and attempting to understand someone else's code.

我是 VBA 的新手,试图理解别人的代码。

Set rstClone = Me.RecordsetClone
rstClone.MoveFirst

设置 rstClone = Me.RecordsetClone
rstClone.MoveFirst

Why does the recordset have to be cloned? Why can't the code be Me.Recordset.MoveFirst?

为什么必须克隆记录集?为什么代码不能是 Me.Recordset.MoveFirst?

回答by Fionnuala

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.

您可能希望使用记录集克隆,因为您不希望影响表单中显示的记录,而 me.recordset.movefirst 会这样做。

回答by David-W-Fenton

First off, the recordset is notcloned -- the form's Recordsetclone exists as long as there is a recordsource, even if it contains no records.

首先,记录集不会被克隆——只要有记录源,表单的 Recordsetclone 就存在,即使它不包含任何记录。

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).

其次,recordsetclone 是一个独立的记录集,您可以浏览它并且不会影响表单的编辑缓冲区,它具有一组独立的记录指针(即书签)。

That said, it's pretty senseless to set a recordset variable to the recordsetclone. Instead, just use a WITH block:

也就是说,将记录集变量设置为记录集是非常没有意义的。相反,只需使用 WITH 块:

  With Me.RecordsetClone
    .FindFirst "[MyPK]=" & Me!cmbFindByPK
    If Not .NoMatch Then
       If Me.Dirty Then
          Me.Dirty = False
       End If
       Me.Bookmark = .Bookmark
    End If
  End With

The alternative using setting a recordset variable looks like this:

使用设置记录集变量的替代方法如下所示:

  Dim rs As DAO.Recordset

  Set rs = Me.RecordsetClone
  rs.FindFirst "[MyPK]=" & Me!cmbFindByPK
  If Not rs.NoMatch Then
     If Me.Dirty Then
        Me.Dirty = False
     End If
     Me.Bookmark = rs.Bookmark
  End If
  Set rs = Nothing

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.

另请注意,从 Access 2000 开始,除了 RecordsetClone 之外,表单还有一个 Recordset 对象。该对象使您可以访问表单的实际编辑缓冲区,并通过它导航更改表单本身中的记录指针。不过,我会避免使用它,因为间接使用作为相同数据的动态集的单独相同对象似乎是一个有用的保护层,可以防止做不应该做的事情。

回答by Albert D. Kallal

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:

例如:

Set rstClone  = me.recordset
rstClone.movenext

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.

典型的情况是当您使用子表单时。如果您想合计或遍历该子表单中的 10 条记录,您可以这样做,而不会影响或导致子表单当前指向的当前显示记录发生变化。它可以让您在幕后做事。

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.

但是,如果您只想移动到表单的下一条记录,那么您不需要 reocrdset 或记录集克隆,您只需执行将表单移动到下一条记录的命令即可。

You could use the following typical command :

您可以使用以下典型命令:

DoCmd.GoToRecord acActiveDataObject, , acNext

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:

而且,如果您想查看放置在表单中的代码中的值,则不需要 recordset 或 recordsetClone,您可以去:

me!nameOfCollumFromTable

回答by cowb0y

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.

使用记录集属性可能会导致意外行为。具体来说(在 Access 2010 中),如果您引用 me.recordset,它可能会意外地导致表单的编辑缓冲区被保存。

Ex.:

前任。:

debug.print me.recordset.recordcount

will cause the form's record to be saved. Using recordsetClone will not exhibit this [mis]behavior.

将导致表单的记录被保存。使用 recordsetClone 不会表现出这种 [mis] 行为。