DAO Recordset: Bookmarks and Record Positions

OverviewHow Do IFAQSampleODBC Driver List

This article explains MFC's interfaces to:

  • The Bookmark property in DAO recordsets.

  • The AbsolutePosition and PercentPosition properties in DAO recordsets.

For details about these properties in DAO, see the following topics in DAO Help:

  • Bookmark Property

  • AbsolutePosition Property

  • PercentPosition Property

  • Positioning the Current Record Pointer with DAO

Those DAO Help topics explain the fundamentals of the properties. This article explains how MFC exposes them to you.

Bookmarks in MFC DAO

Because records can be deleted from a recordset, you can't rely on the absolute position of a record within the recordset. The reliable way to keep track of the position of a particular record in your recordset is to use the record's bookmark.

Except for snapshot-type recordsets, each record has a unique bookmark from the time the recordset is created. In MFC, class supplies member functions for:

  • Getting the bookmark of the current record, so you can save it in a variable.

  • Moving quickly to a given record by specifying its bookmark, which you've saved earlier in a variable.

You can check whether a recordset supports bookmarks by calling .

For example, suppose you want to mark the current record so you can later return to it easily. The following code does this:

// rs is a CDaoRecordset or
// CDaoRecordset-derived object

COleVariant varRecordToReturnTo;
varRecordToReturnTo = rs.GetBookmark( );

//...more code in which you move to other records
rs.SetBookMark( varRecordToReturnTo );

There is no need to extract the underlying data type from the object. Simply get it with and return to that bookmark with .

Absolute and Percent Positions in MFC DAO

Besides bookmarks (and , , and ), DAO provides two other ways to position the current record in a recordset: percent positioning and absolute positioning.

****Note   ****Neither absolute nor percent positioning is recommended for moving the current record to a specific record. Use a bookmark instead. See DAO Recordset: Bookmarks and Record Positions.

Neither percent positioning nor absolute positioning is available for table-type recordsets.

Percent Positioning

You can set the current record to a position that follows a specified percentage of the records in a recordset, and you can get the percentage position of the current record. This is useful for setting scroll bars. That is, you can:

  • Call to determine the percentage position of the current record, that is, what percentage of the records precede the current record.

  • Call to make the record at a specified percentage position in a recordset the current record. For example, you might make the record at 50% of the current record, halfway through the recordset.

Keep in mind the following guidelines:

  • Percent positioning is approximate, and the exact record that is set can be affected by deletion of records.

  • If you call SetPercentPosition before the recordset is fully populated, the amount of movement is relative to the number of populated records. Records are populated as you move to them and they are retrieved from the database. You can determine the number of populated records by calling . You can populate all records in the recordset by calling (keep in mind that calling MoveLast can be slow for dynaset-type and snapshot-type recordsets).

For related information, see the topic "PercentPosition Property" in DAO Help.

Absolute Positioning

You can set or get the record number of the current record in a recordset. That is, you can:

  • Call to get the AbsolutePosition property of the recordset, which contains the ordinal position (zero-based) of the current record in a recordset.

  • Call to set the AbsolutePosition property. This makes the record at that ordinal position in the recordset the current record.

Important   The absolute position of a record is potentially unreliable. If the user can delete records ahead of a position, the ordinal number of records following the deletion is decreased. Bookmarks are a more reliable method of working with record positions. See Bookmarks in MFC DAO.

Keep in mind the following guideline:

  • Setting a position greater than the number of populated records causes MFC to throw an exception. Records are populated as you move to them and they are retrieved from the database. You can determine the number of populated records by calling .

For related information, see the topic "AbsolutePosition Property" in DAO Help.

See Also   DAO: Where Is..., DAO Recordset, DAO Recordset: Recordset Navigation, DAO Recordset: Seeking and Finding