VBGood网站全文搜索 Google

首页 - 经验之谈 - 循环ADO/DAO记录之前使用MoveFirst
发表评论(0)作者:不详, 平台:VB6.0+Win98, 阅读:14533, 日期:2001-04-24
VB myth #2: Use MoveFirst before looping through ADO/DAO recordsets

In early versions of DAO, after opening a recordset it was necessary
to use the MoveFirst method to move the cursor to the first record
before looping through the recordset. ADO and DAO have removed the
need to issue MoveFirst before looping through a recordset, however.
That注释:s because after opening a recordset that contains records, both
these engines automatically move the cursor to the first record. If
the recordset is empty, then they set the BOF and EOF properties to
True. Even so, we often see code examples on the message boards still
issuing MoveFirst before looping through a recordset.

Instead, you can reliably use the EOF property to determine if the
recordset contains records, manipulate the record as you please, then
issue the MoveNext command before looping through the structure. Using
the latest DAO engine in Access, the simplified code might be:

Dim rst As DAO.Recordset
Set rst = CurrentDb.OpenRecordset("tblCategories")
With rst
    Do Until .EOF
        Debug.Print .Fields(1)
End With
Set rst = Nothing
End Sub

For ADO, you注释:d create and open the recordset differently, of course,
but the loop structure would remain the same.