Visual Basic

Updating a Recordset

Updating a cRecordset object through the DAL occurs by way of the UpdateRecordset method. UpdateRecordset will scan through the internal arrays in the recordset and perform the required database operation. The unique row identifier is used to retrieve each row for modification, so if someone has updated a row while the recordset has been in operation this row will not be found and an error will be raised to alert the developer to this fact. The following is an example of updating a row in a recordset and persisting that change to the data source:

Dim oRec     As New cRecordset
  Dim oDAL
      As New cDAL  ' Assume local DAL so don't need serialization
  Set oRec = oDAL.OpenRecordset("Employees")
  oRec.Edit
  oRec.Fields("FirstName") = "Adam Magee"
  oRec.Update
  oDAL.UpdateRecordset oRec

Synchronizing a Recordset

After a cRecordset object has been used by UpdateRecordset to successfully update the data source, the cRecordset object needs to have the same changes committed to itself, which is accomplished by means of the Synchronize method.

Using the Synchronize method will remove all Deleted rows from the recordset and will set any Updated or Created rows to Read status. This gives the developer using the cRecordset object control over committing changes and also means the recordset state can be maintained in the event of an UpdateRecordset failure. Here is an example of synchronizing a recordset after a row has been updated:

oDAL.UpdateRecordset oRec
  oRec.Synchronize
  Parameters

Simply supplying the name of a cRecordset object to OpenRecordset is usually not enough information, except maybe when retrieving entire sets of data, so we need a way of supplying parameters to a DAL cRecordset operation.

This is achieved by using a cParams object. The cParams object is simply a collection of cParam objects, which have a name and a value. The cParams object, like the cRecordset object, can also be serialized. This is useful if the parameters need to be maintained on another machine.

CParams Interface

Member Description
Add Adds a new cParam object with a name and a value
Item Returns a cParam object
Count Returns the count of collected cParam objects
Remove Removes a cParam object
Serialize Converts or sets the content of cParams object into an array

CParam Interface

Member Description
Name Name of the parameter
Value Variant value of the parameter

Here is an example of retrieving a recordset with parameters:

Dim oDAL As New cDAL
  Dim oRec As New cRecordset
  Dim oPar As New cParams
  oPar.AddField "EmpID", "673"
  oPar.AddField "CurrentlyEmployed", "Yes"
  Set oRec = oDAL.OpenRecordset("Employees", oPar)
  MsgBox oRec.RecordCount

We can see now that only two well-defined objects are parameters to the DAL-cRecordset and cParams-and both of these objects support serialization, giving a consistent, distributed operation-aware interface.