home *** CD-ROM | disk | FTP | other *** search
/ Master 95 #1 / MASTER95_1.iso / microsof / vbasic4 / vb4-6.cab / dataform.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-07-26  |  29.6 KB  |  904 lines

  1. VERSION 4.00
  2. Begin VB.Form frmDataControl 
  3.    ClientHeight    =   2160
  4.    ClientLeft      =   4155
  5.    ClientTop       =   2250
  6.    ClientWidth     =   5670
  7.    Height          =   2565
  8.    HelpContextID   =   2016122
  9.    Icon            =   "DATAFORM.frx":0000
  10.    Left            =   4095
  11.    LinkTopic       =   "Form2"
  12.    LockControls    =   -1  'True
  13.    MDIChild        =   -1  'True
  14.    ScaleHeight     =   2160
  15.    ScaleWidth      =   5670
  16.    Tag             =   "Recordset"
  17.    Top             =   1905
  18.    Width           =   5790
  19.    Begin VB.PictureBox picButtons 
  20.       Align           =   1  'Align Top
  21.       Appearance      =   0  'Flat
  22.       BorderStyle     =   0  'None
  23.       ForeColor       =   &H80000008&
  24.       Height          =   600
  25.       Left            =   0
  26.       ScaleHeight     =   600
  27.       ScaleWidth      =   5670
  28.       TabIndex        =   0
  29.       Top             =   0
  30.       Width           =   5670
  31.       Begin VB.CommandButton cmdCancelAdd 
  32.          Caption         =   "C&ancel"
  33.          Height          =   330
  34.          Left            =   0
  35.          TabIndex        =   13
  36.          Top             =   0
  37.          Visible         =   0   'False
  38.          Width           =   960
  39.       End
  40.       Begin VB.CommandButton cmdRefresh 
  41.          Caption         =   "&Refresh"
  42.          Height          =   330
  43.          Left            =   3780
  44.          TabIndex        =   12
  45.          Top             =   0
  46.          Width           =   960
  47.       End
  48.       Begin VB.CommandButton cmdFind 
  49.          Caption         =   "&Find"
  50.          Height          =   330
  51.          Left            =   2835
  52.          TabIndex        =   5
  53.          Top             =   0
  54.          Width           =   960
  55.       End
  56.       Begin VB.CommandButton cmdClose 
  57.          Cancel          =   -1  'True
  58.          Caption         =   "&Close"
  59.          Height          =   330
  60.          Left            =   4725
  61.          TabIndex        =   4
  62.          Top             =   0
  63.          Width           =   960
  64.       End
  65.       Begin VB.CommandButton cmdDelete 
  66.          Caption         =   "&Delete"
  67.          Height          =   330
  68.          Left            =   1890
  69.          TabIndex        =   3
  70.          Top             =   0
  71.          Width           =   960
  72.       End
  73.       Begin VB.CommandButton cmdAdd 
  74.          Caption         =   "&Add"
  75.          Height          =   330
  76.          Left            =   0
  77.          TabIndex        =   2
  78.          Top             =   0
  79.          Width           =   960
  80.       End
  81.       Begin VB.CommandButton cmdUpdate 
  82.          Caption         =   "&Update"
  83.          Height          =   330
  84.          Left            =   960
  85.          TabIndex        =   1
  86.          Top             =   0
  87.          Width           =   960
  88.       End
  89.       Begin VB.Label lblFieldHeader 
  90.          Caption         =   "Field Name:"
  91.          Height          =   255
  92.          Left            =   120
  93.          TabIndex        =   15
  94.          Top             =   360
  95.          Width           =   1215
  96.       End
  97.       Begin VB.Label lblFieldValue 
  98.          Caption         =   " Value:"
  99.          Height          =   255
  100.          Left            =   1680
  101.          TabIndex        =   14
  102.          Top             =   360
  103.          Width           =   2655
  104.       End
  105.    End
  106.    Begin VB.Data datDataCtl 
  107.       Align           =   2  'Align Bottom
  108.       Connect         =   ""
  109.       DatabaseName    =   ""
  110.       Exclusive       =   0   'False
  111.       Height          =   300
  112.       Left            =   0
  113.       Options         =   0
  114.       ReadOnly        =   0   'False
  115.       RecordsetType   =   1  'Dynaset
  116.       RecordSource    =   ""
  117.       Tag             =   "OLE"
  118.       Top             =   1860
  119.       Width           =   5670
  120.    End
  121.    Begin VB.VScrollBar vsbScrollBar 
  122.       Height          =   2440
  123.       LargeChange     =   3000
  124.       Left            =   7665
  125.       SmallChange     =   300
  126.       TabIndex        =   11
  127.       Top             =   630
  128.       Visible         =   0   'False
  129.       Width           =   255
  130.    End
  131.    Begin VB.PictureBox picFields 
  132.       Appearance      =   0  'Flat
  133.       BorderStyle     =   0  'None
  134.       ForeColor       =   &H80000008&
  135.       Height          =   1065
  136.       Left            =   0
  137.       ScaleHeight     =   1056.479
  138.       ScaleMode       =   0  'User
  139.       ScaleWidth      =   7600.262
  140.       TabIndex        =   6
  141.       TabStop         =   0   'False
  142.       Top             =   600
  143.       Width           =   7605
  144.       Begin VB.TextBox txtFieldData 
  145.          BackColor       =   &H00FFFFFF&
  146.          DataSource      =   "datDataCtl"
  147.          ForeColor       =   &H00000000&
  148.          Height          =   285
  149.          Index           =   0
  150.          Left            =   1665
  151.          TabIndex        =   9
  152.          Top             =   0
  153.          Visible         =   0   'False
  154.          Width           =   3255
  155.       End
  156.       Begin VB.CheckBox chkFieldData 
  157.          DataSource      =   "datDataCtl"
  158.          Height          =   282
  159.          Index           =   0
  160.          Left            =   1680
  161.          TabIndex        =   8
  162.          Top             =   735
  163.          Visible         =   0   'False
  164.          Width           =   3270
  165.       End
  166.       Begin VB.PictureBox picFieldData 
  167.          BackColor       =   &H00FFFFFF&
  168.          DataSource      =   "datDataCtl"
  169.          Height          =   285
  170.          Index           =   0
  171.          Left            =   1680
  172.          ScaleHeight     =   225
  173.          ScaleWidth      =   3210
  174.          TabIndex        =   7
  175.          Top             =   360
  176.          Visible         =   0   'False
  177.          Width           =   3270
  178.       End
  179.       Begin VB.OLE oleFieldData 
  180.          BackColor       =   &H00FFFFFF&
  181.          DataSource      =   "datDataCtl"
  182.          DisplayType     =   1  'Icon
  183.          Height          =   300
  184.          Index           =   0
  185.          Left            =   1680
  186.          TabIndex        =   16
  187.          Top             =   360
  188.          Visible         =   0   'False
  189.          Width           =   3255
  190.       End
  191.       Begin VB.Label lblFieldName 
  192.          ForeColor       =   &H00000000&
  193.          Height          =   195
  194.          Index           =   0
  195.          Left            =   105
  196.          TabIndex        =   10
  197.          Top             =   0
  198.          Visible         =   0   'False
  199.          Width           =   300
  200.       End
  201.    End
  202. Attribute VB_Name = "frmDataControl"
  203. Attribute VB_Creatable = False
  204. Attribute VB_Exposed = False
  205. Option Explicit
  206. '============================================================================
  207. ' This is a fairly generic form that can be used in most cases with any
  208. ' table. It can be added to any app with a few changes denoted below
  209. ' with ---------.
  210. ' DATACONS.TXT and the data control section from CONSTANT.TXT
  211. ' are required for this form
  212. '============================================================================
  213. '----------------------------------------------------------------------------
  214. 'Uncomment the next line if it does not already exist in you app
  215. 'Declare Function OSTimeGetTime& Lib "MMSYSTEM.DLL" Alias "TimeGetTime" ()
  216. 'for 32 bit you need to use the following declare
  217. 'Declare Function OSTimeGetTime& Lib "WINMM.DLL" Alias "timeGetTime" ()
  218. '----------------------------------------------------------------------------
  219. Dim maFldArr() As Object
  220. Dim mrecFDS As Recordset
  221. Dim msBookMark As String         'form bookmark
  222. Dim mnNumFields As Integer       'number of fields
  223. Dim mlNumRows As Long            'recordcount for recordset
  224. Dim mbJustUsedFind As Integer    'flag for find function
  225. Dim mbResizing As Integer        'flag to avoid resize recursion
  226. Dim mbCancel As Integer          'flag to cancel an addnew
  227. Dim mnFieldTop As Integer        'top field position
  228. Const mnMSGBOX_YES = 6
  229. Const mnMSGBOX_TYPE = 4 + 48
  230. Const mnCTLARRAYHEIGHT = 340
  231. Private Sub chkFieldData_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
  232.   If Button <> 2 Then Exit Sub
  233.   ShowProperties "Field", mrecFDS.Fields(Index)
  234. End Sub
  235. Private Sub cmdAdd_Click()
  236.   On Error GoTo AddErr
  237.   datDataCtl.Recordset.AddNew
  238.   datDataCtl.Caption = "New Record"
  239.   cmdCancelAdd.Visible = True
  240.   cmdAdd.Visible = False
  241.   If datDataCtl.Recordset.RecordCount <> 0 Then
  242.     msBookMark = datDataCtl.Recordset.Bookmark
  243.     maFldArr(0).SetFocus
  244.   End If
  245.   Exit Sub
  246. AddErr:
  247.   ShowErrMsg
  248.   Exit Sub
  249. End Sub
  250. Private Sub cmdCancelAdd_Click()
  251.   On Error Resume Next
  252.   mbCancel = True
  253.   'go back to the previous current record
  254.   If Len(msBookMark) > 0 Then
  255.     datDataCtl.Recordset.Bookmark = msBookMark
  256.   End If
  257. End Sub
  258. '----------------------------------------------------------
  259. 'this sub loads the Data Control property sheet
  260. 'comment it out for standalone use
  261. '----------------------------------------------------------
  262. Sub datDataCtl_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)
  263.   On Error GoTo DCPErr
  264.   Dim i As Integer
  265.   Dim recClone As Recordset
  266.   Dim sTmpRS As String
  267.   Dim sTmpDB As String
  268.   Dim sTmpTag As String
  269.   If Button = 2 Then
  270.     SetHourglass
  271.     sTmpRS = datDataCtl.RecordSource
  272.     sTmpDB = datDataCtl.DatabaseName
  273.     sTmpTag = datDataCtl.Tag
  274.     Set gDataCtlObj = datDataCtl
  275.     frmDataCtlProp.Show vbModal
  276.     If Not gDataCtlObj Is Nothing Then
  277.       'check for a changed recordset, db or bound binary type
  278.       'and clear the bound fields if one was changed
  279.       If (sTmpRS <> gDataCtlObj.RecordSource Or sTmpDB <> gDataCtlObj.DatabaseName) _
  280.          Or gDataCtlObj.Tag <> sTmpTag Then
  281.         'clear the field names and unbind the controls
  282.         For i = 0 To mnNumFields - 1
  283.           lblFieldName(i).Caption = gsNULL_STR
  284.           maFldArr(i).DataField = gsNULL_STR
  285.           maFldArr(i).Visible = False
  286.         Next
  287.       End If
  288.       datDataCtl.Refresh
  289.       If (sTmpRS <> gDataCtlObj.RecordSource Or sTmpDB <> gDataCtlObj.DatabaseName) _
  290.          Or gDataCtlObj.Tag <> sTmpTag Then
  291.         Set recClone = datDataCtl.Recordset.Clone()
  292.         If recClone.BOF = False And (datDataCtl.Options And dbForwardOnly) = 0 Then
  293.           recClone.MoveLast
  294.           mlNumRows = recClone.RecordCount
  295.         Else
  296.           mlNumRows = 0
  297.         End If
  298.         recClone.Close
  299.         LoadFields
  300.         SetRecNum
  301.       Else
  302.         'need to refresh the local recordset copy for
  303.         'other operations that use it such as property access
  304.         Set mrecFDS = datDataCtl.Recordset
  305.         'if it is a forward only recordset, then we need to call SetRecNum
  306.         'to display the correct data control caption
  307.         If (datDataCtl.Options And dbForwardOnly) = dbForwardOnly Then
  308.           SetRecNum
  309.         End If
  310.       End If
  311.       gbSettingDataCtl = False
  312.       If gDataCtlObj.Tag <> sTmpTag Then
  313.         Form_Resize    'needed to set the left prop on newly loaded ctls
  314.       End If
  315.     End If
  316.   End If
  317.   Exit Sub
  318. DCPErr:
  319.   ShowError
  320.   Unload Me
  321.   Exit Sub
  322. End Sub
  323. Private Sub Form_Unload(Cancel As Integer)
  324.   DBEngine.Idle dbFreeLocks
  325. End Sub
  326. Private Sub oleFieldData_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
  327.   If Button <> 2 Then Exit Sub
  328.   'this will display the dialog that allows
  329.   'entry in to the ole field
  330.   oleFieldData(Index).InsertObjDlg
  331. '  ShowProperties "Field", mrecFDS.Fields(Index)
  332. End Sub
  333. Private Sub picFieldData_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
  334.   If Button <> 2 Then Exit Sub
  335.   ShowProperties "Field", mrecFDS.Fields(Index)
  336. End Sub
  337. Private Sub txtFieldData_KeyPress(Index As Integer, KeyAscii As Integer)
  338.   'go to next field on an enter keypress
  339.   If KeyAscii = 13 Then
  340.     KeyAscii = 0
  341.     SendKeys "{Tab}"
  342.   End If
  343. End Sub
  344. Private Sub picFieldData_Click(Index As Integer)
  345.   'this toggles the size of a picture control
  346.   'so it mat be viewed or compressed
  347.   If picFieldData(Index).Height <= 280 Then
  348.     picFieldData(Index).AutoSize = True
  349.   Else
  350.     picFieldData(Index).AutoSize = False
  351.     picFieldData(Index).Height = 280
  352.   End If
  353. End Sub
  354. Private Sub picFieldData_DblClick(Index As Integer)
  355.   On Error GoTo PicErr
  356.   '----------------------------------------------------------
  357.   ' For standalone operation, you must add a Common Dialog
  358.   ' control called dlgCMD1 and remove the frmMDI references
  359.   '----------------------------------------------------------
  360.   With frmMDI.dlgCMD1
  361.     .Filter = "Bitmaps (*.bmp)|*.bmp|Icons (*.ico)|*.ico|Metafiles (*.wmf)|*.wmf|All Files (*.*)|*.*"
  362.     .DialogTitle = "Select a Picture File to Load"
  363.     .FilterIndex = 1
  364.   End With
  365.   frmMDI.dlgCMD1.ShowOpen
  366.   If Len(frmMDI.dlgCMD1.FileName) > 0 Then
  367.     picFieldData(Index).Picture = LoadPicture(frmMDI.dlgCMD1.FileName)
  368.   End If
  369.   Exit Sub
  370. PicErr:
  371.   ShowErrMsg
  372.   Exit Sub
  373. End Sub
  374. Private Sub cmdClose_Click()
  375.   On Error Resume Next
  376.   Unload Me
  377. End Sub
  378. Sub txtFieldData_MouseUp(Index As Integer, Button As Integer, Shift As Integer, x As Single, Y As Single)
  379.   If Button <> 2 Then Exit Sub
  380.   ShowProperties "Field", mrecFDS.Fields(Index)
  381. End Sub
  382. Private Sub vsbScrollBar_Change()
  383.   Dim nCurrVal As Integer
  384.   nCurrVal = vsbScrollBar
  385.   If (nCurrVal - mnFieldTop) Mod mnCTLARRAYHEIGHT = 0 Then
  386.     picFields.Top = nCurrVal
  387.   Else
  388.     picFields.Top = ((nCurrVal - mnFieldTop) \ mnCTLARRAYHEIGHT) * mnCTLARRAYHEIGHT + mnFieldTop
  389.   End If
  390. End Sub
  391. Private Sub datDataCtl_Error(DataErr As Integer, Response As Integer)
  392.   If DataErr = 481 Then  'throw away bad picture error
  393.     Response = vbDataErrContinue
  394.   Else
  395.     MsgBox "Data error event hit Err:" & Error(DataErr)
  396.   End If
  397. End Sub
  398. Private Sub datDataCtl_RePosition()
  399.   On Error GoTo RepErr
  400.   Dim sBookMark As String
  401.   Dim recClone As Recordset
  402.   'if we are resetting the data control, we need to skip this sub
  403.   If gbSettingDataCtl = True Then Exit Sub
  404.   'if the recordset is empty and we are not already in AddNew mode
  405.   'we need to call the cmdAdd_Click routine to execute an AddNew
  406.   If datDataCtl.Recordset.RecordCount = 0 And datDataCtl.EditMode <> dbEditAdd Then
  407.     Call cmdAdd_Click
  408.     Exit Sub
  409.   End If
  410.   SetRecNum
  411.   Exit Sub
  412. RepErr:
  413.   ShowErrMsg
  414.   Exit Sub
  415. End Sub
  416. Private Sub datDataCtl_Validate(Action As Integer, Save As Integer)
  417.   On Error GoTo ValErr
  418.   If mbCancel Then
  419.     Save = False
  420.     mbCancel = False
  421.     Exit Sub
  422.   End If
  423.   'first check for a move from an addnew or edit record
  424.   If Action < 5 Then
  425.     If Save = True Then      'data changed
  426.       If datDataCtl.EditMode = dbEditAdd Then
  427.         If MsgBox("Save New Record?", mnMSGBOX_TYPE) = mnMSGBOX_YES Then
  428.           mlNumRows = mlNumRows + 1
  429.         Else
  430.           Save = False
  431.         End If
  432.       Else
  433.         If MsgBox("Commit Changes?", mnMSGBOX_TYPE) <> mnMSGBOX_YES Then
  434.           Save = False        'loose changes
  435.         End If
  436.       End If
  437.     End If
  438.   End If
  439.   Select Case Action
  440.     Case vbDataActionMoveFirst
  441.       'do nothing
  442.     Case vbDataActionMovePrevious
  443.       'do nothing
  444.     Case vbDataActionMoveNext
  445.       'do nothing
  446.     Case vbDataActionMoveLast
  447.       'do nothing
  448.     Case vbDataActionAddNew
  449.       'do nothing
  450.     Case vbDataActionUpdate
  451.        'moved to the cmdUpdate_click event code
  452.     Case vbDataActionDelete
  453.       'do nothing
  454.     Case vbDataActionFind
  455.       'set the flag for use in the reposition event
  456.       mbJustUsedFind = True
  457.     Case vbDataActionBookmark
  458.       'do nothing"
  459.     Case vbDataActionClose, vbDataActionUnload
  460.       If Save = True Then
  461.         If MsgBox("Commit Changes before Closing?", mnMSGBOX_TYPE) <> mnMSGBOX_YES Then
  462.           Save = False
  463.         End If
  464.       End If
  465.   End Select
  466.   Exit Sub
  467. ValErr:
  468.   ShowErrMsg
  469.   Exit Sub
  470. End Sub
  471. Private Sub cmdDelete_Click()
  472.   On Error GoTo DELErr
  473.   If MsgBox("Delete Current Record?", mnMSGBOX_TYPE) = mnMSGBOX_YES Then
  474.     datDataCtl.Recordset.Delete
  475.     mlNumRows = mlNumRows - 1
  476.     datDataCtl.Recordset.MoveNext
  477.     'move off the EOF condition if it occurs
  478.     If datDataCtl.Recordset.RecordCount > 0 Then datDataCtl.Recordset.MoveLast
  479.     maFldArr(0).SetFocus
  480.   End If
  481.   Exit Sub
  482. DELErr:
  483.   ShowErrMsg
  484.   Exit Sub
  485. End Sub
  486. Private Sub cmdFind_Click()
  487.   On Error GoTo FindErr
  488.   Dim sBookMark As String
  489.   Dim sFindStr As String
  490.   If datDataCtl.Recordset.Type = dbOpenTable Then
  491.     sFindStr = InputBox("Enter Seek Value:")
  492.   Else
  493.     sFindStr = InputBox("Enter Search Expression:")
  494.   End If
  495.   If Len(sFindStr) = 0 Then Exit Sub
  496.   If datDataCtl.Recordset.RecordCount > 0 Then
  497.     sBookMark = datDataCtl.Recordset.Bookmark
  498.   End If
  499.   If datDataCtl.Recordset.Type = dbOpenTable Then
  500.     datDataCtl.Recordset.Seek "=", sFindStr
  501.   Else
  502.     datDataCtl.Recordset.FindFirst sFindStr
  503.   End If
  504.   'return to old record if no match was found
  505.   If datDataCtl.Recordset.NoMatch And Len(sBookMark) > 0 Then
  506.     datDataCtl.Recordset.Bookmark = sBookMark
  507.   End If
  508.   maFldArr(0).SetFocus
  509.   Exit Sub
  510. FindErr:
  511.   ShowErrMsg
  512.   maFldArr(0).SetFocus
  513.   Exit Sub
  514. End Sub
  515. Private Sub Form_Load()
  516.   Dim recTmp As Recordset
  517.   Dim qdfTmp As QueryDef
  518.   Dim bParmQry As Integer
  519.   Dim Start1 As Long, Finish1 As Long, Start2 As Long, Finish2 As Long
  520.   Dim sTmp As String
  521.   On Error GoTo LoadErr
  522.   '-------------------------------------------------------
  523.   'this is where the data control properties get
  524.   'set from whatever source they are coming from
  525.   'in this case, it is form1 controls
  526.   '-------------------------------------------------------
  527.   If gsDataType <> gsSQLDB Then
  528.     datDataCtl.DatabaseName = gdbCurrentDB.Name
  529.   End If
  530.   datDataCtl.Connect = gdbCurrentDB.Connect
  531.   'determine if a table name or sql statement is used
  532.   If gbFromSQL = True Then
  533.     Set qdfTmp = gdbCurrentDB.CreateQueryDef(gsNULL_STR)
  534.     If MsgBox("Is this a SQLPassThrough Query?", vbYesNo + vbQuestion + vbDefaultButton2) = vbYes Then
  535.       sTmp = InputBox("Enter Connect property value:")
  536.       If Len(sTmp) > 0 Then
  537.         qdfTmp.Connect = sTmp
  538.         qdfTmp.ReturnsRecords = True
  539.       End If
  540.     End If
  541.     If Len(gsDynaString) = 0 Then
  542.       datDataCtl.RecordSource = frmSQL.txtSQLStatement
  543.       qdfTmp.SQL = frmSQL.txtSQLStatement
  544.     Else
  545.       datDataCtl.RecordSource = gsDynaString
  546.       qdfTmp.SQL = gsDynaString
  547.     End If
  548.     If qdfTmp.PARAMETERS.Count > 0 Then
  549.       bParmQry = True
  550.     End If
  551.   Else
  552.     If frmTables.optTables.Value = True Then
  553.       If frmMDI.optPassThru.Value = True Then
  554.         datDataCtl.RecordSource = "select * from " & StripOwner((StripConnect(frmTables.lstTables.Text)))
  555.       Else
  556.         datDataCtl.RecordSource = StripConnect(frmTables.lstTables.Text)
  557.       End If
  558.     Else
  559.       datDataCtl.RecordSource = frmTables.lstQueryDefs.Text
  560.       Set qdfTmp = gdbCurrentDB.QueryDefs(frmTables.lstQueryDefs.Text)
  561.       If qdfTmp.PARAMETERS.Count > 0 Then
  562.         bParmQry = True
  563.       End If
  564.     End If
  565.   End If
  566.   'set the passthrough option if checked on the MDI form
  567.   If frmMDI.optPassThru.Visible = True Then
  568.     If frmMDI.optPassThru.Value = True Then
  569.       datDataCtl.Options = dbSQLPassThrough
  570.     End If
  571.   End If
  572.   'set the recordset type
  573.   If frmMDI.optDynaset.Value = True Then
  574.     datDataCtl.RecordsetType = vbRSTypeDynaset
  575.   ElseIf frmMDI.optTable.Value = True Then
  576.     datDataCtl.RecordsetType = vbRSTypeTable
  577.   Else
  578.     'this covers passthrough as well
  579.     datDataCtl.RecordsetType = vbRSTypeSnapShot
  580.   End If
  581.   If bParmQry = True Then
  582.     'parameterized query
  583.     SetParams qdfTmp
  584.     Start1 = OSTimeGetTime()
  585.     Set datDataCtl.Recordset = qdfTmp.OpenRecordset( _
  586.                              IIf(datDataCtl.RecordsetType = vbRSTypeDynaset, 2, 4) _
  587.                              , datDataCtl.Options)
  588.   Else
  589.     Start1 = OSTimeGetTime()
  590.     datDataCtl.Refresh
  591.   End If
  592.   '-------------------------------------------------------
  593.   'for a standalone form, comment out the code above and
  594.   'uncomment the lines below and add the needed info to them
  595.   'datDataCtl.DatabaseName = ""
  596.   'datDataCtl.Connect = ""
  597.   'datDataCtl.RecordSource = ""
  598.   'datDataCtl.RecordsetType = 1   'dynaset
  599.   'datDataCtl.Options = 0
  600.   'Start1 = OSTimeGetTime()
  601.   'datDataCtl.Refresh
  602.   '-------------------------------------------------------
  603.   Finish1 = OSTimeGetTime()
  604.   'set the locking type
  605.   If gsDataType = gsJETMDB And datDataCtl.Recordset.Type <> dbOpenSnapshot Then
  606.     datDataCtl.Recordset.LockEdits = gnMULocking
  607.   End If
  608.   Start2 = OSTimeGetTime()
  609.   If datDataCtl.RecordsetType = vbRSTypeTable Then
  610.     'need to set the index on a table if there is any
  611.     If datDataCtl.Database(datDataCtl.RecordSource).Indexes.Count > 0 Then
  612.       datDataCtl.Recordset.Index = datDataCtl.Database(datDataCtl.RecordSource).Indexes(0).Name
  613.     End If
  614.     mlNumRows = datDataCtl.Recordset.RecordCount
  615.   Else
  616.     'on dynasets and snapshots, we need to move last to get the recordcount
  617.     Set recTmp = datDataCtl.Recordset.Clone()
  618.     If recTmp.BOF = False And (datDataCtl.Options And dbForwardOnly) = 0 Then
  619.       recTmp.MoveLast
  620.       mlNumRows = recTmp.RecordCount
  621.     Else
  622.       mlNumRows = 0
  623.     End If
  624.     recTmp.Close
  625.   End If
  626.   Finish2 = OSTimeGetTime()
  627.   If Len(datDataCtl.RecordSource) > 50 Then
  628.     Me.Caption = "SQL Statement"
  629.   Else
  630.     Me.Caption = datDataCtl.RecordSource
  631.   End If
  632.   Me.Width = 5805
  633.   LoadFields
  634.   Me.Show
  635.   maFldArr(0).SetFocus
  636.   SetRecNum
  637.   Finish2 = OSTimeGetTime()
  638.   '-------------------------------------------------------
  639.   'change the line below for a standalone form
  640.   '-------------------------------------------------------
  641.   If frmMDI.mnuPShowPerf.Checked Then
  642.   '-------------------------------------------------------
  643.     MsgBox CStr(mlNumRows) & " rows found in " & (Finish1 - Start1) / 1000 & " seconds!" & gsNewLine & (Finish2 - Start2) / 1000 & " seconds to Get Record Count!", 48
  644.   End If
  645.   Exit Sub
  646. LoadErr:
  647.   ShowErrMsg
  648.   Unload Me
  649.   Exit Sub
  650. End Sub
  651. Private Sub Form_Resize()
  652.   On Error Resume Next
  653.   If gbSettingDataCtl = True Then Exit Sub
  654.   If mbResizing = True Then Exit Sub
  655.   Dim nHeight As Integer
  656.   Dim i As Integer
  657.   Dim nTotalWidth As Integer
  658.   mbResizing = True
  659.   If Me.WindowState <> 1 And lblFieldName(0).Visible = True Then 'not minimized
  660.     'make sure the form is lined up on a field
  661.     nHeight = Me.Height
  662.     If (nHeight - 1320) Mod mnCTLARRAYHEIGHT <> 0 Then
  663.       Me.Height = ((nHeight - 1280) \ mnCTLARRAYHEIGHT) * mnCTLARRAYHEIGHT + 1280
  664.     End If
  665.     'resize the status bar
  666.     datDataCtl.Top = Me.Height - 650
  667.     'resize the scrollbar
  668.     vsbScrollBar.Height = Me.Height - 1300
  669.     vsbScrollBar.Left = Me.Width - 360
  670.     If mrecFDS.Fields.Count > 10 Then
  671.       picFields.Width = Me.Width - 260
  672.       nTotalWidth = vsbScrollBar.Left - 20
  673.     Else
  674.       picFields.Width = Me.Width - 20
  675.       nTotalWidth = Me.Width - 50
  676.     End If
  677.     picButtons.Width = Me.Width - 20
  678.     'widen the fields if possible
  679.     For i = 0 To mrecFDS.Fields.Count - 1
  680.       lblFieldName(i).Width = 0.3 * nTotalWidth - 100
  681.       maFldArr(i).Left = lblFieldName(i).Width + 200
  682.       If datDataCtl.Recordset.Fields(i).Type > 9 Then
  683.         maFldArr(i).Width = 0.7 * nTotalWidth - 270
  684.       End If
  685.     Next
  686.     lblFieldValue.Left = maFldArr(0).Left
  687.   End If
  688.   mbResizing = False
  689. End Sub
  690. Private Function GetFieldWidth(rnType As Integer)
  691.   'determines the form control width
  692.   'based on the field type
  693.   Select Case rnType
  694.     Case dbBoolean
  695.       GetFieldWidth = 850
  696.     Case dbByte
  697.       GetFieldWidth = 650
  698.     Case dbInteger
  699.       GetFieldWidth = 900
  700.     Case dbLong
  701.       GetFieldWidth = 1100
  702.     Case dbCurrency
  703.       GetFieldWidth = 1800
  704.     Case dbSingle
  705.       GetFieldWidth = 1800
  706.     Case dbDouble
  707.       GetFieldWidth = 2200
  708.     Case dbDate
  709.       GetFieldWidth = 2000
  710.     Case dbText
  711.       GetFieldWidth = 3250
  712.     Case dbMemo
  713.       GetFieldWidth = 3250
  714.     Case Else
  715.       GetFieldWidth = 3250
  716.   End Select
  717. End Function
  718. Private Sub LoadFields()
  719.    Dim recTmp As Recordset
  720.    Dim nFldType As Integer
  721.    Dim i As Integer
  722.    On Error GoTo LoadFieldsErr
  723.    Set mrecFDS = datDataCtl.Recordset
  724.    Set recTmp = mrecFDS
  725.    'load the controls on the recordset form
  726.    mnNumFields = recTmp.Fields.Count
  727.    ReDim maFldArr(mnNumFields) As Object
  728.    lblFieldName(0).Visible = True
  729.    nFldType = recTmp.Fields(0).Type
  730.    If nFldType = dbBoolean Then
  731.      Set maFldArr(0) = chkFieldData(0)
  732.    ElseIf nFldType = dbLongBinary Then
  733.      If datDataCtl.Tag = "OLE" Then
  734.        Set maFldArr(0) = oleFieldData(0)
  735.      Else
  736.        Set maFldArr(0) = picFieldData(0)
  737.      End If
  738.    Else
  739.      Set maFldArr(0) = txtFieldData(0)
  740.    End If
  741.    maFldArr(0).Visible = True
  742.    maFldArr(0).Top = 0
  743.    maFldArr(0).Width = GetFieldWidth(nFldType)
  744.    If nFldType = dbText Then maFldArr(0).MaxLength = recTmp.Fields(0).Size
  745.    maFldArr(0).TabIndex = 0
  746.    On Error Resume Next
  747.    For i = 1 To recTmp.Fields.Count - 1
  748.      picFields.Height = picFields.Height + mnCTLARRAYHEIGHT
  749.      Load lblFieldName(i)
  750.      lblFieldName(i).Top = lblFieldName(i - 1).Top + mnCTLARRAYHEIGHT
  751.      lblFieldName(i).Visible = True
  752.      nFldType = recTmp.Fields(i).Type
  753.      If nFldType = dbBoolean Then
  754.        Load chkFieldData(i)
  755.        Set maFldArr(i) = chkFieldData(i)
  756.      ElseIf nFldType = dbLongBinary Then
  757.        If datDataCtl.Tag = "OLE" Then
  758.          Load oleFieldData(i)
  759.          Set maFldArr(i) = oleFieldData(i)
  760.        Else
  761.          Load picFieldData(i)
  762.          Set maFldArr(i) = picFieldData(i)
  763.        End If
  764.      Else
  765.        Load txtFieldData(i)
  766.        Set maFldArr(i) = txtFieldData(i)
  767.      End If
  768.      maFldArr(i).Top = maFldArr(i - 1).Top + mnCTLARRAYHEIGHT
  769.      maFldArr(i).Visible = True
  770.      maFldArr(i).Width = GetFieldWidth(nFldType)
  771.      maFldArr(i).TabIndex = i
  772.      If nFldType = dbText Then maFldArr(i).MaxLength = recTmp.Fields(i).Size
  773.    Next
  774.    On Error GoTo LoadFieldsErr
  775.    'resize main window
  776.    picFields.Top = picButtons.Top + picButtons.Height
  777.    mnFieldTop = picFields.Top
  778.    vsbScrollBar.Value = mnFieldTop
  779.    If i <= 10 Then
  780.      Height = i * mnCTLARRAYHEIGHT + 1500
  781.      vsbScrollBar.Visible = False
  782.    Else
  783.      Height = 4500
  784.      Width = Width + 260
  785.      vsbScrollBar.Visible = True
  786.      vsbScrollBar.Min = mnFieldTop
  787.      vsbScrollBar.Max = mnFieldTop - (i * mnCTLARRAYHEIGHT) + 3000
  788.    End If
  789.    'display the field names
  790.    For i = 0 To recTmp.Fields.Count - 1
  791.      lblFieldName(i).Caption = recTmp.Fields(i).Name & ":"
  792.    Next
  793.    'bind the controls
  794.    On Error Resume Next   'bind even if table is empty
  795.    For i = 0 To recTmp.Fields.Count - 1
  796.      maFldArr(i).DataField = recTmp.Fields(i).Name
  797.    Next
  798.    Exit Sub
  799. LoadFieldsErr:
  800.    ShowErrMsg
  801.    Exit Sub
  802. End Sub
  803. Private Sub cmdRefresh_Click()
  804.   On Error GoTo RefErr
  805.   datDataCtl.Refresh
  806.   Exit Sub
  807. RefErr:
  808.   ShowErrMsg
  809.   Exit Sub
  810. End Sub
  811. Private Sub SetRecNum()
  812.   On Error GoTo SRErr
  813.   Dim sCurrStat As String
  814.   Dim lCurrRec As Long
  815.   Dim bNoInd As Integer
  816.   If datDataCtl.EditMode <> dbEditAdd Then
  817.     If datDataCtl.Recordset.BOF = True Then
  818.       sCurrStat = "Record BOF of " & mlNumRows
  819.     ElseIf datDataCtl.Recordset.EOF = True Then
  820.       sCurrStat = "Record EOF of " & mlNumRows
  821.     Else
  822.       'check to see if a table w/ 0 indexes is in use
  823.       If datDataCtl.Recordset.Type = dbOpenTable Then
  824.         If datDataCtl.Database(datDataCtl.RecordSource).Indexes.Count = 0 Then
  825.           bNoInd = True
  826.         End If
  827.       End If
  828.       'if there are no indexes on a table or the recset if ForwardOnly
  829.       'then the PercentPosition is unavailable
  830.       If bNoInd = True Then
  831.         sCurrStat = mlNumRows & " Rows"
  832.       ElseIf (datDataCtl.Options And dbForwardOnly) = dbForwardOnly Then
  833.         sCurrStat = mlNumRows & " Rows (Forward Only Recordset)"
  834.       Else
  835.         lCurrRec = (mlNumRows * (datDataCtl.Recordset.PercentPosition * 0.01)) + 1
  836.         sCurrStat = "Row " & lCurrRec & " of " & mlNumRows
  837.       End If
  838.     End If
  839.     If datDataCtl.Recordset.Updatable = False Then sCurrStat = sCurrStat & "  [Not Updatable]"
  840.     datDataCtl.Caption = sCurrStat
  841.   End If
  842.   'reset buttons if needed
  843.   If datDataCtl.EditMode <> dbEditAdd Then
  844.     cmdCancelAdd.Visible = False
  845.     cmdAdd.Visible = True
  846.   End If
  847.   Exit Sub
  848. SRErr:
  849.   If Err <> 3021 Then
  850.     ShowErrMsg
  851.   End If
  852.   Exit Sub
  853. End Sub
  854. Private Sub cmdUpdate_Click()
  855.   On Error GoTo UpdErr
  856.   Dim bAddFlag As Integer
  857.   Dim nDelay As Long
  858.   Dim nRetryCnt As Integer
  859.   bAddFlag = datDataCtl.EditMode
  860.   If datDataCtl.EditMode = dbEditAdd Then
  861.     If MsgBox("Save New Record?", mnMSGBOX_TYPE) = mnMSGBOX_YES Then
  862.       SetHourglass
  863. RetryUpd1:
  864.       datDataCtl.UpdateRecord
  865.       mlNumRows = mlNumRows + 1
  866.     End If
  867.   Else
  868.     If MsgBox("Commit Changes?", mnMSGBOX_TYPE) = mnMSGBOX_YES Then
  869.       SetHourglass
  870. RetryUpd2:
  871.       datDataCtl.UpdateRecord
  872.     End If
  873.   End If
  874.   If bAddFlag = dbEditAdd Then
  875.     mrecFDS.MoveLast
  876.   End If
  877.   DBEngine.Idle dbFreeLocks
  878.   Screen.MousePointer = vbDefault
  879.   Exit Sub
  880. UpdErr:
  881.   If Err = 3260 And nRetryCnt < gnMURetryCnt Then
  882.     nRetryCnt = nRetryCnt + 1
  883.     datDataCtl.Recordset.Bookmark = datDataCtl.Recordset.Bookmark   'Cancel the update
  884.     DBEngine.Idle dbFreeLocks
  885.     nDelay = Timer
  886.     'Wait gnMUDelay seconds
  887.     While Timer - nDelay < gnMUDelay
  888.       'do nothing
  889.     Wend
  890.     If datDataCtl.EditMode = dbEditAdd Then
  891.       Resume RetryUpd1
  892.     Else
  893.       Resume RetryUpd2
  894.     End If
  895.   Else
  896.     Screen.MousePointer = vbDefault
  897.     ShowErrMsg
  898.     Exit Sub
  899.   End If
  900. End Sub
  901. Private Sub ShowErrMsg()
  902.   MsgBox "Error:" & Err & " " & Error
  903. End Sub
  904.