home *** CD-ROM | disk | FTP | other *** search
/ Programming Tool Box / SIMS_2.iso / bp_6_93 / bonus / dmsrc / dataform.txt < prev    next >
Text File  |  1995-02-26  |  13KB  |  535 lines

  1. Dim FldArr() As control
  2.  
  3. Dim FDS As Dynaset
  4. Dim numFlds As Integer
  5. Dim CurrField As Integer
  6. Dim JustUsedFind As Integer        'flag for find function
  7. Dim fResizing As Integer           'flag to avoid resize recursion
  8.  
  9. Dim FldTop As Integer
  10.  
  11. Const EM_NOTHING = 0
  12. Const EM_EDIT = 1
  13. Const EM_ADDNEW = 2
  14.  
  15. Const FT_TRUEFALSE = 1
  16. Const FT_BYTE = 2
  17. Const FT_INTEGER = 3
  18. Const FT_LONG = 4
  19. Const FT_CURRENCY = 5
  20. Const FT_SINGLE = 6
  21. Const FT_DOUBLE = 7
  22. Const FT_DATETIME = 8
  23. Const FT_STRING = 10
  24. Const FT_BINARY = 11
  25. Const FT_MEMO = 12
  26.  
  27. Const YES = 6
  28. Const MSGBOX_TYPE = 4 + 48
  29.  
  30. Sub AddBtn_Click ()
  31.   On Error GoTo AddErr
  32.     
  33.     data1.Caption = "Entering New Record"
  34.   If AddBtn.Tag = "Disabled" Then
  35.     EnableAllControls
  36.   End If
  37.   data1.Recordset.AddNew
  38.   FldArr(0).SetFocus
  39.   Exit Sub
  40.  
  41. AddErr:
  42.   MsgBox Error$
  43.   Resume AddEnd
  44. AddEnd:
  45.  
  46. End Sub
  47.  
  48. Sub cFieldPicture_Click (Index As Integer)
  49.   'this toggles the size of a picture control
  50.   'so it mat be viewed or compressed
  51.   If cFieldPicture(Index).Height <= 280 Then
  52.     cFieldPicture(Index).AutoSize = True
  53.   Else
  54.     cFieldPicture(Index).AutoSize = False
  55.     cFieldPicture(Index).Height = 280
  56.   End If
  57. End Sub
  58.  
  59. Sub cFieldPicture_DblClick (Index As Integer)
  60.   On Error GoTo PicErr
  61.  
  62.   st = InputBox("Enter Picture FilName:")
  63.   If st <> "" Then
  64.     cFieldPicture(Index).Picture = LoadPicture(st)
  65.   End If
  66.  
  67.   GoTo PicEnd
  68.  
  69. PicErr:
  70.   MsgBox Error$
  71.   Resume PicEnd
  72.  
  73. PicEnd:
  74.  
  75. End Sub
  76.  
  77. Sub cScrollBar_Change ()
  78.   Dim t As Integer
  79.  
  80.   t = cScrollBar
  81.   If (t - FldTop) Mod 350 = 0 Then
  82.     cFields.Top = t
  83.   Else
  84.     cFields.Top = ((t - FldTop) \ 350) * 350 + FldTop
  85.   End If
  86.  
  87. End Sub
  88.  
  89. Sub Data1_Error (dataerr As Integer, response As Integer)
  90.   If dataerr = 3021 Then
  91.     response = 0
  92.   ElseIf dataerr = 481 Or dataerr = 321 Then 'Invalid picture
  93.     response = 0
  94.   Else
  95.     MsgBox "Error:  " + Error$(dataerr)
  96.     response = 0
  97.   End If
  98. End Sub
  99.  
  100. Sub data1_Reposition ()
  101.     'if not valid record and not in addnew mode
  102.     If (data1.Recordset.BOF Or data1.Recordset.EOF) And data1.Caption <> "Entering New Record" Then
  103.         DisableAllControls
  104.     'otherwise, if form is disabled, then enable it
  105.     ElseIf AddBtn.Tag = "Disabled" Then
  106.         EnableAllControls
  107.     Else
  108.        If data1.Caption <> "Entering New Record" Then data1.Caption = "Editing Record"
  109.     End If
  110. End Sub
  111.  
  112. Sub Data1_Validate (Action As Integer, save As Integer)
  113. On Error Resume Next
  114.   'first check for a move from an addnew or edit record
  115.   If Action < 5 Then
  116.     If save = True Then      'data changed
  117.       If data1.EditMode = EM_ADDNEW Then
  118.         If MsgBox("Save New Record?", MSGBOX_TYPE) = YES Then
  119.           data1.UpdateRecord
  120.           If Err <> 0 Then
  121.             MsgBox Error$, 0, "Data Manager"
  122.             Action = 0: save = 0
  123.           End If
  124.           save = 0
  125.         Else
  126.             save = 0
  127.         End If
  128.       ElseIf MsgBox("Commit Changes?", MSGBOX_TYPE) <> YES Then
  129.          save = False        'loose changes
  130.       End If
  131.     End If
  132.   data1.Caption = "Editing Record"
  133.   End If
  134.  
  135.   Select Case Action
  136.     Case 1          'First
  137.     Case 6          'Update
  138.       If save = True Then
  139.         If data1.EditMode = EM_ADDNEW Then
  140.           If MsgBox("Save New Record?", MSGBOX_TYPE) = YES Then
  141.             data1.UpdateRecord
  142.             data1.Caption = "Editing Record"
  143.           Else
  144.             save = 0: Action = 0
  145.           End If
  146.         ElseIf MsgBox("Commit Changes?", MSGBOX_TYPE) = YES Then
  147.           data1.UpdateRecord
  148.         End If
  149.       End If
  150.  
  151.     Case 10          'Close
  152.       If save = True Then
  153.         If MsgBox("Commit Changes before Closing?", MSGBOX_TYPE) = YES Then
  154.         Else
  155.           Cancel = True
  156.         End If
  157.       End If
  158.  
  159.   End Select
  160.  
  161.  
  162. End Sub
  163.  
  164. Sub DeleteBtn_Click ()
  165.   On Error GoTo DelErr
  166.  
  167.   If MsgBox("Delete Current Record?", MSGBOX_TYPE) = YES Then
  168.     data1.Recordset.Delete
  169.     data1.Recordset.MoveNext
  170.     If AddBtn.Tag <> "Disabled" Then FldArr(0).SetFocus
  171.   End If
  172.  
  173.   GoTo DelEnd
  174.  
  175. DelErr:
  176.     If Err = 444 Then
  177.         MsgBox "Can't delete this record.", 64, "Data Manager"
  178.     ElseIf Err = 3021 Then
  179.         DisableAllControls
  180.     Else
  181.         MsgBox Error$, 64, "Data Manager"
  182.     End If
  183.     If AddBtn.Tag <> "Disabled" Then FldArr(0).SetFocus
  184.   Resume DelEnd
  185.  
  186. DelEnd:
  187.  
  188. End Sub
  189.  
  190. Sub DisableAllControls ()
  191.  
  192.     On Error GoTo disableerror
  193.     'This handles the case of calls with empty tables before
  194.     'call of loadfields.  Otherwise, you get subscript out of range.
  195.    
  196.    Dim i As Integer
  197.    DeleteBtn.Enabled = False
  198.    UpdateBtn.Enabled = False
  199. '   FindBtn.Enabled = False
  200.    For i = 0 To data1.Recordset.Fields.Count - 1
  201.      FldArr(i).Visible = False
  202.    Next i
  203.  
  204.     GoTo disableend
  205. disableerror:
  206.     Resume disableend
  207. disableend:
  208.    AddBtn.Tag = "Disabled"
  209.    data1.Caption = "No Current Record"
  210.  
  211. End Sub
  212.  
  213. Sub EnableAllControls ()
  214.    
  215.    Dim i As Integer
  216.    DeleteBtn.Enabled = True
  217.    UpdateBtn.Enabled = True
  218. '   FindBtn.Enabled = True
  219.  
  220.    For i = 0 To data1.Recordset.Fields.Count - 1
  221.      FldArr(i).Visible = True
  222.    Next i
  223.    AddBtn.Tag = "Enabled"
  224.    If data1.Caption <> "Entering New Record" Then
  225.     data1.Caption = "Editing Record"
  226.    End If
  227. End Sub
  228.  
  229. Sub FindBtn_Click ()
  230.   On Error GoTo FindErr
  231.   Dim bm As String, findstr As String
  232.  
  233.   findstr = InputBox("Enter Search Expression:")
  234.   If findstr = "" Then Exit Sub
  235.  
  236.   If Not data1.Recordset.BOF And Not data1.Recordset.EOF Then
  237.     bm = data1.Recordset.Bookmark
  238.   End If
  239.  
  240.   data1.Recordset.FindFirst findstr
  241.  
  242.   'return to old record if no match was found
  243.   If data1.Recordset.NoMatch And bm <> "" Then
  244.     data1.Recordset.Bookmark = bm
  245.   End If
  246.  
  247.   GoTo FindEnd
  248.  
  249. FindErr:
  250.   MsgBox Error$
  251.   Resume FindEnd
  252.  
  253. FindEnd:
  254.   If FldArr(0).Visible = True Then FldArr(0).SetFocus
  255.  
  256. End Sub
  257.  
  258. Sub Form_Load ()
  259.   Dim ds2 As Dynaset
  260.  
  261.   On Error GoTo LoadErr
  262.  
  263.   '-------------------------------------------------------
  264.   'this is where the data control properties get
  265.   'set from whatever source they are coming from
  266.   'in this case, it is mainform controls
  267.   '-------------------------------------------------------
  268.   Screen.MousePointer = 11 'wait cursor
  269.   data1.DatabaseName = gDatabaseName
  270.   data1.Connect = gDatabase.Connect
  271.   Me.Caption = UCase(gDatabaseName) + " : " + UCase(mainForm.TableName)
  272.   data1.RecordSource = mainForm.TableName
  273.   '-------------------------------------------------------
  274.   data1.Refresh
  275.  
  276.   LoadFields data1.Recordset, mainForm.TableName
  277.   data1_Reposition 'This ensures that we enable the controls
  278.   Me.Show
  279.   If AddBtn.Tag = "Enabled" Then
  280.     FldArr(0).SetFocus
  281.   End If
  282.   GoTo loadend
  283. LoadErr:
  284.  
  285.   MsgBox Error$
  286.   Unload Me
  287.   Resume loadend
  288.  
  289. loadend:
  290.   Screen.MousePointer = 0
  291. End Sub
  292.  
  293. Sub Form_Resize ()
  294.   On Error Resume Next
  295.  
  296.   If fResizing = True Then Exit Sub
  297.  
  298.   Dim h As Integer, i As Integer
  299.   Dim totw As Integer
  300.  
  301.   fResizing = True
  302.   If WindowState <> 1 And cFieldName(0).Visible = True Then 'not minimized
  303.     'make sure the form is lined up on a field
  304.     h = Height
  305.     If (h - 1340) Mod 350 <> 0 Then
  306.       Height = ((h - 1340) \ 350) * 350 + 1340
  307.     End If
  308.     'reset scroll
  309.     If Height - 1340 >= cFields.Height - 1065 + 350 Then
  310.         cScrollBar.Visible = False
  311.     Else
  312.         cScrollBar.Max = cScrollBar.Min + 350 * ((Height - 1340) \ 350) - (cFields.Height - 1065 + 350)
  313.         cScrollBar.Visible = True
  314.     End If
  315.     'resize the status bar
  316.     StatBox.Top = Height - 650
  317.     'resize the scrollbar
  318.     cScrollBar.Height = StatBox.Top - (FieldHeader.Top - FieldHeader.Height) - 600
  319.     cScrollBar.Left = Width - 360
  320.     If FDS.Fields.Count > 10 Then
  321.       cFields.Width = Width - 260
  322.       totw = cScrollBar.Left - 20
  323.     Else
  324.       cFields.Width = Width - 20
  325.       totw = Width - 50
  326.     End If
  327.     FieldHeader.Width = Width - 20
  328.     'widen the fields if possible
  329. '    data1.Database.TableDefs(TableName).Fields.Refresh
  330. '    For i = 0 To data1.Recordset.Fields.Count - 1
  331. '      cFieldName(i).Width = .3 * totw
  332. '       FldArr(i).Left = cFieldName(i).Width + 20
  333. '      If data1.Recordset.Fields(i).Type > 9 Then
  334. '        FldArr(i).Width = .7 * totw - 270
  335. '      End If
  336. '    Next
  337.     FieldValueLabel.Left = FldArr(0).Left
  338.   End If
  339.  
  340.   data1.Width = StatBox.Width
  341.   fResizing = False
  342.  
  343. End Sub
  344.  
  345. Function GetFieldWidth (t As Integer)
  346.   'determines the form control width
  347.   'based on the field type
  348.   Select Case t
  349.     Case FT_TRUEFALSE
  350.       GetFieldWidth = 850
  351.     Case FT_BYTE
  352.       GetFieldWidth = 650
  353.     Case FT_INTEGER
  354.       GetFieldWidth = 900
  355.     Case FT_LONG
  356.       GetFieldWidth = 1100
  357.     Case FT_CURRENCY
  358.       GetFieldWidth = 1800
  359.     Case FT_SINGLE
  360.       GetFieldWidth = 1800
  361.     Case FT_DOUBLE
  362.       GetFieldWidth = 2200
  363.     Case FT_DATETIME
  364.       GetFieldWidth = 2000
  365.     Case FT_STRING
  366.       GetFieldWidth = 3250
  367.     Case FT_MEMO
  368.       GetFieldWidth = 3250
  369.     Case Else
  370.       GetFieldWidth = 3250
  371.   End Select
  372.  
  373. End Function
  374.  
  375. Sub LoadFields (t As Dynaset, tName)
  376.    
  377. '   Dim t As table
  378.    Dim ft As Integer
  379.    Dim i As Integer
  380.  
  381.    On Error GoTo LoadFieldsErr
  382.  
  383. '   Set t = db.OpenTable(tName)
  384.  
  385.    'load the controls on the dynaset form
  386.    numFlds = t.Fields.Count
  387.  
  388.     If numFlds = 0 Then
  389.         MsgBox "There are no fields in this table.  Cannot Edit Table Data", 64, "Data Manager"
  390.         Unload Me
  391.     End If
  392.  
  393.    ReDim FldArr(numFlds) As control
  394.    cFieldName(0).Visible = True
  395.    ft = t.Fields(0).Type
  396.    If ft = FT_TRUEFALSE Then
  397.      Set FldArr(0) = cFieldCheck(0)
  398.    ElseIf ft = FT_BINARY Then
  399.      Set FldArr(0) = cFieldPicture(0)
  400.    Else
  401.      Set FldArr(0) = cFieldData(0)
  402.    End If
  403.    FldArr(0).Visible = True
  404.    FldArr(0).Top = 0
  405.    FldArr(0).Width = GetFieldWidth(ft)
  406.    FldArr(0).TabIndex = 0
  407.  
  408.    On Error Resume Next
  409.    For i = 1 To t.Fields.Count - 1
  410.      cFields.Height = cFields.Height + 350
  411.      Load cFieldName(i)
  412.      cFieldName(i).Top = cFieldName(i - 1).Top + 350
  413.      cFieldName(i).Visible = True
  414.      ft = t.Fields(i).Type
  415.      If ft = FT_TRUEFALSE Then
  416.        Load cFieldCheck(i)
  417.        Set FldArr(i) = cFieldCheck(i)
  418.      ElseIf ft = FT_BINARY Then
  419.        Load cFieldPicture(i)
  420.        Set FldArr(i) = cFieldPicture(i)
  421.      Else
  422.        Load cFieldData(i)
  423.        Set FldArr(i) = cFieldData(i)
  424.      End If
  425.      FldArr(i).Top = FldArr(i - 1).Top + 350
  426.      FldArr(i).Width = GetFieldWidth(ft)
  427.      FldArr(i).TabIndex = i
  428.    Next
  429.    AddBtn.Tag = "Disabled"
  430.  
  431.    On Error GoTo LoadFieldsErr
  432.  
  433.    'resize main window
  434.    cFields.Top = FieldHeader.Top + FieldHeader.Height
  435.    FldTop = cFields.Top
  436.    cScrollBar.Min = FldTop
  437.    If i <= 10 Then
  438.      Height = i * 350 + 1500
  439.      cScrollBar.Visible = False
  440.    Else
  441.      Height = 5000
  442.      Width = Width + 260
  443.      cScrollBar.Visible = True
  444.      cScrollBar.Max = FldTop - (i * 350) + 3500
  445.      cScrollBar = FldTop
  446.    End If
  447.  
  448.    'display the field names
  449.    For i = 0 To t.Fields.Count - 1
  450.      cFieldName(i) = UCase(t.Fields(i).Name) + ":"
  451.    Next
  452.    
  453.    'bind the controls
  454.    On Error Resume Next   'bind even if table is empty
  455.    For i = 0 To t.Fields.Count - 1
  456.      FldArr(i).DataField = t.Fields(i).Name
  457.    Next
  458.  
  459.    GoTo LoadFieldsEnd
  460.  
  461. LoadFieldsErr:
  462.    MsgBox Error$
  463.    Resume LoadFieldsEnd
  464.  
  465. LoadFieldsEnd:
  466.  
  467. End Sub
  468.  
  469. Sub MoveBtn_Click (Index As Integer)
  470.   On Error GoTo moveerr
  471.   Dim bm As String
  472.  
  473.   If Not data1.Recordset.BOF And Not data1.Recordset.EOF Then
  474.     bm = data1.Recordset.Bookmark
  475.   End If
  476.   Select Case Index
  477.     Case 0
  478.       If findval <> "" Then
  479.         data1.Recordset.FindFirst findval
  480.       Else
  481.         data1.Recordset.MoveFirst
  482.       End If
  483.     Case 1
  484.       If findval <> "" Then
  485.         data1.Recordset.FindPrevious findval
  486.       Else
  487.         data1.Recordset.MovePrevious
  488.       End If
  489.     Case 2
  490.       If findval <> "" Then
  491.         data1.Recordset.FindNext findval
  492.       Else
  493.         data1.Recordset.MoveNext
  494.       End If
  495.     Case 3
  496.       If findval <> "" Then
  497.         data1.Recordset.FindLast findval
  498.       Else
  499.         data1.Recordset.MoveLast
  500.       End If
  501.   End Select
  502.   'return to old record if no match was found
  503.   If data1.Recordset.NoMatch And bm <> "" Then
  504.     data1.Recordset.Bookmark = bm
  505.   End If
  506.  
  507.   GoTo moveend
  508.  
  509. moveerr:
  510.   MsgBox Error$
  511.   Resume moveend
  512.  
  513. moveend:
  514.   FldArr(0).SetFocus
  515. End Sub
  516.  
  517. Sub RefreshBtn_Click ()
  518.   data1.Refresh
  519. End Sub
  520.  
  521. Sub UpdateBtn_Click ()
  522.   On Error GoTo UpdErr
  523.  
  524.   data1.Recordset.Update
  525.  
  526.   GoTo UpdEnd
  527. UpdErr:
  528.   MsgBox Error$
  529.   Resume UpdEnd
  530.  
  531. UpdEnd:
  532.  
  533. End Sub
  534.  
  535.