Visual Basic

Recordset

The method TMS uses for sending and receiving data to and from the database is a custom recordset object (cRecordset). In this chapter, whenever I mention recordset, I am referring to this custom cRecordset implementation, rather than to the various DAO/RDO/ADO incarnations.

A recordset is returned from the DAL by calling the OpenRecordset method. This recordset is completely disconnected from the data source. Updating a recordset will have no effect on the central data source until you call the UpdateRecordset method on the DAL.

We could have easily returned an array of data from our DAL, especially since ADO and RDO nicely support GetRows, which does exactly that, but arrays are limited in a number of ways.

Array manipulation is horrible. In Visual Basic, you can resize only the last dimension of an array, so forget about adding columns easily. Also, arrays are not self-documenting. Retrieving information from an array means relying on such hideous devices as constants for field names and the associated constant maintenance, or the low-performance method of looping through indexes looking for fields.

Enabling varying rows and columns involves using a data structure known as a ragged array-essentially an array of arrays-which can be cumbersome and counterintuitive to develop against.

The advantage of using a custom recordset object is that we can present the data in a way that is familiar to most programmers, but we also get full control of what is happening inside the recordset. We can again simplify and customize its operation to support the rest of our components.

Notice the Serialize method, which allows us to move these objects easily across machine boundaries. More on this powerful method later. For the moment, let's look at the typical interface of a cRecordset.

cRecordset Interface

Member Description
MoveFirst Moves to first record
MoveNext Moves to next record
MovePrevious Moves to previous record
MoveLast Moves to last record
Name Shows the name of the recordset
Fields Returns a Field object
Synchronize Refreshes the contents of the recordset
RowStatus Shows whether this row has been created, updated, or deleted
RowCount Shows the number of records in the recordset
AbsolutePosition Shows the current position in the recordset
Edit Copies the current row to a buffer for modification
AddNew Creates an empty row in a buffer for modification
Update Commits the modification in the buffer to the recordset
Serialize Converts or sets the contents of the recordset to an array

This table shows the details of the interface for the cField object, which is returned from the cRecordset object.

cField Interface

Member Description
Name Name of the field
Value Contents of the field
Type Visual Basic data type
Length Length of the field