home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap14 / vbu1402.frm < prev    next >
Text File  |  1995-10-07  |  7KB  |  256 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   4140
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1512
  7.    ClientWidth     =   6696
  8.    Height          =   4524
  9.    Left            =   1092
  10.    LinkTopic       =   "Form1"
  11.    ScaleHeight     =   4140
  12.    ScaleWidth      =   6696
  13.    Top             =   1176
  14.    Width           =   6792
  15.    Begin VB.CommandButton Command1 
  16.       Caption         =   "&Update Prices"
  17.       Height          =   495
  18.       Left            =   360
  19.       TabIndex        =   0
  20.       Top             =   840
  21.       Width           =   1215
  22.    End
  23.    Begin VB.Data Data1 
  24.       Caption         =   "Data1"
  25.       Connect         =   "Access"
  26.       DatabaseName    =   "Ch1402.mdb"
  27.       Exclusive       =   0   'False
  28.       Height          =   300
  29.       Left            =   720
  30.       Options         =   2
  31.       ReadOnly        =   0   'False
  32.       RecordsetType   =   1  'Dynaset
  33.       RecordSource    =   "InvPrice"
  34.       Top             =   3000
  35.       Width           =   3375
  36.    End
  37. End
  38. Attribute VB_Name = "Form1"
  39. Attribute VB_Creatable = False
  40. Attribute VB_Exposed = False
  41. Option Explicit
  42.  
  43. Private Sub Command1_Click()
  44.     UpdatePrices ' run price update
  45. End Sub
  46.  
  47.  
  48. Private Sub Data1_Error(DataErr As Integer, Response As Integer)
  49.     If DataErr = 3196 Or DataErr = 3045 Then
  50.         MsgBox "Database Locked for Maintenance - Try again later", vbCritical, "Open DB"
  51.     Else
  52.         MsgBox Error$, vbCritical, "Error " + Str(Err)
  53.         Unload Me
  54.         End
  55.     End If
  56. End Sub
  57.  
  58. Private Sub Form_Load()
  59.     Dim db As Database
  60.     '
  61.     ' open db exclusively
  62.     On Error GoTo FormLoadErr
  63.     '
  64.     'Set db = OpenDatabase("ch1402.mdb", True)
  65.     'MsgBox "Database Opened Successfully", vbInformation, "Open DB"
  66.     '
  67.     GoTo FormLoadExit
  68.     '
  69. FormLoadErr:
  70.     If Err = 3196 Or Err = 3045 Then
  71.         MsgBox "Database Locked for Maintenance - Try again later", vbCritical, "Open DB"
  72.     Else
  73.         MsgBox Error$, vbCritical, "Error " + Str(Err)
  74.     End If
  75.     Unload Me
  76.     End
  77.     '
  78. FormLoadExit:
  79.     '
  80. End Sub
  81.  
  82.  
  83.  
  84. Public Sub UpdatePrices()
  85.     '
  86.     ' update all inventory wholesale prices
  87.     '
  88.     On Error GoTo UpdatePricesErr
  89.     '
  90.     Dim dbFile As Database
  91.     Dim rsTable As Recordset
  92.     '
  93.     ' open db
  94.     Set dbFile = DBEngine.OpenDatabase("ch1402.mdb")
  95.     '
  96.     ' open table exclusively
  97.     Set rsTable = dbFile.OpenRecordset("InvPrice", dbOpenTable, dbDenyRead)
  98.     '
  99.     ' attempt mass update
  100.     'dbFile.Execute "UPDATE InvPrice SET WholesalePrice = WholesalePrice * 1.05;"
  101.     '
  102.     ' perform mass update
  103.     '
  104.     On Error Resume Next    ' ignore errors
  105.     Workspaces(0).BeginTrans ' start trans tracking
  106.     While Not rsTable.EOF   ' for every row
  107.         rsTable.Edit        ' start edit
  108.         rsTable.Fields("WholesalePrice") = rsTable.Fields("WholesalePrice") * 1.05
  109.         rsTable.Update      ' end edit
  110.     Wend                    ' get another
  111.     If Err = 0 Then         ' no errors
  112.         Workspaces(0).CommitTrans    ' final update
  113.         MsgBox "Wholesale Prices Updated", vbInformation, "Inventory"
  114.     Else                    ' trouble
  115.         Workspaces(0).Rollback   ' undo all edits
  116.         MsgBox "Wholesale Price Update Failed", vbCritical, "Error " + Str(Err)
  117.     End If
  118.     On Error GoTo 0         ' tell me about errors!
  119.     '
  120.     '
  121.     GoTo UpdatePricesExit
  122.     '
  123. UpdatePricesErr:
  124.     MsgBox Error$, vbCritical, "UpdatePrices Error " + Str(Err)
  125.     '
  126. UpdatePricesExit:
  127.     On Error Resume Next
  128.     dbFile.Close
  129.     On Error GoTo 0
  130. End Sub
  131.  
  132. Public Sub PageLockErrors(LockErr As Integer)
  133.     '
  134.     ' handle errors due to page locks
  135.     '
  136.     Static nTries As Integer
  137.     Dim nMaxTries As Integer
  138.     Dim nLoop As Integer
  139.     Dim nTemp As Integer
  140.     '
  141.     nMaxTries = 3
  142.     '
  143.     Select Case LockErr
  144.         Case 3197
  145.             ' data changed
  146.             'Edit    ' re-read changed record
  147.         Case 3260, 3816
  148.             ' currently locked
  149.             nTries = nTries + 1 ' try again
  150.             If nTries > nMaxTries Then  ' too manytimes?
  151.                 nTemp = MsgBox(Error, vbRetryCancel, "Error " + Str(Err))
  152.                 If nTemp = vbRetry Then ' user said try again
  153.                     nTries = 1  ' start all over
  154.                 End If
  155.             End If
  156.     End Select
  157.     '
  158.     DBEngine.Idle  ' free up old locks
  159.     DoEvents            ' let messages catch up
  160.     '
  161.     For nLoop = 1 To 2000 ' 2000 millisecond pause
  162.         ' empty loop
  163.     Next nLoop
  164.     Resume
  165.     '
  166. End Sub
  167.  
  168. Public Function CheckLockTable(cTable$, cRecId$) As Boolean
  169.     '
  170.     ' check lock table to see if
  171.     ' we can edit this rec
  172.     '
  173.     rsLockTable.FindFirst "TableName=" + cTable$ + " and RecordID=" + cRecId$
  174.     If rsLockTable.NoMatch = False Then
  175.         MsgBox cTable$ + "." + cRecId$ + " is Locked - Try again later.", vbCritical, "CheckLockTable"
  176.         CheckLockTable = False
  177.     Else
  178.         CheckLockTable = True
  179.     End If
  180.     '
  181. End Function
  182.  
  183. Public Sub EditRec()
  184.     '
  185.     ' check locks, then edit
  186.     '
  187.     Dim cTable As String
  188.     Dim cRecId As String
  189.     '
  190.     cTable = rsEditTable.Name ' get table to lock
  191.     cRecId = rsEditTable.Field(0) ' first field is primary key
  192.     '
  193.     If CheckLockTable(cTable, cRecId) = True Then
  194.         rsEditTable.Edit
  195.         '
  196.         ' perform edits
  197.         '
  198.         rsEditTable.udpate
  199.         ClearLock cTable, cRecId
  200.     End If
  201. End Sub
  202.  
  203. Public Sub ClearLock(cTable, cRecId)
  204.     '
  205.     ' remove rec form lock table
  206.     '
  207.     rsLockTable.FindFirst "TableName=" + cTable + " and REcordID=" + cRecId
  208.     If rsLockTable.nomath = False Then
  209.         rsLockTable.Delete
  210.     End If
  211.     '
  212. End Sub
  213.  
  214. Public Sub SQLProcess()
  215.     '
  216.     ' peform updates via SQL statements
  217.     '
  218.     Dim db As Database
  219.     Dim rs As Recordset
  220.     Dim cSQL As String
  221.     '
  222.     Set db = DBEngine.OpenDatabase("ch1402.mdb")
  223.     Set rs = db.OpenRecordset("Table1", dbOpenSnapshot)
  224.     '
  225.     ' add new record
  226.     cSQL = "INSERT INTO Table1 VALUES('MCA','Weather Ballon','FirstClass','RoundTrip');"
  227.     db.Execute cSQL
  228.     '
  229.     db.Close
  230.     '
  231.  End Sub
  232.  
  233. Public Sub BatchUpdate()
  234.     '
  235.     ' provide temporary table
  236.     ' for batch loading of
  237.     ' master table
  238.     '
  239.     Dim dbFile As Database
  240.     Dim rsTable As Recordset
  241.     Dim cSQL As String
  242.     '
  243.     Set dbFile = DBEngine.OpenDatabase("ch1402.mdb")
  244.     '
  245.     cSQL = "SELECT InvTrans.* INTO MyTrans SELECT * FROM InvTrans WHERE InvID='JUNK';"
  246.     dbFile.Execute cSQL
  247.     '
  248.     Set rsTable = dbFile.OpenRecordset("MyTrans", dbOpenTable)
  249.     '
  250.     ' allow user to peform batch processing on local table
  251.     '
  252.     cSQL = "INSERT * INTO InvTrans FROM MyTrans;"
  253.     dbFile.Execute cSQL
  254.     '
  255. End Sub
  256.