Separate speaker notes to accompany classmodulesavings:
This presentation will go through a series of programs involving class modules.
This slide shows processing using this project.
Note here that curBalance and intIntRate have been set up as module level variables in the class module SavAcct.cls.
Class properties can be set through a Property Let procedure and retrieved through a Property Get procedure. I created procedures and eliminated the Let for the Balance because I only want to be able to retrieve or get the balance. The balance will always be changed through the withdrawal or retrieval processes.
Note that in fact I do not use interest rate yet. I decided to break the program down into steps and I had already defined interest rate in anticipation, so I left it!
The withdrawal method and the deposit method are used to effect the balance.
The new object is created when the set statement is executed. In this example the set statement is in the form load.
In Reset, objSavAcct.Balance means that the Get procedure of Balance is executed to retrieve the balance for display.
In deposit, the objSavAcct.Deposit method is passed the wkTranAmt which goes in as vTranAmt and is used to add to the balance. Then the balance is retrieved and displayed.
In withdrawal, the objSavAcct.WithDrawal method is passed the wkTranAmt which goes in as vTranAmt and is used to subtract from the balance.
I do not actually validate data, I am assigning the data from the text box to the work area. However, I want to comment on the way validate works.
"The Validate event works in tandem with the CausesValidation property to prevent a control from losing the focus until certain criteria are met. Important: The Validate event only occurs when the control which is about to receive the focus has its CausesValidation property set to True." from Microsoft VB Help
I am still not using the interest rate. I am simply setting it in this program.
This shows the setting of the Interest Rate.
This shows another way of coding the posting of the interest rate. In this case objSavAcct.IntRate is put on the form using the Get. This change better illustrates the use of the class module.
This is the first part of the code for the frmSavAcct. It handles Deposits and Interest.
This slide elaborates on the code that handles the deposit on the form and shows the connection to the class module SavAcct.
This is the rest of the code for the form. The fix shown puts in a 0 deposit or withdrawal if an amount is not entered. You could put in additional code to force the user to make an entry.
This has been revised in SavAcct01.vbp
This project will not allow a withdrawal if there is not a sufficient balance to cover the withdrawal. The insufficient funds raises or triggers an event.
The Event InsufficientFunds is defined. If the curBalance is less than the vTranAmt, then the RaiseEvent will cause InsuffientFunds to be executed.
This shows the the Dim on objSavAcct includes WithEvents because we now have coded the ability to RaiseEvent when the balance is insufficient.
This shows the Insufficient Funds routine that is executed when the Event is Raised.
In the validate, the txtTranAmt is only moved to wkTranAmt if there is data entered and 0 is moved to wkTranAmt is no data is entered. You could add more code to force an entry.
If the event InsufficientFunds is raised, then the form puts out a message box that warns the user.
This shows that three different accounts have been setup. Note that there is no file associated with this processing so the accounts are not permanent. In this example account 111 has a balance of 950 and an interest rate of 5 etc. The accounts are seen in the list box when Display Accounts is clicked. The account is originally created through Add Account and can be removed or retrieved through the appropriate buttons. Once an account is retrieved, deposits and withdrawals can be made.
This shows the class module for the individual savings account. Note that the account now has an additional AccountNumber property (previous examples only had a Balance property and an IntRate property). The AccountNumber will be used as a key to retrieve the account that is needed from the SavAccts collection that has been added to this program. Note that previous programs only dealt with one account. This program will allow more than one account.
The individual account activities are in SavAcct and the activities related to the collection of accounts is in SavAccts following the concept that a collection is noted with the plural.
This shows the Class moduel SavAccts which is a collection. Note that in the General area I have Private colSavAccts as Collection and then in Class_Initialize I is instantiated by Set colSavAccts = New Collection.
Class_Terminate sets colSavAccts = Nothing.
"The Nothing keyword is used to disassociate an object variable from an actual object. Use the Set statement to assign Nothing to an object variable." from Microfocus VB help
We are now dealing with the collection. When we want to add an account to the collection we need to pass it an account number and an interest rate. We do not have the ability to impact balance directly (only through deposits and withdrawals), so a balance is not passed. We then establish the NewSavAcct with the account number and interest rate that was passed to it and use the Add to actually establish the account object with a key of NewSavAcct.AccountNumber.
Note that NewSavAcct is locally set up as a New SavAcct object.
Note that colSavAccts is the name of the collection.
Removal is done based on the account number, so only the account number is passed.
An account number is passed to the Function that is designed to return one member of the collection. Item now refers to that particular member or object. Based on the retrieval of a specific member or object, I can now act on that object with withdrawals and deposits.
The For Each is not available for user defined collections so extra steps must be taken to make it workable. First you need to expose the enumerator which is the hidden property of the collection in a function. Note that the brackets are needed because the underscore that says this is a hidden property is not a legal character for a property. After writing the function shown, you then need to go into Tools/Procedure Attributes/Advanced and set the procedure ID to -4 and check the box for Hide this member. Source for this is Advanced Programming Using Visual Basic 6 by Bradley and Millspaugh.
This shows the entire Add. The user clicks on the Add Account command button and colSavAccts.Add is passed the account number and the interest rate. It uses these to set up a new account and then the actual add happens with the Account Number as the key. After the add is complete the from then calls cmdRetrvAcct_Click to retrieve the account so it can be shown on the screen. That code is shown on the next slide.
The new account that was added is now retrieved using the function that is the Item method. I used labels to display the information to make it clear that the information could not be changed by the user. I am now setting the caption of the Opening Balance to the value that is in the Balance for this account, and setting the Closing Balance to empty. I then unlock the interest rate to show the value of the IntRate for this account and then lock it back up.
When the user clicks on remove account, the account number in wkAcctNbr is sent to colSavAccts.Remove. The account is removed by the code in the collection since the collection controls adding and removing from the collection.
This code is why the Enum described previously in this presentation had to be set up. This clears the list box where I will display the data and defines an object of the SavAcct type that I will use in this routine. It then goes through each object in the collection and sets up a display line to show the account number, balance and interest rate of that object. The object is then added to the list box and then the Next is reached and the For Each is executed again if there are more objects in the collection.
Form_Load uses set...new to instantiate the class modules for the savings account object and for the collection.
Initialize variables initializes the fields on the form.
This shows playing with the appearance of the interest rate.
When Set Interest Rate is clicked, the appearance is changed from flat to 3-D, other changes are made to the look, the text box is unlocked and receives the focus. The user then enters the interest rate.
When the text box for the interest rate looses focus, the appearance is changed including setting it back to flat, the box is locked and the interest rate is put into wkIntRate.
When you want to make a deposit, first you must retrieve the right account using the Item method which you pass the account number currently in wkAcctNbr.
Then using that account, you pass the transaction amount to the deposit method and add it to the current balance.
When you want to make a withdrawal, you click on the withdrawal button and that brings up cmdWthDrwl_click. You then retrieve the account using the Item method and then using that account you pass the transaction amount to the withdrawal method and the amount is subtracted from the balance providing there are sufficient funds. If the funds are not sufficient the Insufficient Funds event is raises and the subtraction is not done.