Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "VB Class with Access Data Please see speaker notes for additional information!"— Presentation transcript:

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

2 SavAcct07.vbp

3

4 Option Explicit Dim WithEvents objSavAcct As SavAcct Dim colSavAccts As SavAcctDBI Dim wkAcctNbr As String Dim wkBalance As Currency Dim wkIntRate As Integer Dim wkTranAmt As Currency Dim ans As String Private Sub cmdAddAcct_Click() On Error GoTo AddAcctErr colSavAccts.Add wkAcctNbr, wkIntRate Call cmdRetrvAcct_Click Exit Sub AddAcctErr: 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 Sub ApplyIntErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End If End 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.

5 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 Sub DpsitErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End If End Sub

6 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 With End Sub

7 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 If End Sub Transactions references to the Transaction DBI that contains this particular accounts transactions. Private Sub cmdReset_Click() Call Initialize_Variables txtAcctNbr.SetFocus End Sub

8 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 Sub RetrvErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End If End Sub Private Sub cmdRmvAcct_Click() On Error GoTo RmvAcctErr colSavAccts.Remove wkAcctNbr Exit Sub RmvAcctErr: 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 Select End 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.

9 Private Sub cmdSetInt_Click() With txtIntRate.Appearance = 1.BackColor = &H80000005.BorderStyle = 1.Locked = False.SetFocus End With End 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 Sub WthDrwlErr: If Err.Number = saerrKeyNF Then MsgBox "Account does not exist.", vbOKOnly Else MsgBox "Unexpected Error!" & vbCrLf & Err.Number & " " & Err.Description & " " & Err.Source, vbInformation End If End Sub

10 Private Sub Form_Load() Set colSavAccts = New SavAcctDBI Call Initialize_Variables End 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_Click End Sub Private Sub objSavAcct_InsufficientFunds() ans = MsgBox("Insfficient funds to cover withdrawal!", vbOKOnly) End Sub Private Sub txtAcctNbr_LostFocus() wkAcctNbr = txtAcctNbr.Text End Sub Private Sub txtIntRate_LostFocus() With txtIntRate.Appearance = 0.BackColor = &H8000000F.BorderStyle = 0.Locked = True wkIntRate =.Text End With End Sub Database rather than collection!

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

12 Option Explicit Event InsufficientFunds() Private strAcctNbr As String Private curBalance As Currency Private intIntRate As Integer Private colTransactions As TransactionDBI Public Property Get Balance() As Currency Balance = curBalance End Property Public Property Let Balance(ByVal vBalance As Currency) Static BalanceSet As Boolean If Not BalanceSet Then curBalance = vBalance BalanceSet = True End If End Property Public Property Get IntRate() As Integer IntRate = intIntRate End Property Public Property Let IntRate(ByVal vIntRate As Integer) intIntRate = vIntRate End Property We are now dealing with a database rather than a collection.

13 Public Sub WithDrawal(ByVal vTranAmt As Currency) If curBalance < vTranAmt Then RaiseEvent InsufficientFunds Else curBalance = curBalance - vTranAmt colTransactions.Add strAcctNbr, "WTH", vTranAmt End If End 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 = strAcctNbr End Property Public Property Let AccountNumber(ByVal vAcctNbr As String) Static AcctNbrSet As Boolean If Not AcctNbrSet Then strAcctNbr = vAcctNbr AcctNbrSet = True End If End Property

14 Public Sub PostInterest() Dim IntAmt As Currency IntAmt = curBalance * (IntRate / 100) curBalance = curBalance + IntAmt colTransactions.Add strAcctNbr, "INT", IntAmt End Sub Private Sub Class_Initialize() Set colTransactions = New TransactionDBI ' MsgBox "Calling # = " & strAcctNbr,, "SavAcctClass" End Sub Public Sub LoadTransactions() colTransactions.LoadRecordSet strAcctNbr End Sub Public Property Get Transactions() As TransactionDBI Set Transactions = colTransactions End 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.

15 Returns a reference to the connection.

16 Option Explicit Private SavAcctConn As New SavAcctConnection Private SavAcctRS As ADODB.Recordset Private WithEvents retSavAcct As SavAcct Public Enum SavAcctDBIErrors saerrDupKey = vbObjectError + 512 + 1001 saerrKeyNF = vbObjectError + 512 + 1002 saerrnzbal = vbObjectError + 512 + 1003 saerrInvalidDataType = vbObjectError + 512 + 1004 End 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 With Exit Sub ClassInitError: Err.Raise Err.Number, "SavAcctDBI-Class_Initialize", Err.Source & vbCrLf & Err.Description End Sub See SavAcctConnection for this.

17 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 Sub SetUpSavAcctError: Err.Raise Err.Number, "SavAcctDBI-SetUpSavAcct", Err.Source & vbCrLf & Err.Description End Sub Public Function Count() As Long Count = SavAcctRS.RecordCount End 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.Description End 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).

18 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 Sub RemoveError: Err.Raise Err.Number, "SavAcctDBI-Remove", Err.Source & vbCrLf & Err.Description End Sub Private Sub Class_Terminate() Set SavAcctRS = Nothing Set retSavAcct = Nothing Set SavAcctConn = Nothing End Sub If not empty database. Deletes the transactions associated with the account. Deletes from the SavAcctRS.

19 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.

20 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

21

22 Option Explicit Private SavAcctConn As New SavAcctConnection Private TransactionRS As ADODB.Recordset Private retTransaction As Transaction Public Enum TransactionDBIErrors trnerrDupKey = vbObjectError + 512 + 2001 trnerrKeyNF = vbObjectError + 512 + 2002 trnerrInvalidDataType = vbObjectError + 512 + 2004 End Enum Private Sub Class_Initialize() Set TransactionRS = New ADODB.Recordset End Sub Private Sub Class_Terminate() Set TransactionRS = Nothing Set retTransaction = Nothing Set SavAcctConn = Nothing End Sub

23 Public Function Item(ByVal vKeyIndex As Variant) As Transaction On 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 Function ItemError: Err.Raise Err.Number, "TransactionDBI-Item", Err.Source & vbCrLf & Err.Description End Function

24 Private Sub SetUpTran() Set retTransaction = New Transaction With retTransaction.AccountNumber = TransactionRS!AcctNum.TransactionNumber = TransactionRS!TranNbr.TransactionType = TransactionRS!TranType.TransactionAmount = TransactionRS!TranAmt End With End Sub Public Function Count() As Long Count = TransactionRS.RecordCount End 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 Sub AddError: Err.Raise Err.Number, "TransactionDBI-Add", Err.Source & vbCrLf & Err.Description End Sub

25 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

26

27 See Class_Initialize() on next slide.

28

29

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

31 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.

32 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.

33 Call SetUpSavAcct See code on next slide.

34

35 Option Explicit Dim WithEvents objSavAcct As SavAcct Dim colSavAccts As SavAcctDBI Add Account

36 Option Explicit Private SavAcctConn As New SavAcctConnection Private SavAcctRS As ADODB.Recordset Private 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.

37 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.

38 Remove Account

39 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

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

41 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.

42 vKeyIndex is vbLong so it is an index.

43 retSavAcct is used in Set Item.

44 Savings Accounts Table Transactions Table DATABASE SAVACCTDBI SAVACCT AccountNumber Balance IntRate TransactionDBI

45 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.

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

47 With colSavAccts.Item(wkAcctNbr).Transactions

48 retSavAcct is used in Set Item.

49

50

51 Deposit

52

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

54

55 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.

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

57

58


Download ppt "VB Class with Access Data Please see speaker notes for additional information!"

Similar presentations


Ads by Google