home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Unleashed / Visual_Basic_4_Unleashed_SAMS_Publishing_1995.iso / truegrid / disk1 / tablevw / tablevw.$ / TABLEVW.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-02-17  |  10.8 KB  |  336 lines

  1. VERSION 2.00
  2. Begin Form TableForm 
  3.    BackColor       =   &H00808080&
  4.    Caption         =   "View Table Sample"
  5.    ClientHeight    =   3150
  6.    ClientLeft      =   1740
  7.    ClientTop       =   2100
  8.    ClientWidth     =   6090
  9.    Height          =   3840
  10.    Icon            =   TABLEVW.FRX:0000
  11.    Left            =   1680
  12.    LinkTopic       =   "Form1"
  13.    ScaleHeight     =   3150
  14.    ScaleWidth      =   6090
  15.    Top             =   1470
  16.    Width           =   6210
  17.    Begin TrueGrid Table1 
  18.       AllowArrows     =   -1  'True
  19.       AllowTabs       =   -1  'True
  20.       BackColor       =   &H00C0C0C0&
  21.       Editable        =   -1  'True
  22.       EditDropDown    =   -1  'True
  23.       ExposeCellMode  =   0  'Expose upon selection
  24.       FetchMode       =   0  'By cell
  25.       FontBold        =   0   'False
  26.       FontItalic      =   0   'False
  27.       FontName        =   "MS Sans Serif"
  28.       FontSize        =   8.25
  29.       FontStrikethru  =   0   'False
  30.       FontUnderline   =   0   'False
  31.       HeadingHeight   =   1
  32.       Height          =   1815
  33.       HorzLines       =   0  'None
  34.       Layout          =   TABLEVW.FRX:0302
  35.       LayoutIndex     =   1
  36.       Left            =   120
  37.       LinesPerRow     =   1
  38.       MarqueeUnique   =   -1  'True
  39.       SplitPropsGlobal=   -1  'True
  40.       SplitTabMode    =   0  'Don't tab across splits
  41.       TabCapture      =   0   'False
  42.       TabIndex        =   0
  43.       Top             =   120
  44.       UseBookmarks    =   -1  'True
  45.       Width           =   2775
  46.       WrapCellPointer =   0   'False
  47.    End
  48.    Begin Menu ExitMenuOption 
  49.       Caption         =   "E&xit!"
  50.    End
  51.    Begin Menu IndexMenuOption 
  52.       Caption         =   "&Indexes"
  53.       Visible         =   0   'False
  54.       Begin Menu IndexMenu 
  55.          Index           =   0
  56.       End
  57.    End
  58.    Begin Menu HelpMenuOption 
  59.       Caption         =   "&Help"
  60.       Begin Menu HelpMenu 
  61.          Caption         =   "&Index"
  62.          Index           =   0
  63.       End
  64.       Begin Menu HelpMenu 
  65.          Caption         =   "&Using Help"
  66.          Index           =   1
  67.       End
  68.       Begin Menu HelpMenu 
  69.          Caption         =   "-"
  70.          Index           =   2
  71.       End
  72.       Begin Menu HelpMenu 
  73.          Caption         =   "&About View Table..."
  74.          Index           =   3
  75.       End
  76.    End
  77. ' Global values for Database and Table
  78. Dim Db As Database
  79. Dim Tb As Table
  80. 'Global constants that determine the size
  81. 'of the grid and it's current status
  82. Const MAXROW = 1000
  83. Dim CurrentRow As Long
  84. Dim EndRow As Long
  85. Sub CenterForm (F As Form)
  86. ' Center the specified form within the screen
  87.     F.Move (Screen.Width - F.Width) \ 2, (Screen.Height - F.Height) \ 2
  88. End Sub
  89. Sub CheckForIndexes ()
  90.     ' If Indexes exist then show Index menu option
  91.     If Tb.Indexes.Count > 0 Then
  92.         IndexMenuOption.Visible = True
  93.         IndexMenu(0).Visible = True
  94.         IndexMenu(0).Checked = True
  95.         IndexMenu(0).Caption = "&None"
  96.         
  97.         ' Add Index menu option for each index
  98.         For ct = 0 To Tb.Indexes.Count - 1
  99.             Load IndexMenu(ct + 1)
  100.             IndexMenu(ct + 1).Caption = Tb.Indexes(ct)
  101.             IndexMenu(ct + 1).Checked = False
  102.         Next ct
  103.     End If
  104. End Sub
  105. Sub ExitApp ()
  106.     ' Close database and table before exiting
  107.     Tb.Close
  108.     Db.Close
  109.     End
  110. End Sub
  111. Sub ExitMenuOption_Click ()
  112.     Unload Me
  113. End Sub
  114. Sub FieldLayout ()
  115.     ' Get Field Layout to determine field display
  116.     ' and data entry size
  117.     For ct = 0 To Tb.Fields.Count - 1
  118.         
  119.         'Set display heading to database fieldname
  120.         FldName = Tb.Fields(ct).Name
  121.         Table1.ColumnName(ct + 1) = FldName
  122.         
  123.         'Get width of fieldname
  124.         NameWidth = Len(FldName)
  125.         'Get type of field to determine it's display size
  126.         Select Case Tb.Fields(ct).Type
  127.             Case 1, 10      'Text and Logic types
  128.                 FldSize = Tb.Fields(ct).Size
  129.             Case 3          'Integer type
  130.                 FldSize = 7
  131.             Case 4, 8       'Long and date types
  132.                 FldSize = 14
  133.             Case 5, 6, 7    'Currency, Single, Double types
  134.                 FldSize = 10
  135.             Case 11, 12     'Memo and binary types
  136.                 FldSize = 25
  137.         End Select
  138.         ' Use field width or the field name width whichever is larger
  139.         If NameWidth > FldSize Then
  140.             Table1.ColumnWidth(ct + 1) = NameWidth + 2
  141.         Else
  142.             Table1.ColumnWidth(ct + 1) = FldSize + 2
  143.         End If
  144.         ' Set data entry width to Field size
  145.         Table1.ColumnSize(ct + 1) = FldSize
  146.     Next ct
  147. End Sub
  148. Sub Form_Load ()
  149.     'Center the sample on the screen
  150.     CenterForm TableForm
  151.     ' Open Database and Table functions
  152.     OpenDb ("market.mdb")
  153.     OpenTb ("Contact Information")
  154.     ' Estimate begining size, put approx size in MAXROW
  155.     EndRow = MAXROW
  156.     ' Set grid Rows to estimated MAXROW
  157.     Table1.Rows = MAXROW
  158.     ' Set Current Row to one
  159.     Temp = MoveToRow(1)
  160.     ' Function to add indexes to the menu if any exist
  161.     CheckForIndexes
  162.     ' Function to setup grids columns
  163.     FieldLayout
  164. End Sub
  165. Sub Form_Resize ()
  166.     'Make the grid to the size of the form
  167.     Table1.Move 0, 0, ScaleWidth, ScaleHeight
  168. End Sub
  169. Sub Form_Unload (Cancel As Integer)
  170.     ExitApp
  171. End Sub
  172. Sub HelpMenu_Click (Index As Integer)
  173.     'This event calls the WinHelp EXE and a location to goto based on which selection the user has chosen
  174.     'Case 4 shows the about box for the Callback sample
  175.     Select Case Index
  176.         Case 0
  177.             HelpContext TableForm, HELP_VIEWTABLE
  178.         Case 1
  179.             HelpOnHelp TableForm
  180.         Case 3
  181.             About.Show 1
  182.     End Select
  183. End Sub
  184. Sub IndexMenu_Click (Index As Integer)
  185.   If IndexMenu(Index).Checked <> True Then
  186.     ' Set Index to whichever one the user chooses
  187.     Select Case Index
  188.         Case 0
  189.             SetIndex ("")
  190.         Case Else
  191.             SetIndex (IndexMenu(Index).Caption)
  192.     End Select
  193.     ' Refresh grid, move to beginning, reset table row
  194.     Table1.Refresh
  195.     Table1.RowIndex = 1
  196.     Temp = MoveToRow(1)
  197.     ' Turn off all check marks
  198.     For ct = 0 To Tb.Indexes.Count
  199.         IndexMenu(ct).Checked = False
  200.     Next ct
  201.     ' Check value user choose
  202.     IndexMenu(Index).Checked = True
  203.   End If
  204. End Sub
  205. Function MoveToRow (NewRow As Long) As Long
  206. Dim CurDiff, EndDiff, BeginDiff As Long
  207.     ' Find differences between beginning, end and current position
  208.     CurDiff = Abs(CurrentRow - NewRow)
  209.     EndDiff = EndRow - NewRow
  210.     BeginDiff = NewRow - 1
  211.     ' If values are same no need to move db
  212.     If CurrentRow = NewRow Then
  213.         MoveToRow = CurrentRow
  214.         Exit Function
  215.     ' If moving forward in db
  216.     ElseIf CurrentRow < NewRow Then
  217.         ' Check to see if End is closer, if not
  218.         ' move from current position to new position
  219.         If EndDiff > CurDiff Then
  220.             For ct = 1 To CurDiff
  221.                 Tb.MoveNext
  222.                 If Tb.EOF Then
  223.                     CurrentRow = Tb.RecordCount
  224.                     MoveToRow = CurrentRow
  225.                     Exit Function
  226.                 Else
  227.                     CurrentRow = CurrentRow + 1
  228.                 End If
  229.             Next ct
  230.         
  231.         ' If end is closer move to the end of the database
  232.         ' and go backwards to the new position
  233.         Else
  234.             Tb.MoveLast
  235.             CurrentRow = Tb.RecordCount
  236.             
  237.             'Check to see if estimated equal actual, if not equal
  238.             'exit function so CheckRows can set the actual EndRow value
  239.             If EndRow = Tb.RecordCount Then
  240.                 For ct = 1 To EndDiff
  241.                     Tb.MovePrevious
  242.                     CurrentRow = CurrentRow - 1
  243.                 Next ct
  244.             End If
  245.         End If
  246.     ' Moving backward in db
  247.     Else
  248.         ' If BeginDiff is greater than CurDiff then move
  249.         ' from current position to new position
  250.         If BeginDiff > CurDiff Then
  251.             For ct = 1 To CurDiff
  252.                 Tb.MovePrevious
  253.                 If Tb.BOF Then
  254.                     CurrentRow = 1
  255.                     MoveToRow = CurrentRow
  256.                     Exit Function
  257.                 Else
  258.                     CurrentRow = CurrentRow - 1
  259.                 End If
  260.             Next ct
  261.         
  262.         ' If beginning is closer then move from
  263.         ' beginning to new position
  264.         Else
  265.             Tb.MoveFirst
  266.             CurrentRow = 1
  267.             For ct = 1 To BeginDiff
  268.                 Tb.MoveNext
  269.                 CurrentRow = CurrentRow + 1
  270.             Next ct
  271.         End If
  272.     End If
  273.     MoveToRow = CurrentRow
  274. End Function
  275. Sub OpenDb (DbName As String)
  276.     ' Put your open database code here
  277.     ChDir App.Path
  278.     Set Db = OpenDatabase(DbName)
  279. End Sub
  280. Sub OpenTb (TableName As String)
  281.     ' Put your open table code here
  282.     Set Tb = Db.OpenTable(TableName)
  283. End Sub
  284. Sub SetIndex (IndexVal As String)
  285.     ' If you database type supports multiple indexes
  286.     ' set the index type you want to use here
  287.     Tb.Index = IndexVal
  288. End Sub
  289. Sub Table1_CheckRows (RequestRows As Long, CurRows As Long)
  290.     ' Move in table to value specified by RequestRows
  291.     NewRow = MoveToRow(RequestRows)
  292.     ' If table did not make it to the NewRow value
  293.     ' i.e. NewRow was not attainable then
  294.     ' end of db was reached
  295.     If NewRow <> RequestRows Then
  296.         ' Set CurRows to actual end of file
  297.         CurRows = NewRow
  298.         ' Set EndRow to actual end of file
  299.         EndRow = NewRow
  300.     End If
  301. End Sub
  302. Sub Table1_Fetch (row As Long, Col As Integer, Value As String)
  303.     ' This condition should always be true because of the
  304.     ' code in the CheckRows events but we double check
  305.     NewRow = MoveToRow(row)
  306. '    Debug.Print "OR=" & Str$(row)
  307. '    Debug.Print "NR =" & Str$(NewRow)
  308.     If NewRow = row Then
  309.         
  310.         ' If field is empty trap Null and use empty quotes instead
  311.         If IsNull(Tb(Col - 1)) Then
  312.             Value = ""
  313.         Else
  314.             Value = Tb(Col - 1)
  315.         End If
  316.     Else
  317.         MsgBox "Error in navigating database"
  318.     End If
  319. End Sub
  320. Sub Table1_Update (row As Long, Col As Integer, Value As String)
  321.     ' This should always be true because of the code in the
  322.     ' CheckRows but we double check anyways
  323.     If MoveToRow(row) = row Then
  324.         Call UpdateTable(Col, Value)
  325.     Else
  326.         MsgBox "Error updating value"
  327.     End If
  328. End Sub
  329. Sub UpdateTable (Column As Integer, NewValue As String)
  330.         ' There is no error checking so becareful
  331.         ' of data mismatches!!!
  332.         Tb.Edit
  333.         Tb(Column - 1) = NewValue
  334.         Tb.Update
  335. End Sub
  336.