Part II: Programming Microsoft Access
496
Debug.Print rs!CustomerID, rs!Company
rs.MoveFirst
Debug.Print rs!CustomerID, rs!Company
rs.Close
Set rs = Nothing
End Sub
This procedure begins by opening a Recordset object populated with data from tblCus-
tomers. It immediately displays the CustomerID and Company from the very first record; then
it moves around the recordset a few rows at a time, displaying the CustomerID and Company for
each record along the way. It ends by returning to the first record and displaying its data. The out-
put produced by RecordsetNavigation is shown in Figure 13.12.
FIGURE 13.12
Demonstrating recordset navigation
Obviously, this is a trivial example meant to demonstrate how easily ADO recordsets can be navi-
gated. As a developer, you’re free to work with any record in the recordset, moving up and down
the rows as needed.
Access recordsets support the concept of a current record pointer. Only one record within a record-
set is current at a time. When you make changes to a recordset or navigate through its rows, your
code affects only the current record.
The RecordsetNavigation procedure also demonstrates how to reference individual fields
within a record. After moving to a row, individual fields are referenced as members of the record-
set. Access works on just one record at a time, so any reference to a field evaluates to the field
within the current record.
Understanding CursorType
In the RecordsetNavigation procedure, notice the recordset’s CursorType property. In this
example, it’s set to adOpenStatic. There are several settings for CursorType; adOpenStatic
means to open the recordset with a static type cursor. Access uses a cursor to keep track of the cur-
rent record in a recordset. A static cursor means that the data in the recordset is static, and new
records can’t be added to the recordset. Static cursors are ideal when the purpose of the recordset
is to review data in the underlying tables and adding new records is not necessary.