Class Module Savings

29
Savings Account using Class Modules Please use speaker notes for additional information!

description

vb

Transcript of Class Module Savings

Page 1: Class Module Savings

Savings Account using Class Modules

Please use speaker notes for additional information!

Page 2: Class Module Savings

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.

Page 3: Class Module Savings

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.

Page 4: Class Module Savings

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.

Page 5: Class Module Savings

SavAcct00.vbp

Page 6: Class Module Savings

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.

Page 7: Class Module Savings

SavAcct00.vbp

Page 8: Class Module Savings

SavAcct00.vbp

Page 9: Class Module Savings

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.

Page 10: Class Module Savings

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

Page 11: Class Module Savings

SavAcct01.vbp

Page 12: Class Module Savings

SavAcct01.vbp

RaiseEvent on insufficient funds.

Page 13: Class Module Savings

SavAcct01.vbpIf there are events that can be raised or triggered, the Dim statement needs to include the WithEvents clause.

Page 14: Class Module Savings

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.

Page 15: Class Module Savings

SavAcct01.vbp

Page 16: Class Module Savings

SavAcct02.vbp

Page 17: Class Module Savings

SavAcct02.vbp

Page 18: Class Module Savings

SavAcct02.vbp

Page 19: Class Module Savings

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.

Page 20: Class Module Savings

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

Page 21: Class Module Savings

SavAcct02.vbp

Public Function NewEnum() Set NewEnum = colSavAccts.[_NewEnum]End Function

Page 22: Class Module Savings

SavAcct02.vbp

Page 23: Class Module Savings

SavAcct02.vbp

Page 24: Class Module Savings

SavAcct02.vbp

Page 25: Class Module Savings

SavAcct02.vbp

Page 26: Class Module Savings

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.

Page 27: Class Module Savings

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

Page 28: Class Module Savings
Page 29: Class Module Savings