vb class with access data please see speaker notes for additional information!

58
VB Class with Access Data Please see speaker notes for additional information!

Upload: scot-rose

Post on 17-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: VB Class with Access Data Please see speaker notes for additional information!

VB Class with Access Data

Please see speaker notes for additional information!

Page 2: VB Class with Access Data Please see speaker notes for additional information!

SavAcct07.vbpSavAcct07.vbp

Page 3: VB Class with Access Data Please see speaker notes for additional information!
Page 4: VB Class with Access Data Please see speaker notes for additional information!

Option ExplicitDim WithEvents objSavAcct As SavAcctDim colSavAccts As SavAcctDBIDim wkAcctNbr As StringDim wkBalance As CurrencyDim wkIntRate As IntegerDim wkTranAmt As CurrencyDim ans As String

Private Sub cmdAddAcct_Click()On Error GoTo AddAcctErr colSavAccts.Add wkAcctNbr, wkIntRate Call cmdRetrvAcct_Click Exit SubAddAcctErr: If Err.Number = saerrDupKey Then MsgBox "Account already exists.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End If End Sub

Private Sub cmdApplyInt_Click()On Error GoTo ApplyIntErr With colSavAccts.Item(wkAcctNbr) .PostInterest lblClsBal.Caption = .Balance End With Exit SubApplyIntErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End IfEnd Sub

I am now using a database as opposed to the collection in version 04.

As noted above, colSavAccts now refers to SavAcctDBI as opposed to the collection discussed before.

Page 5: VB Class with Access Data Please see speaker notes for additional information!

Private Sub cmdDpsit_Click()On Error GoTo DpsitErr With colSavAccts.Item(wkAcctNbr) .Deposit wkTranAmt lblClsBal.Caption = .Balance End With txtTrnAmt.Text = "" wkTranAmt = 0 txtTrnAmt.SetFocus Exit SubDpsitErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End IfEnd Sub

Page 6: VB Class with Access Data Please see speaker notes for additional information!

Private Sub cmdDsplyAccts_Click()lstDsplyAcct.Clear With colSavAccts If .Count > 0 Then Dim i As Long For i = 0 To .Count - 1 Dim objDsplyAcct As SavAcct Set objDsplyAcct = .Item(i) With objDsplyAcct Dim DsplyLine As String DsplyLine = .AccountNumber & " " & .Balance & " " & .IntRate lstDsplyAcct.AddItem DsplyLine End With Next i End If End WithEnd Sub

Page 7: VB Class with Access Data Please see speaker notes for additional information!

Private Sub cmdDsplyTrans_Click()On Error GoTo DsplyTransErr lstDsplyTrn.Clear With colSavAccts.Item(wkAcctNbr).Transactions If .Count > 0 Then Dim i As Long For i = 0 To .Count - 1 Dim objDsplyTran As Transaction Set objDsplyTran = .Item(i) With objDsplyTran Dim DsplyLine As String DsplyLine = .AccountNumber & " " & .TransactionNumber & " " & .TransactionType & " " & .TransactionAmount

lstDsplyTrn.AddItem DsplyLine End With Next i End If End With Exit Sub

DsplyTransErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End IfEnd Sub

Transactions references to the Transaction DBI that contains this particular accounts transactions.

Private Sub cmdReset_Click() Call Initialize_Variables txtAcctNbr.SetFocusEnd Sub

Page 8: VB Class with Access Data Please see speaker notes for additional information!

Private Sub cmdRetrvAcct_Click()On Error GoTo RetrvErr Set objSavAcct = colSavAccts.Item(wkAcctNbr) With objSavAcct lblOpnBal.Caption = .Balance lblClsBal.Caption = "" txtIntRate.Locked = False txtIntRate.Text = .IntRate txtIntRate.Locked = True End With Exit SubRetrvErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End IfEnd SubPrivate Sub cmdRmvAcct_Click()On Error GoTo RmvAcctErr colSavAccts.Remove wkAcctNbr Exit SubRmvAcctErr: Select Case Err.Number Case saerrKeyNF MsgBox "Account does not exist. " & Err.Number, vbOKOnly Case saerrnzbal MsgBox Err.Description, vbOKOnly Case Else MsgBox "Unexpected Error! " & Err.Number & " " & Err.Description, vbInformation End SelectEnd Sub

This calls the Remove in the SavAcctDBI which removes the account and the accompanying transactions.

Using SavAcctDBI Item method gives me a savings account object with this account number.

Page 9: VB Class with Access Data Please see speaker notes for additional information!

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

Private Sub cmdWthDrwl_Click()On Error GoTo WthDrwlErr ans = "" With colSavAccts.Item(wkAcctNbr) .WithDrawal wkTranAmt If ans = "" Then lblClsBal.Caption = .Balance End If wkTranAmt = 0 txtTrnAmt.Text = "" txtTrnAmt.SetFocus End With Exit SubWthDrwlErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End IfEnd Sub

Page 10: VB Class with Access Data Please see speaker notes for additional information!

Private Sub Form_Load() Set colSavAccts = New SavAcctDBI Call Initialize_VariablesEnd Sub

Private Sub lstDsplyAcct_Click() Call cmdReset_Click With lstDsplyAcct wkAcctNbr = Left(.List(.ListIndex), 4) End With txtAcctNbr.Text = wkAcctNbr Call cmdRetrvAcct_Click Call cmdDsplyTrans_ClickEnd Sub

Private Sub objSavAcct_InsufficientFunds() ans = MsgBox("Insfficient funds to cover withdrawal!", vbOKOnly)End Sub

Private Sub txtAcctNbr_LostFocus() wkAcctNbr = txtAcctNbr.TextEnd Sub

Private Sub txtIntRate_LostFocus() With txtIntRate .Appearance = 0 .BackColor = &H8000000F .BorderStyle = 0 .Locked = True wkIntRate = .Text End WithEnd Sub

Database rather than collection!

Page 11: VB Class with Access Data Please see speaker notes for additional information!

Private Sub txtTrnAmt_LostFocus() If txtTrnAmt.Text <> "" Then wkTranAmt = txtTrnAmt.Text End IfEnd 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

Page 12: VB Class with Access Data Please see speaker notes for additional information!

Option ExplicitEvent InsufficientFunds()Private strAcctNbr As StringPrivate curBalance As CurrencyPrivate intIntRate As IntegerPrivate colTransactions As TransactionDBI

Public Property Get Balance() As Currency Balance = curBalanceEnd Property

Public Property Let Balance(ByVal vBalance As Currency)Static BalanceSet As Boolean If Not BalanceSet Then curBalance = vBalance BalanceSet = True End IfEnd Property

Public Property Get IntRate() As Integer IntRate = intIntRateEnd Property

Public Property Let IntRate(ByVal vIntRate As Integer) intIntRate = vIntRateEnd Property

We are now dealing with a database rather than a collection.

Page 13: VB Class with Access Data Please see speaker notes for additional information!

Public Sub WithDrawal(ByVal vTranAmt As Currency) If curBalance < vTranAmt Then RaiseEvent InsufficientFunds Else curBalance = curBalance - vTranAmt colTransactions.Add strAcctNbr, "WTH", vTranAmt End IfEnd Sub

Public Sub Deposit(ByVal vTranAmt As Currency) curBalance = curBalance + vTranAmt colTransactions.Add strAcctNbr, "DEP", vTranAmt End Sub

Public Property Get AccountNumber() As String AccountNumber = strAcctNbrEnd Property

Public Property Let AccountNumber(ByVal vAcctNbr As String)Static AcctNbrSet As Boolean If Not AcctNbrSet Then strAcctNbr = vAcctNbr AcctNbrSet = True End IfEnd Property

Page 14: VB Class with Access Data Please see speaker notes for additional information!

Public Sub PostInterest()Dim IntAmt As Currency IntAmt = curBalance * (IntRate / 100) curBalance = curBalance + IntAmt colTransactions.Add strAcctNbr, "INT", IntAmtEnd Sub

Private Sub Class_Initialize() Set colTransactions = New TransactionDBI' MsgBox "Calling # = " & strAcctNbr, , "SavAcctClass"

End Sub

Public Sub LoadTransactions() colTransactions.LoadRecordSet strAcctNbrEnd Sub

Public Property Get Transactions() As TransactionDBI Set Transactions = colTransactionsEnd Property

Form uses to Get transactions properties (gives outside world access).

Instantiates the DBI. Creates link to Transaction DBI.

TransactionDBI LoadRecordSet method is passed the account number. Creates a record set of only the active savings account transactions.

Page 15: VB Class with Access Data Please see speaker notes for additional information!

Returns a reference to the connection.

Page 16: VB Class with Access Data Please see speaker notes for additional information!

Option Explicit

Private SavAcctConn As New SavAcctConnectionPrivate SavAcctRS As ADODB.RecordsetPrivate WithEvents retSavAcct As SavAcct

Public Enum SavAcctDBIErrors saerrDupKey = vbObjectError + 512 + 1001 saerrKeyNF = vbObjectError + 512 + 1002 saerrnzbal = vbObjectError + 512 + 1003 saerrInvalidDataType = vbObjectError + 512 + 1004End Enum

Private Sub Class_Initialize()On Error GoTo ClassInitError

Set SavAcctRS = New ADODB.Recordset Dim SavAcctSQL As String SavAcctSQL = "SELECT * FROM SavingsAccounts ORDER BY AcctNum" With SavAcctRS .CursorLocation = adUseClient .CursorType = adOpenDynamic .ActiveConnection = SavAcctConn.Object .LockType = adLockOptimistic .Open SavAcctSQL, , , , adCmdText End WithExit Sub

ClassInitError: Err.Raise Err.Number, "SavAcctDBI-Class_Initialize", Err.Source & vbCrLf & Err.DescriptionEnd Sub

See SavAcctConnection for this.

Page 17: VB Class with Access Data Please see speaker notes for additional information!

Private Sub SetUpSavAcct()On Error GoTo SetUpSavAcctError Set retSavAcct = New SavAcct With retSavAcct .AccountNumber = SavAcctRS!AcctNum .IntRate = SavAcctRS!IntRate .Balance = SavAcctRS!Balance .LoadTransactions End With Exit SubSetUpSavAcctError: Err.Raise Err.Number, "SavAcctDBI-SetUpSavAcct", Err.Source & vbCrLf & Err.Description End SubPublic Function Count() As Long Count = SavAcctRS.RecordCountEnd Function

Public Sub Add(ByVal vAcctNbr As String, vIntRate As Integer)On Error GoTo AddError Update retSavAcct With SavAcctRS .AddNew !AcctNum = vAcctNbr !IntRate = vIntRate !Balance = 0 .Update .Requery End With Exit Sub AddError: Err.Raise Err.Number, "SavAcctDBI-Add", Err.Source & vbCrLf & Err.DescriptionEnd Sub

Sets up a new SavAcct object from a savings account in the database.

Counts the number of records in the savings account table in the database.

This updates the existing record that has just been used prior to setting up the new account.

Puts the passed data into fields. Use ! To designate fields of the recordset.

This is an update of the database with the data just setup. The Requery updates the recordset and in this process puts it into order (the additions were done at the end).

Page 18: VB Class with Access Data Please see speaker notes for additional information!

Public Sub Remove(vAcctNbr)On Error GoTo RemoveError: Update retSavAcct Set retSavAcct = Nothing With SavAcctRS .MoveFirst .Find "AcctNum ='" & vAcctNbr & "'" If .EOF Then Err.Raise saerrKeyNF, "SavAcctDBI", "Saving Account does not exists" ElseIf !Balance = 0 Then Dim DeleteTranSQL As String DeleteTranSQL = "DELETE FROM Transactions WHERE AcctNum = '" & _ vAcctNbr & "'" SavAcctConn.Object.Execute DeleteTranSQL, , adCmdText .Delete .MoveFirst If Not .BOF Then .Requery End If Else Err.Raise saerrnzbal, "SavAcctDBI", "Saving Account has a non-zero balance. Cannot delete." End If End With Exit SubRemoveError: Err.Raise Err.Number, "SavAcctDBI-Remove", Err.Source & vbCrLf & Err.DescriptionEnd SubPrivate Sub Class_Terminate() Set SavAcctRS = Nothing Set retSavAcct = Nothing Set SavAcctConn = NothingEnd Sub

If not empty database.

Deletes the transactions associated with the account.

Deletes from the SavAcctRS.

Page 19: VB Class with Access Data Please see speaker notes for additional information!

Public Function Item(vKeyIndex As Variant) As SavAcct

On Error GoTo ItemError Update retSavAcct With SavAcctRS If VarType(vKeyIndex) = vbLong Then .Move vKeyIndex, adBookmarkFirst If .EOF Then Set Item = Nothing Else Call SetUpSavAcct Set Item = retSavAcct End If ElseIf VarType(vKeyIndex) = vbString Then .MoveFirst .Find "AcctNum ='" & vKeyIndex & "'" If .EOF Then Set Item = Nothing Err.Raise saerrKeyNF, "SavAcctDBI", "Saving Account does not exist" Else Call SetUpSavAcct Set Item = retSavAcct End If Else Err.Raise saerrInvalidDataType, "SavAcctDBI", "KeyIndex is not a String or Long Data Type" End If End With Exit Function

ItemError: Err.Raise Err.Number, "SavAcctDBI-Item", Err.Source & vbCrLf & Err.Description End Function

Is sent a field that is either a key or an index.

Returns SavAcct object.

Setting up the SavAcct that was asked for by transferring values into a savings account object.

Returns the SavAcct.

Page 20: VB Class with Access Data Please see speaker notes for additional information!

Private Sub Update(vSavAcct As SavAcct)

On Error GoTo UpdateError

If vSavAcct.AccountNumber <> "" Then With SavAcctRS .MoveFirst .Find "AcctNum ='" & vSavAcct.AccountNumber & "'" If .EOF Then Err.Raise saerrKeyNF, "SavAcctDBI-Update", "Saving Account '" & vSavAcct.AccountNumber & "' does not exists" Else !Balance = vSavAcct.Balance !IntRate = vSavAcct.IntRate .Update .Requery End If End With End If

Exit Sub

UpdateError:

If Err.Number <> 91 Then Err.Raise Err.Number, "SavAcctDBI-Update", Err.Source & vbCrLf & Err.Description End If

End Sub

Page 21: VB Class with Access Data Please see speaker notes for additional information!
Page 22: VB Class with Access Data Please see speaker notes for additional information!

Option Explicit

Private SavAcctConn As New SavAcctConnectionPrivate TransactionRS As ADODB.RecordsetPrivate retTransaction As Transaction

Public Enum TransactionDBIErrors trnerrDupKey = vbObjectError + 512 + 2001 trnerrKeyNF = vbObjectError + 512 + 2002 trnerrInvalidDataType = vbObjectError + 512 + 2004End Enum

Private Sub Class_Initialize() Set TransactionRS = New ADODB.RecordsetEnd Sub

Private Sub Class_Terminate() Set TransactionRS = Nothing Set retTransaction = Nothing Set SavAcctConn = NothingEnd Sub

Page 23: VB Class with Access Data Please see speaker notes for additional information!

Public Function Item(ByVal vKeyIndex As Variant) As TransactionOn Error GoTo ItemError

With TransactionRS If VarType(vKeyIndex) = vbLong Then .Move vKeyIndex, adBookmarkFirst If .EOF Then Set Item = Nothing Err.Raise trnerrKeyNF, "TransactionDBI", "Transaction does not exist" Else Call SetUpTran Set Item = retTransaction End If ElseIf VarType(vKeyIndex) = vbString Then .MoveFirst .Find "TranNbr ='" & vKeyIndex & "'" If .EOF Then Set Item = Nothing Err.Raise trnerrKeyNF, "TransactionDBI", "Transaction does not exist" Else Call SetUpTran Set Item = retTransaction End If Else Err.Raise trnerrInvalidDataType, "TransactionDBI", "KeyIndex is not a String or Long Data Type" End If End With Exit FunctionItemError: Err.Raise Err.Number, "TransactionDBI-Item", Err.Source & vbCrLf & Err.DescriptionEnd Function

Page 24: VB Class with Access Data Please see speaker notes for additional information!

Private Sub SetUpTran() Set retTransaction = New Transaction With retTransaction .AccountNumber = TransactionRS!AcctNum .TransactionNumber = TransactionRS!TranNbr .TransactionType = TransactionRS!TranType .TransactionAmount = TransactionRS!TranAmt End WithEnd Sub

Public Function Count() As Long Count = TransactionRS.RecordCountEnd Function

Public Sub Add(ByVal vAcctNbr As String, vTranType As String, vTranAmt As Currency)On Error GoTo AddError

Dim TranNbr As String TranNbr = Right(Format(100000 + Count + 1), 5) With TransactionRS .AddNew !AcctNum = vAcctNbr !TranNbr = TranNbr !TranType = vTranType !TranAmt = vTranAmt .Update End With Exit SubAddError: Err.Raise Err.Number, "TransactionDBI-Add", Err.Source & vbCrLf & Err.DescriptionEnd Sub

Page 25: VB Class with Access Data Please see speaker notes for additional information!

Public Sub LoadRecordSet(vAcctNum As String)

On Error GoTo LoadRecSetError

Dim TransactionsSQL As String TransactionsSQL = "SELECT * FROM Transactions " & _ "WHERE AcctNum = '" & vAcctNum & "' " & _ "ORDER BY TranNbr" With TransactionRS .CursorLocation = adUseClient .CursorType = adOpenDynamic .ActiveConnection = SavAcctConn.Object .LockType = adLockOptimistic .Open TransactionsSQL, , , , adCmdText End With Exit Sub

LoadRecSetError:

Err.Raise Err.Number, "TransactionDBI-LoadRecordSet", Err.Source & vbCrLf & Err.Description

End Sub

Page 26: VB Class with Access Data Please see speaker notes for additional information!
Page 27: VB Class with Access Data Please see speaker notes for additional information!

See Class_Initialize() on next slide.

Page 28: VB Class with Access Data Please see speaker notes for additional information!
Page 29: VB Class with Access Data Please see speaker notes for additional information!
Page 30: VB Class with Access Data Please see speaker notes for additional information!

Item is passed the wkAcctNbr and after processing objSavAcct is set to this account.

Retrieve AccountRetrieve Account

Page 31: VB Class with Access Data Please see speaker notes for additional information!

If you are using the index, you will use this code to get to the first record.

See next slide and return

Advance two slides to see the code and then return.

Page 32: VB Class with Access Data Please see speaker notes for additional information!

Update retSavAcctUsing the record set, we move to the first and then find the account # that matches the account number of the passed savings account.

If the record was found we update the record with the Balance and IntRate that were passed with the savings account and then requery to establish the order.

Page 33: VB Class with Access Data Please see speaker notes for additional information!

Call SetUpSavAcct

See code on next slide.

Page 34: VB Class with Access Data Please see speaker notes for additional information!
Page 35: VB Class with Access Data Please see speaker notes for additional information!

Option ExplicitDim WithEvents objSavAcct As SavAcctDim colSavAccts As SavAcctDBI

Add AccountAdd Account

Page 36: VB Class with Access Data Please see speaker notes for additional information!

Option Explicit

Private SavAcctConn As New SavAcctConnectionPrivate SavAcctRS As ADODB.RecordsetPrivate WithEvents retSavAcct As SavAcct The passed account

number and interest rate are received.

See the update sub on the next slide. (retSavAcct is passed to the Update) - then return to this slide.

Page 37: VB Class with Access Data Please see speaker notes for additional information!

Public Sub Add(ByVal vAcctNbr As String, vIntRate As Integer)On Error GoTo AddError Update retSavAcct Using the record set, we move to the first and then find the

account # that matches the account number of the passed savings account.

If the record was found we update the record with the Balance and IntRate that were passed with the savings account and then requery to establish the order.

Page 38: VB Class with Access Data Please see speaker notes for additional information!

Remove AccountRemove Account

Page 39: VB Class with Access Data Please see speaker notes for additional information!

Receives the account number passed from the remove on the form.

“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 Microsoft VB Help

Page 40: VB Class with Access Data Please see speaker notes for additional information!

Display AccountsDisplay Accounts

Clear is a method associated with the list box, lstDsplyAcct.

Page 41: VB Class with Access Data Please see speaker notes for additional information!

Private SavAcctRS As ADODB.Recordset

From SavAcctDBI

Once I have the objDsplyAcct, I can set up the display line and put it in the list box using AddItem.

Page 42: VB Class with Access Data Please see speaker notes for additional information!

vKeyIndex is vbLong so it is an index.

Page 43: VB Class with Access Data Please see speaker notes for additional information!

retSavAcct is used in Set Item.

Page 44: VB Class with Access Data Please see speaker notes for additional information!

SavingsAccounts

Table

TransactionsTable

DATABASE SAVACCTDBISAVACCTAccountNumberBalanceIntRate

TransactionDBI

Page 45: VB Class with Access Data Please see speaker notes for additional information!

When you select an item in a list box, the ListIndex gets set to the index of that item. I then go to the lstDsplyAcct.List and get the right line using the index and take the first four characters from that line (account numbers are 4 digits).

The user clicked on the 7777 savings account in the savings account list box and immediately the transactions associated with that transaction appeared in the transaction list box.

Page 46: VB Class with Access Data Please see speaker notes for additional information!

The set is because you are passing a reference to an object and the object is colTransactions which has been defined as TransactionDBI.

Page 47: VB Class with Access Data Please see speaker notes for additional information!

With colSavAccts.Item(wkAcctNbr).Transactions

Page 48: VB Class with Access Data Please see speaker notes for additional information!

retSavAcct is used in Set Item.

Page 49: VB Class with Access Data Please see speaker notes for additional information!
Page 50: VB Class with Access Data Please see speaker notes for additional information!
Page 51: VB Class with Access Data Please see speaker notes for additional information!

DepositDeposit

Page 52: VB Class with Access Data Please see speaker notes for additional information!
Page 53: VB Class with Access Data Please see speaker notes for additional information!

colSavAccts.Item(wkAcctNbr.Deposit wkTranAmt handles the deposit.

Page 54: VB Class with Access Data Please see speaker notes for additional information!
Page 55: VB Class with Access Data Please see speaker notes for additional information!

If for example this Item method is executed after the deposit, the update will be done and the deposit will be credited to the account.

Page 56: VB Class with Access Data Please see speaker notes for additional information!

The Update retSavAcct code has been added to fix an error - it does not appear in previous views of this code.

Page 57: VB Class with Access Data Please see speaker notes for additional information!
Page 58: VB Class with Access Data Please see speaker notes for additional information!