home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / source / chap14 / vbu1402.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-10-07  |  6.8 KB  |  242 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. Attribute VB_Name = "Form1"
  38. Attribute VB_Creatable = False
  39. Attribute VB_Exposed = False
  40. Option Explicit
  41. Private Sub Command1_Click()
  42.     UpdatePrices ' run price update
  43. End Sub
  44. Private Sub Data1_Error(DataErr As Integer, Response As Integer)
  45.     If DataErr = 3196 Or DataErr = 3045 Then
  46.         MsgBox "Database Locked for Maintenance - Try again later", vbCritical, "Open DB"
  47.     Else
  48.         MsgBox Error$, vbCritical, "Error " + Str(Err)
  49.         Unload Me
  50.         End
  51.     End If
  52. End Sub
  53. Private Sub Form_Load()
  54.     Dim db As Database
  55.     '
  56.     ' open db exclusively
  57.     On Error GoTo FormLoadErr
  58.     '
  59.     'Set db = OpenDatabase("ch1402.mdb", True)
  60.     'MsgBox "Database Opened Successfully", vbInformation, "Open DB"
  61.     '
  62.     GoTo FormLoadExit
  63.     '
  64. FormLoadErr:
  65.     If Err = 3196 Or Err = 3045 Then
  66.         MsgBox "Database Locked for Maintenance - Try again later", vbCritical, "Open DB"
  67.     Else
  68.         MsgBox Error$, vbCritical, "Error " + Str(Err)
  69.     End If
  70.     Unload Me
  71.     End
  72.     '
  73. FormLoadExit:
  74.     '
  75. End Sub
  76. Public Sub UpdatePrices()
  77.     '
  78.     ' update all inventory wholesale prices
  79.     '
  80.     On Error GoTo UpdatePricesErr
  81.     '
  82.     Dim dbFile As Database
  83.     Dim rsTable As Recordset
  84.     '
  85.     ' open db
  86.     Set dbFile = DBEngine.OpenDatabase("ch1402.mdb")
  87.     '
  88.     ' open table exclusively
  89.     Set rsTable = dbFile.OpenRecordset("InvPrice", dbOpenTable, dbDenyRead)
  90.     '
  91.     ' attempt mass update
  92.     'dbFile.Execute "UPDATE InvPrice SET WholesalePrice = WholesalePrice * 1.05;"
  93.     '
  94.     ' perform mass update
  95.     '
  96.     On Error Resume Next    ' ignore errors
  97.     Workspaces(0).BeginTrans ' start trans tracking
  98.     While Not rsTable.EOF   ' for every row
  99.         rsTable.Edit        ' start edit
  100.         rsTable.Fields("WholesalePrice") = rsTable.Fields("WholesalePrice") * 1.05
  101.         rsTable.Update      ' end edit
  102.     Wend                    ' get another
  103.     If Err = 0 Then         ' no errors
  104.         Workspaces(0).CommitTrans    ' final update
  105.         MsgBox "Wholesale Prices Updated", vbInformation, "Inventory"
  106.     Else                    ' trouble
  107.         Workspaces(0).Rollback   ' undo all edits
  108.         MsgBox "Wholesale Price Update Failed", vbCritical, "Error " + Str(Err)
  109.     End If
  110.     On Error GoTo 0         ' tell me about errors!
  111.     '
  112.     '
  113.     GoTo UpdatePricesExit
  114.     '
  115. UpdatePricesErr:
  116.     MsgBox Error$, vbCritical, "UpdatePrices Error " + Str(Err)
  117.     '
  118. UpdatePricesExit:
  119.     On Error Resume Next
  120.     dbFile.Close
  121.     On Error GoTo 0
  122. End Sub
  123. Public Sub PageLockErrors(LockErr As Integer)
  124.     '
  125.     ' handle errors due to page locks
  126.     '
  127.     Static nTries As Integer
  128.     Dim nMaxTries As Integer
  129.     Dim nLoop As Integer
  130.     Dim nTemp As Integer
  131.     '
  132.     nMaxTries = 3
  133.     '
  134.     Select Case LockErr
  135.         Case 3197
  136.             ' data changed
  137.             'Edit    ' re-read changed record
  138.         Case 3260, 3816
  139.             ' currently locked
  140.             nTries = nTries + 1 ' try again
  141.             If nTries > nMaxTries Then  ' too manytimes?
  142.                 nTemp = MsgBox(Error, vbRetryCancel, "Error " + Str(Err))
  143.                 If nTemp = vbRetry Then ' user said try again
  144.                     nTries = 1  ' start all over
  145.                 End If
  146.             End If
  147.     End Select
  148.     '
  149.     DBEngine.Idle  ' free up old locks
  150.     DoEvents            ' let messages catch up
  151.     '
  152.     For nLoop = 1 To 2000 ' 2000 millisecond pause
  153.         ' empty loop
  154.     Next nLoop
  155.     Resume
  156.     '
  157. End Sub
  158. Public Function CheckLockTable(cTable$, cRecId$) As Boolean
  159.     '
  160.     ' check lock table to see if
  161.     ' we can edit this rec
  162.     '
  163.     rsLockTable.FindFirst "TableName=" + cTable$ + " and RecordID=" + cRecId$
  164.     If rsLockTable.NoMatch = False Then
  165.         MsgBox cTable$ + "." + cRecId$ + " is Locked - Try again later.", vbCritical, "CheckLockTable"
  166.         CheckLockTable = False
  167.     Else
  168.         CheckLockTable = True
  169.     End If
  170.     '
  171. End Function
  172. Public Sub EditRec()
  173.     '
  174.     ' check locks, then edit
  175.     '
  176.     Dim cTable As String
  177.     Dim cRecId As String
  178.     '
  179.     cTable = rsEditTable.Name ' get table to lock
  180.     cRecId = rsEditTable.Field(0) ' first field is primary key
  181.     '
  182.     If CheckLockTable(cTable, cRecId) = True Then
  183.         rsEditTable.Edit
  184.         '
  185.         ' perform edits
  186.         '
  187.         rsEditTable.udpate
  188.         ClearLock cTable, cRecId
  189.     End If
  190. End Sub
  191. Public Sub ClearLock(cTable, cRecId)
  192.     '
  193.     ' remove rec form lock table
  194.     '
  195.     rsLockTable.FindFirst "TableName=" + cTable + " and REcordID=" + cRecId
  196.     If rsLockTable.nomath = False Then
  197.         rsLockTable.Delete
  198.     End If
  199.     '
  200. End Sub
  201. Public Sub SQLProcess()
  202.     '
  203.     ' peform updates via SQL statements
  204.     '
  205.     Dim db As Database
  206.     Dim rs As Recordset
  207.     Dim cSQL As String
  208.     '
  209.     Set db = DBEngine.OpenDatabase("ch1402.mdb")
  210.     Set rs = db.OpenRecordset("Table1", dbOpenSnapshot)
  211.     '
  212.     ' add new record
  213.     cSQL = "INSERT INTO Table1 VALUES('MCA','Weather Ballon','FirstClass','RoundTrip');"
  214.     db.Execute cSQL
  215.     '
  216.     db.Close
  217.     '
  218.  End Sub
  219. Public Sub BatchUpdate()
  220.     '
  221.     ' provide temporary table
  222.     ' for batch loading of
  223.     ' master table
  224.     '
  225.     Dim dbFile As Database
  226.     Dim rsTable As Recordset
  227.     Dim cSQL As String
  228.     '
  229.     Set dbFile = DBEngine.OpenDatabase("ch1402.mdb")
  230.     '
  231.     cSQL = "SELECT InvTrans.* INTO MyTrans SELECT * FROM InvTrans WHERE InvID='JUNK';"
  232.     dbFile.Execute cSQL
  233.     '
  234.     Set rsTable = dbFile.OpenRecordset("MyTrans", dbOpenTable)
  235.     '
  236.     ' allow user to peform batch processing on local table
  237.     '
  238.     cSQL = "INSERT * INTO InvTrans FROM MyTrans;"
  239.     dbFile.Execute cSQL
  240.     '
  241. End Sub
  242.