home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 February / CHIP_2_98.iso / software / pelne / optionp / mts4.cab / Account.VB_Step5_account.cls < prev    next >
Text File  |  1997-11-14  |  3KB  |  115 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. END
  5. Attribute VB_Name = "Account"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = True
  10. ' Filename: Account.vbp
  11. '
  12. ' Description:  Account Class
  13. '
  14. ' This file is provided as part of the Microsoft Transaction Server Samples
  15. '
  16. ' THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT
  17. ' WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED,
  18. ' INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
  19. ' OF MERCHANTABILITY AND/OR FITNESS FOR A  PARTICULAR
  20. ' PURPOSE.
  21. '
  22. ' Copyright (C) 1997 Microsoft Corporation, All rights reserved
  23.  
  24. Option Explicit
  25.  
  26. Private Const ERROR_NUMBER = vbObjectError + 0
  27. Private Const APP_ERROR = -2147467008
  28. Private Const strConnect = "FILEDSN=MTSSamples"
  29.  
  30. Public Function Post(ByVal lngAccountNo As Long, ByVal lngAmount As Long) As String
  31.     
  32.     Dim strResult As String
  33.     
  34.     On Error GoTo ErrorHandler
  35.     
  36.     ' obtain the ADO environment and connection
  37.     Dim adoConn As New ADODB.Connection
  38.     Dim varRows As Variant
  39.     
  40.     adoConn.Open strConnect
  41.    
  42.     On Error GoTo ErrorCreateTable
  43.  
  44.     ' update the balance
  45.     Dim strSQL As String
  46.     strSQL = "UPDATE Account SET Balance = Balance + " + Str$(lngAmount) + " WHERE AccountNo = " + Str$(lngAccountNo)
  47.     
  48. TryAgain:
  49.     adoConn.Execute strSQL, varRows
  50.  
  51.     ' if anything else happens
  52.     On Error GoTo ErrorHandler
  53.   
  54.     ' get resulting balance which may have been further updated via triggers
  55.     strSQL = "SELECT Balance FROM Account WHERE AccountNo = " + Str$(lngAccountNo)
  56.     
  57.     Dim adoRS As ADODB.Recordset
  58.     Set adoRS = adoConn.Execute(strSQL)
  59.     If adoRS.EOF Then
  60.         Err.Raise Number:=APP_ERROR, Description:="Error. Account " + Str$(lngAccountNo) + " not on file."
  61.     End If
  62.  
  63.     Dim lngBalance As Long
  64.     lngBalance = adoRS.Fields("Balance").Value
  65.     
  66.     ' check if account is overdrawn
  67.     If (lngBalance) < 0 Then
  68.         Err.Raise Number:=APP_ERROR, Description:="Error. Account " + Str$(lngAccountNo) + " would be overdrawn by " + Str$(lngBalance) + ". Balance is still " + Str$(lngBalance - lngAmount) + "."
  69.     Else
  70.         If lngAmount < 0 Then
  71.             strResult = strResult & "Debit from account " & lngAccountNo & ", "
  72.         Else
  73.             strResult = strResult & "Credit to account " & lngAccountNo & ", "
  74.         End If
  75.         strResult = strResult + "balance is $" & Str$(lngBalance) & ". (VB)"
  76.     End If
  77.  
  78.     ' cleanup
  79.     Set adoRS = Nothing
  80.     Set adoConn = Nothing
  81.     
  82.     GetObjectContext.SetComplete          ' we are finished and happy
  83.     
  84.     Post = strResult
  85.     
  86.  Exit Function
  87.  
  88. ErrorCreateTable:
  89.     On Error GoTo ErrorHandler
  90.     
  91.     ' create the account table
  92.     Dim objCreateTable As CreateTable
  93.     Set objCreateTable = GetObjectContext.CreateInstance("Bank.CreateTable")
  94.     objCreateTable.CreateAccount
  95.       
  96.     GoTo TryAgain
  97.  
  98. ErrorHandler:
  99.     ' cleanup
  100.     If Not adoRS Is Nothing Then
  101.         Set adoRS = Nothing
  102.     End If
  103.     If Not adoConn Is Nothing Then
  104.         Set adoConn = Nothing
  105.     End If
  106.     
  107.     GetObjectContext.SetAbort              ' we are unhappy
  108.     
  109.     Post = ""                       ' indicate that an error occurred
  110.     
  111.     Err.Raise Err.Number, "Bank.Accout.Post", Err.Description
  112.     
  113. End Function
  114.  
  115.