In this exercise, you will write code to initialize the CAccount class. You will also implement the Authorize method of the CAccount class.

Create a connection to the database

  1. In the General Declarations section of the CAccount class, declare a Private ADO Connection object.

  2. In the Initialize event of the CAccount class, create a new instance of the ADO Connection object.

  3. Add code to open the connection.

  4. In the Terminate event of the CAccount class, add code to close the connection and release the Connection object variable.

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

Implement the Authorize method

The Authorize method will determine if a student has enough credit to purchase a book. It will look up a student's account balance in the StateUBookstore database. Students can owe the university a maximum of $500. If they have a balance including the book they are trying to buy of less than $500, the Authorize method will return True. If they are within $50 of their limit, they will be authorized, but the CAccount class will raise the Warning error to let students know they are close to their limit. If students are over their $500 limit, the Authorize method will return False.

  1. Open the CAccount class module.

  2. In the Authorize method, declare an ADO Recordset object called rsAccountBalance.

  3. Declare two variables of type Double. One will hold the current account balance. Call it dBalance. The other will hold the account balance including the book price. Call it dPotentialBalance.

  4. Declare a string variable called strWarning to hold a warning message used by the Warning event.

  5. Create a new ADO Recordset object with the Open method using the following parameters:
  6. Parameter Value
    Source "Select AccountBalance from Students Where StudentID = " & str(iStudentID)
    ActiveConnection cnStateUBookstore
    CursorType adOpenStatic
    LockType adLockReadOnly


  7. Set the dBalance variable equal to the value of the AccountBalance field in the recordset.

  8. Set the dPotentialBalance variable equal to the account balance plus the book price.

  9. Use a Case statement to evaluate the dPotentialBalance variable based on the following criteria:
  10. Criteria Result
    Is <450 The Authorize method returns True.
    Is <= 499 The Authorize method returns True and raises the Warning event because the student is within $50 of their $500 limit.
    Is >= 500 The Authorize method returns False and raises the Warning event that the student does not have enough available credit.
    All other values The Authorize method returns False.


  11. Close and release the recordset object variable.

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

  12. Test the class.

    To test the CAccount class, you may want to create a test procedure that creates an instance of the class, and calls the Authorize method for a student. Remember that you can use the Data View window to create a connection to the StateUBookstore database. With such a connection, you can open tables and evaluate the data to determine if your class is working correctly.

  13. Save your work.