You are on page 1of 29

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
SavAcct.vbp

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.

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.

SavAcct.vbp
SavAcct.vbp

The Deposit method receives wkTranAmt as vTranAmt and curBalance is


changed. When I get the Balance it receives curBalance.

objSavAcct is
dimensioned

Reset puts the current


objSavAcct.Balance
on the form using Get.

objSavAcct is instantiated

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

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

SavAcct00.vbp
SavAcct00.vbp

Both ways of coding work:


Private Sub cmdDpsit_Click()
objSavAcct.Deposit wkTranAmt
lblClsBal.Caption = objSavAcct.Balance
cmdDpsit.Enabled = False
cmdWthDrwl.Enabled = False
cmdSetInt.Enabled = False
cmdReset.Enabled = True
End Sub

Private Sub cmdDpsit_Click()


objSavAcct.Deposit (wkTranAmt)
lblClsBal.Caption = objSavAcct.Balance
cmdDpsit.Enabled = False
cmdWthDrwl.Enabled = False
cmdSetInt.Enabled = False
cmdReset.Enabled = True
End Sub

The steps in coding the line


objSavAcct.Deposit (wkTranAmt)

The Deposit Method


on the Class Module
SavAcct.

SavAcct00.vbp
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 If
End Sub

SavAcct01.vbp
SavAcct01.vbp

SavAcct01.vbp
SavAcct01.vbp

RaiseEvent on
insufficient funds.

SavAcct01.vbp
SavAcct01.vbp

If there are events that can be raised or


triggered, the Dim statement needs to
include the WithEvents clause.

SavAcct01.vbp
SavAcct01.vbp

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.

Note that the code that puts out the


msgbox saying insufficient funds is on
the form.

SavAcct01.vbp
SavAcct01.vbp

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp
Option Explicit
'A collection is a container object - it contains objects
'A collection can only have certain methods such as Add, Remove and Item
Private colSavAccts As Collection
Private Sub Class_Initialize()
Set colSavAccts = New Collection
End Sub
Public Sub Add(ByVal vAcctNbr As String, ByVal vIntRate As Integer)
Dim NewSavAcct As New SavAcct
This sets up a NewSavAcct which is an object of the
With NewSavAcct
.AccountNumber = vAcctNbr
SavAcct type and then adds it to the collection.
.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 vAcctNbr
End Sub

SavAcct02.vbp
SavAcct02.vbp
Option Explicit
'A collection is a container object - it contains objects
'A collection can only have certain methods such as Add, Remove and Item
Private colSavAccts As Collection
Private Sub Class_Initialize()
Set colSavAccts = New Collection
End Sub

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

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

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp

SavAcct02.vbp
SavAcct02.vbp
Private Sub Form_Load()
Set objSavAcct = New SavAcct
Set colSavAccts = New SavAccts
Call Initialize_Variables
End 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 = True
End Sub

Instantiates the class modules


SavAcct and SavAccts.

SavAcct02.vbp
SavAcct02.vbp
Private Sub cmdSetInt_Click()
With txtIntRate
.Appearance = 1
.BackColor = &H80000005
.BorderStyle = 1
.Locked = False
.SetFocus
End With
End Sub

Private Sub txtIntRate_LostFocus()


With txtIntRate
.Appearance = 0
.BackColor = &H8000000F
.BorderStyle = 0
.Locked = True
wkIntRate = .Text
End With
End Sub

You might also like