Why clone an MS-Access recordset?

后端 未结 4 1628
孤独总比滥情好
孤独总比滥情好 2020-12-20 18:51

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

Set rstClone = Me.RecordsetClone
rstClone.MoveFirst

<
4条回答
  •  情深已故
    2020-12-20 19:28

    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:

      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.

提交回复
热议问题