Class Module Savings
-
Upload
andymclaren -
Category
Documents
-
view
220 -
download
2
description
Transcript of Class Module Savings
Savings Account using Class Modules
Please use speaker notes for additional information!
Enter 100 in Transaction Amount and Click Deposit.
Click on reset to transfer into balance. Enter amount
and click on withdrawal.
Click on reset to make opening balance.
SavAcct.vbp
The deposit and withdrawal methods give the class the behaviors it needs to increase or decrease the balance.
vTranAmt is the information passed to the deposit or withdrawal method.
The Get and Let procedures are assign and retrieve property values. In this example, I do not want to assign to Balance so there is no Let.
SavAcct.vbp
objSavAcct is dimensioned
objSavAcct is instantiated
Reset puts the current objSavAcct.Balance on the form using Get.
The Deposit method receives wkTranAmt as vTranAmt and curBalance is changed. When I get the Balance it receives curBalance.
“Occurs before the focus shifts to a (second) control that has its CausesValidation property set to True.” from Microsoft VB In fact, I am not really doing validation here, I am assigning.
SavAcct00.vbp
SavAcct00.vbp
On the form an InputBox is used to take in the interest rate. The interest rate is now assigned to objSavAcct.IntRate where as vIntRate it is assigned to intIntRate.
SavAcct00.vbp
SavAcct00.vbp
Private Sub cmdDpsit_Click() objSavAcct.Deposit wkTranAmt lblClsBal.Caption = objSavAcct.Balance cmdDpsit.Enabled = False cmdWthDrwl.Enabled = False cmdSetInt.Enabled = False cmdReset.Enabled = TrueEnd Sub
Private Sub cmdDpsit_Click() objSavAcct.Deposit (wkTranAmt) lblClsBal.Caption = objSavAcct.Balance cmdDpsit.Enabled = False cmdWthDrwl.Enabled = False cmdSetInt.Enabled = False cmdReset.Enabled = TrueEnd Sub
Both ways of coding work:Both ways of coding work:
The steps in coding the line objSavAcct.Deposit (wkTranAmt)
The Deposit Method on the Class Module SavAcct.
SavAcct00.vbp
In this example the validate makes sure that an amount was entered in the transaction box. However, problems develop if the entry is not made because the amount from the last transaction is used. A simple fix is shown below.
Private Sub txtTrnAmt_Validate(Cancel As Boolean) If txtTrnAmt.Text <> "" Then wkTranAmt = txtTrnAmt.Text Else wkTranAmt = 0 End IfEnd Sub
SavAcct01.vbp
SavAcct01.vbp
RaiseEvent on insufficient funds.
SavAcct01.vbpIf there are events that can be raised or triggered, the Dim statement needs to include the WithEvents clause.
SavAcct01.vbp
Note that the code that puts out the msgbox saying insufficient funds is on the form.
In this example, the validate checks to make sure a transaction amount was entered before transferring to wkTranAmt, and acts on the problem by setting to 0.
SavAcct01.vbp
SavAcct02.vbp
SavAcct02.vbp
SavAcct02.vbp
SavAcct02.vbp
Public Sub Add(ByVal vAcctNbr As String, ByVal vIntRate As Integer)Dim NewSavAcct As New SavAcct With NewSavAcct .AccountNumber = vAcctNbr .IntRate = vIntRate colSavAccts.Add NewSavAcct, .AccountNumber End With'The last line of the with block actually adds the NewSavAcct object to the savings'accounts collection (SavAccts) and uses the AccountNumber as a key to the'collection. You can retrieve that object from the collection by using the'AccountNumber.End Sub
Public Sub Remove(ByVal vAcctNbr As String) colSavAccts.Remove vAcctNbrEnd Sub
Option Explicit'A collection is a container object - it contains objects'A collection can only have certain methods such as Add, Remove and ItemPrivate colSavAccts As Collection
Private Sub Class_Initialize() Set colSavAccts = New CollectionEnd Sub
This sets up a NewSavAcct which is an object of the SavAcct type and then adds it to the collection.
SavAcct02.vbp
Public Function Item(ByVal vAcctNbr As String) As SavAcct Set Item = colSavAccts.Item(vAcctNbr)End Function'Retrieves a particular object from the collection - this is the item method
Option Explicit'A collection is a container object - it contains objects'A collection can only have certain methods such as Add, Remove and ItemPrivate colSavAccts As Collection
Private Sub Class_Initialize() Set colSavAccts = New CollectionEnd Sub
SavAcct02.vbp
Public Function NewEnum() Set NewEnum = colSavAccts.[_NewEnum]End Function
SavAcct02.vbp
SavAcct02.vbp
SavAcct02.vbp
SavAcct02.vbp
SavAcct02.vbp
Private Sub Form_Load() Set objSavAcct = New SavAcct Set colSavAccts = New SavAccts Call Initialize_VariablesEnd Sub
Public Sub Initialize_Variables() wkAcctNbr = "" wkBalance = 0 wkIntRate = 0 wkTranAmt = 0 txtAcctNbr.Text = "" lblOpnBal.Caption = "" lblClsBal.Caption = "" txtIntRate.Locked = False txtIntRate.Text = "" txtIntRate.Locked = True txtTrnAmt.Text = "" cmdDpsit.Enabled = True cmdWthDrwl.Enabled = True cmdReset.Enabled = TrueEnd Sub
Instantiates the class modules SavAcct and SavAccts.
SavAcct02.vbp
Private Sub txtIntRate_LostFocus() With txtIntRate .Appearance = 0 .BackColor = &H8000000F .BorderStyle = 0 .Locked = True wkIntRate = .Text End WithEnd Sub
Private Sub cmdSetInt_Click() With txtIntRate .Appearance = 1 .BackColor = &H80000005 .BorderStyle = 1 .Locked = False .SetFocus End WithEnd Sub