In this exercise, you will implement the Debit method of the CAccount class.

Change the Student AccountBalance

The Debit method is called only after the book purchase transaction has been authorized. This method will add the book price amount to the student's account balance.

  1. In the Debit method declare an ADO Recordset object called rsStudent.

  2. Declare a variable of type Double called dCurrentBalance.

  3. Declare a second variable of type Double called dNewBalance.

  4. Create a new Recordset object with the Open method using the following parameters:
  5. Parameter Value
    Source "Select AccountBalance from Students Where Student ID = " & str(iStudentID)
    ActiveConnection cnStateUBookstore
    CursorType adOpenKeyset
    LockType adLockOptimistic
  1. Set the dCurrentBalance variable equal to the value of the AccountBalance field in the recordset.

  2. Set the dNewBalance variable equal to the account balance plus the book price.

  3. Set the AccountBalance field's value equal to the new balance.

  4. Call the Update method of the recordset to save the change.

  5. Add an error handler to the Debit method that returns vbFalse and raises the Warning event with the error description of any error that occurs.

  6. Return vbTrue from the Debit method to indicate success.

  7. Close and release the Recordset object variable.

    To see an example of how your code should look, click this icon.

  8. Save your work.

Record the book purchase

  1. After the student AccountBalance has been written, this transaction needs to be recorded in the AccountLog table.

  2. Create a new private function in the class called RecordPurchase. It should take an Integer argument called iStudentID.

  3. Declare a string variable called sSQLInsert. It will contain the SQL Insert statement.

  4. Set the sSQLInsert text to a SQL Insert string to add a new record to the AccountLog table. Your code should resemble the following:
    sSQLInsert = "INSERT INTO AccountLog(StudentID,LogDate,Amount,Memo) " & _
                     "VALUES(" & Str(iStudentID) & ", '" & Str(Now) & _
                     "'," & Str(mvarPrice) & ",'" & mvarMemo & "')"
      
  5. Call the Execute method of the active connection to insert the record into the AccountLog table.

  6. Add a call to the RecordPurchase function at the end of the Debit method.

  7. Test the class.

    If you created a test procedure in the previous exercise, you can add to that procedure to test the Debit method for the same student.

  8. Save your work.