Visual Basic

Synchronizing a Recordset Commands

A lot of database operations do not involve, or should not involve, cRecordset objects. For instance, checking and changing a password are two operations that do not require the overhead of instantiating and maintaining a cRecordset. This is where you use the ExecuteCommand method of the DAL. The ExecuteCommand method takes as parameters both the name of the command to perform and a cParams object.

Any output cParam objects generated by the command are automatically populated into the cParams object if they are not supplied by default. Here's an example of checking a password:

Dim oDAL As New cDAL
  Dim oPar As New cParams
  oPar.Add "UserID", "637"
  oPar.Add "Password", "Chebs"
  oPar.Add "PasswordValid", ""     ' The DAL would have added this output
                                   ' parameter if we had left it out.
  oDAL.ExecuteCommand "CheckPassword", oPar
  If oPar("PasswordValid").Value = "False" Then
      Msgbox "Sorry Invalid Password", vbExclamation
  End If

Synchronizing a Recordset Transactions

Most corporate data sources support transactions; our DAL must enable this functionality as well. This is relatively easy if you are using data libraries such as DAO or RDO, since it is a trivial task to simply map these transactions onto the underlying calls. If your data source does not support transactions, you might have to implement this functionality yourself. If so, may the force be with you. The three transaction commands are BeginTransaction, CommitTransaction, and RollbackTransaction.

The DAL is taking care of all the specifics for our transaction, leaving us to concentrate on the manipulation code. In the case of a transaction that must occur across business objects, we'll see later how these business objects will all support the same DAL. Here's an example of updating a field inside a transaction:

Dim oRec As New cRecordset
  Dim oDAL As New cDAL  ' Assume local DAL so don't need serialization
  With oDAL
      .BeginTransaction
      Set oRec = oDAL.OpenRecordset("Employees")
      With oRec
          .Edit
          .Fields("FirstName") = "Steve Gray"
          .Update
      End With
      .UpdateRecordset oRec
      .CommitTransaction
  End With
  Wrap Up

So that's a look at how our abstracted data interface works. I hope you can see how the combination of cDAL, cRecordset, and cParams presents a consistent logical interface to any particular type of data source. There is comprehensive support for distributed operation in the sense that the DAL is completely stateless and that the input and output objects (cParams and cRecordset) both support serialization.