home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbdb / definedb.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-09-06  |  20.0 KB  |  667 lines

  1. VERSION 2.00
  2. Begin Form DefineDatabase 
  3.    BorderStyle     =   3  'Fixed Double
  4.    Caption         =   "Define Database"
  5.    ClientHeight    =   3930
  6.    ClientLeft      =   2040
  7.    ClientTop       =   1905
  8.    ClientWidth     =   5760
  9.    ControlBox      =   0   'False
  10.    Height          =   4335
  11.    Left            =   1980
  12.    LinkMode        =   1  'Source
  13.    LinkTopic       =   "Form1"
  14.    MaxButton       =   0   'False
  15.    MinButton       =   0   'False
  16.    ScaleHeight     =   3930
  17.    ScaleWidth      =   5760
  18.    Top             =   1560
  19.    Width           =   5880
  20.    Begin CommandButton Command2 
  21.       Caption         =   "E&xit"
  22.       Height          =   372
  23.       Left            =   1920
  24.       TabIndex        =   12
  25.       Top             =   3480
  26.       Width           =   972
  27.    End
  28.    Begin CommandButton Command1 
  29.       Caption         =   "C&reate Database"
  30.       Height          =   372
  31.       Left            =   120
  32.       TabIndex        =   11
  33.       Top             =   3480
  34.       Width           =   1572
  35.    End
  36.    Begin TextBox Text1 
  37.       BackColor       =   &H00C0C0C0&
  38.       Enabled         =   0   'False
  39.       Height          =   732
  40.       Left            =   -120
  41.       TabIndex        =   23
  42.       Top             =   3360
  43.       Width           =   6012
  44.    End
  45.    Begin Frame Fields 
  46.       Caption         =   "Fields"
  47.       Height          =   2292
  48.       Left            =   4080
  49.       TabIndex        =   13
  50.       Top             =   960
  51.       Width           =   1572
  52.       Begin CommandButton Chk_Fld 
  53.          Caption         =   "&Validate"
  54.          Height          =   372
  55.          Left            =   360
  56.          TabIndex        =   17
  57.          Top             =   1800
  58.          Width           =   852
  59.       End
  60.       Begin CommandButton Clear_Fld 
  61.          Caption         =   "&Clear"
  62.          Height          =   372
  63.          Left            =   360
  64.          TabIndex        =   16
  65.          Top             =   1320
  66.          Width           =   852
  67.       End
  68.       Begin CommandButton Last_Fld 
  69.          Caption         =   "&Last"
  70.          Height          =   372
  71.          Left            =   360
  72.          TabIndex        =   15
  73.          Top             =   840
  74.          Width           =   852
  75.       End
  76.       Begin CommandButton Next_Fld 
  77.          Caption         =   "&Next"
  78.          Height          =   372
  79.          Left            =   360
  80.          TabIndex        =   14
  81.          Top             =   360
  82.          Width           =   852
  83.       End
  84.    End
  85.    Begin Frame Frame1 
  86.       Caption         =   "Define Fields"
  87.       Height          =   2292
  88.       Left            =   120
  89.       TabIndex        =   0
  90.       Top             =   960
  91.       Width           =   3852
  92.       Begin TextBox Fld_Dec 
  93.          Enabled         =   0   'False
  94.          Height          =   372
  95.          Left            =   2040
  96.          TabIndex        =   8
  97.          Top             =   1680
  98.          Width           =   612
  99.       End
  100.       Begin TextBox Fld_Log 
  101.          Enabled         =   0   'False
  102.          Height          =   372
  103.          Left            =   1320
  104.          TabIndex        =   9
  105.          Top             =   1680
  106.          Width           =   612
  107.       End
  108.       Begin TextBox Fld_Len 
  109.          Height          =   372
  110.          Left            =   2040
  111.          TabIndex        =   5
  112.          Top             =   1200
  113.          Width           =   612
  114.       End
  115.       Begin TextBox WhichFld 
  116.          Height          =   372
  117.          Left            =   1320
  118.          TabIndex        =   19
  119.          Text            =   "1"
  120.          Top             =   1200
  121.          Width           =   612
  122.       End
  123.       Begin ComboBox Fld_Type 
  124.          Height          =   288
  125.          Left            =   1320
  126.          Sorted          =   -1  'True
  127.          TabIndex        =   4
  128.          Text            =   "Combo1"
  129.          Top             =   732
  130.          Width           =   2292
  131.       End
  132.       Begin TextBox Fld_Name 
  133.          Height          =   372
  134.          Left            =   1320
  135.          TabIndex        =   1
  136.          Top             =   252
  137.          Width           =   2292
  138.       End
  139.       Begin Label Label4 
  140.          Caption         =   "&Decimals"
  141.          Height          =   252
  142.          Left            =   2760
  143.          TabIndex        =   7
  144.          Top             =   1800
  145.          Width           =   852
  146.       End
  147.       Begin Label Label5 
  148.          Alignment       =   1  'Right Justify
  149.          Caption         =   "Lo&gical"
  150.          Height          =   252
  151.          Left            =   480
  152.          TabIndex        =   10
  153.          Top             =   1800
  154.          Width           =   732
  155.       End
  156.       Begin Label Label3 
  157.          Caption         =   "L&ength"
  158.          Height          =   252
  159.          Left            =   2760
  160.          TabIndex        =   6
  161.          Top             =   1320
  162.          Width           =   612
  163.       End
  164.       Begin Label Label6 
  165.          Alignment       =   1  'Right Justify
  166.          Caption         =   "&Field no."
  167.          Height          =   252
  168.          Left            =   360
  169.          TabIndex        =   18
  170.          Top             =   1320
  171.          Width           =   852
  172.       End
  173.       Begin Label Label2 
  174.          Alignment       =   1  'Right Justify
  175.          Caption         =   "Field &Type"
  176.          Height          =   252
  177.          Left            =   240
  178.          TabIndex        =   3
  179.          Top             =   768
  180.          Width           =   972
  181.       End
  182.       Begin Label Label1 
  183.          Alignment       =   1  'Right Justify
  184.          Caption         =   "Field &Name"
  185.          Height          =   252
  186.          Left            =   120
  187.          TabIndex        =   2
  188.          Top             =   324
  189.          Width           =   1092
  190.       End
  191.    End
  192.    Begin Frame Frame2 
  193.       Caption         =   "DBF Name"
  194.       Height          =   732
  195.       Left            =   120
  196.       TabIndex        =   20
  197.       Top             =   120
  198.       Width           =   5532
  199.       Begin TextBox DBF_Name 
  200.          Height          =   372
  201.          Left            =   2040
  202.          TabIndex        =   22
  203.          Text            =   "NEW.DBF"
  204.          Top             =   240
  205.          Width           =   3012
  206.       End
  207.       Begin Label Label7 
  208.          Alignment       =   1  'Right Justify
  209.          Caption         =   "Data&base name"
  210.          Height          =   252
  211.          Left            =   480
  212.          TabIndex        =   21
  213.          Top             =   360
  214.          Width           =   1452
  215.       End
  216.    End
  217. 'Code for Visual Basic 1.0 and Windows 3.0
  218. '(C)1991 Marquis Computing. All Rights Reserved.
  219. 'Form DefintDatabase
  220. '-------------------
  221. 'Gets user input, performs entry validation, verifies structure and builds
  222. 'a FldInfo type array that may be used by other VBDB^ routines to create
  223. 'a dBASE* III+ compatible database.
  224. '^VBDB(C)1991 Marquis Computing, parts (C)1990 Marquis Computing.
  225. ' VBDB is a tradmark of Marquis Computing.
  226. '*dBASE and dBASE III+ are trademarks of Ashton-Tate.
  227.     DefInt A-Z
  228.     '
  229.     'Fld() is an array which carries information to define a database
  230.     'header. The constants are used to make using this easier. Fld() is
  231.     'defined as follows --
  232.     '
  233.     'Element #:
  234.     '
  235.     '           1 = decimal flag (-1 or 0)
  236.     '           2 = field length
  237.     '           3 = field type (String * 1)
  238.     '           4 = field name (String * 12)
  239.     '           5 = location of field
  240.     '
  241.     Const Decimal = 1
  242.     Const FLen = 2
  243.     Const FType = 3
  244.     Const FName = 4
  245.     Const OffSet = 5
  246.     Dim Fld() As String
  247. Sub Chk_Fld_Click ()
  248.     '
  249.     'Verifies a flieds data
  250.     '
  251.     ThisFld = Val(WhichFld.Text)
  252.     VerifyData Status, ThisFld
  253.     If Not Status Then MsgBox "field definition ok", 64, "Validate Fields"
  254. End Sub
  255. Sub Clear_Fld_Click ()
  256.     '
  257.     'Clears out a fields data. If the current field is the last field,
  258.     'and has data in it, then the data is reset to 0 (character
  259.     'with length 0 and null field name), if the field is already 0,
  260.     'and this is the last fiels, it is erased and focus is given to
  261.     'the next field UNDER this one.
  262.     '
  263.     ThisFld = Val(WhichFld.Text)            'get this field no.
  264.     ResetFld ThisFld                        'blow it out
  265.     Fld_Name.SetFocus                       'set focus to name
  266.     Fld_Name.SelStart = 0
  267.     Fld_Name.Sellength = 256
  268.     LastFld = GetLastFld()                  'get the last fld no.
  269.     If LastFld < 1 Then LastFld = 1         'make up for 0 fld no.
  270.     If ThisFld >= LastFld Then              'is this the last one?
  271.         ResetFld ThisFld                    'yes, reset it
  272.         DisplayFld LastFld                  're-display it with 0
  273.     End If
  274. End Sub
  275. Sub Command1_Click ()
  276.     '
  277.     'Saves database definition into the Fld() type
  278.     'and then create a database.
  279.     '
  280.     ThisFld = Val(WhichFld.Text)
  281.     SaveFld ThisFld
  282.     LastFld = GetLastFld()
  283.     '--- copy down other fields
  284.     For ThisFld = 1 To LastFld
  285.       DisplayFld ThisFld
  286.       VerifyData Status, ThisFld
  287.       If Status Then Exit Sub
  288.     Next
  289.     '--- pointer wait
  290.     MousePointer = 11
  291.     '--- create a dbf
  292.     Mode = 0    'normal create / open
  293.     DBFName = DBF_Name.Text
  294.     Fld(0, 0) = Str$(LastFld)
  295.     CreateDBF DBFName, DBFHandle, Fld(), Mode, Status
  296.     Unload DefineDatabase
  297. End Sub
  298. Sub Command2_Click ()
  299.     Unload DefineDatabase
  300. End Sub
  301. Sub DisplayFld (ThisFld)
  302.     '
  303.     'Displays a fields data
  304.     '
  305.     '--- figure out field type
  306.     Select Case Left$(Fld(ThisFld, FType), 1)
  307.         Case "C"
  308.             Fld_Type.Text = "Character"
  309.         Case "D"
  310.             Fld_Type.Text = "Date"
  311.         Case "L"
  312.             Fld_Type.Text = "Logical"
  313.         Case "M"
  314.             Fld_Type.Text = "Memo"
  315.         Case "N"
  316.             Fld_Type.Text = "Numeric"
  317.     End Select
  318.     '--- update the basic info regarding this field
  319.     UpDateFlds
  320.     '--- display its data
  321.     Fld_Name.Text = RTrim$(LTrim$(Fld$(ThisFld, FName)))
  322.     WhichFld.Text = Str$(ThisFld)
  323.     Fld_Len.Text = Fld$(ThisFld, FLen)
  324.     '--- set focus to top of form
  325.     Fld_Name.SetFocus
  326.     Fld_Name.SelStart = 0
  327.     Fld_Name.Sellength = 256
  328. End Sub
  329. Sub DoDefError (Msg$)
  330.     '
  331.     'pops up an error message
  332.     '
  333.     MsgBox Msg$, 48, "Define Database Error"
  334. End Sub
  335. Sub Fld_Dec_Change ()
  336.     '
  337.     'Indicate that field info has changed
  338.     '
  339.     Changed = -1
  340. End Sub
  341. Sub Fld_Len_Change ()
  342.     '
  343.     'Indicate that field info has changed
  344.     '
  345.     Changed = -1
  346. End Sub
  347. Sub Fld_Log_Change ()
  348.     '
  349.     'Indicate that field info has changed
  350.     '
  351.     Changed = -1
  352. End Sub
  353. Sub Fld_Name_Change ()
  354.     '
  355.     'Indicate that field info has changed
  356.     '
  357.     Changed = -1
  358. End Sub
  359. Sub Fld_Name_KeyPress (KeyASCII As Integer)
  360.     '
  361.     'On enter key, goto next field
  362.     '
  363.     If KeyASCII = 13 Then
  364.         Fld_Type.SetFocus
  365.         Fld_Type.SelStart = 0
  366.         Fld_Type.Sellength = 256
  367.         KeyASCII = 0
  368.     End If
  369. End Sub
  370. Sub Fld_Name_LostFocus ()
  371.     Fld_Name.Text = UCase$(Fld_Name.Text)
  372. End Sub
  373. Sub Fld_Type_Change ()
  374.     '
  375.     'Indicate that field info has changed
  376.     '
  377.     Changed = -1
  378. End Sub
  379. Sub Fld_Type_KeyPress (KeyASCII As Integer)
  380.     '
  381.     'On enter key, goto next field
  382.     '
  383.     If KeyASCII = 13 Then
  384.         Fld_Len.SetFocus
  385.         Fld_Len.SelStart = 0
  386.         Fld_Len.Sellength = 256
  387.         KeyASCII = 0
  388.     End If
  389. End Sub
  390. Sub Fld_Type_LostFocus ()
  391.     '
  392.     'Make sure data is correct for this type
  393.     '
  394.     UpDateFlds
  395. End Sub
  396. Sub Form_Load ()
  397.     Screen.MousePointer = 11
  398.     '--- center the window
  399.     WinWidth = (Screen.Width - DefineDatabase.Width) \ 2
  400.     WinHieght = (Screen.Height - DefineDatabase.Height) \ 2
  401.     DefineDatabase.Move WinWidth, WinHieght
  402.     '--- build combo box
  403.     Fld_Type.AddItem "Character"
  404.     Fld_Type.AddItem "Numeric"
  405.     Fld_Type.AddItem "Date"
  406.     Fld_Type.AddItem "Logical"
  407.     Fld_Type.AddItem "Memo"
  408.     '--- assign 0 values
  409.     ReDim Fld(0 To 128, 0 To 5)
  410.     dummy = DoEvents()
  411.     ResetFld 1
  412.     '--- reset pointer
  413.     Screen.MousePointer = 0
  414. End Sub
  415. Function GetLastFld ()
  416.     '
  417.     'Returns the last valid field in a field
  418.     'definition array
  419.     '
  420.     For Size = 1 To 128
  421.         If Fld$(Size, FName) = "" Then
  422.             GetLastFld = Size - 1
  423.             If Size - 1 = 0 Then GetLastFld = Size
  424.             Exit For
  425.         End If
  426.     Next
  427. End Function
  428. Sub Last_Fld_Click ()
  429.     '--- is there another field?
  430.     ThisFld = Val(WhichFld.Text)
  431.     If ThisFld < 1 Then
  432.         Fld_Name.SetFocus
  433.         Fld_Name.SelStart = 0
  434.         Fld_Name.Sellength = 256
  435.         Exit Sub
  436.     End If
  437.     '--- save this data
  438.     SaveFld ThisFld
  439.     '--- determine NEXT field
  440.     ThisFld = ThisFld - 1
  441.     If ThisFld <= 0 Then ThisFld = 1
  442.     WhichFld.Text = Str$(ThisFld)
  443.     '--- display next data
  444.     DisplayFld ThisFld
  445. End Sub
  446. Sub Next_Fld_Click ()
  447.     '--- is there another field?
  448.     ThisFld = Val(WhichFld.Text)
  449.     '--- assign THIS field type
  450.     SaveFld ThisFld
  451.     '--- determine NEXT field
  452.     ThisFld = ThisFld + 1
  453.     If ThisFld > 255 Then ThisFld = 255
  454.     WhichFld.Text = Str$(ThisFld)
  455.     '--- display fields data
  456.     DisplayFld ThisFld
  457. End Sub
  458. Sub ResetFld (ThisFld)
  459.     '
  460.     'Resets the field data for a new field
  461.     '
  462.     Fld_Type.Text = "Character"
  463.     Fld_Type.ListIndex = 0
  464.     Fld_Name.Text = String$(12, 0)
  465.     Fld_Len.Text = "0"
  466.     Fld_Len.Enabled = -1
  467.     Fld_Dec.Text = "N/A"
  468.     Fld_Dec.Enabled = 0
  469.     Fld_Log.Text = "N/A"
  470.     Fld_Log.Enabled = 0
  471.     Fld(ThisFld, Decimal) = "0"
  472.     Fld(ThisFld, FLen) = "0"
  473.     Fld(ThisFld, FType) = ""
  474.     Fld(ThisFld, FName) = ""
  475.     DisplayFld ThisFld
  476.     Changed = 0
  477. End Sub
  478. Sub SaveFld (ThisFld)
  479.     '
  480.     'Saves a fld definition into position
  481.     'ThisFld of array
  482.     '
  483.     Fld(ThisFld, Decimal) = Fld_Dec.Text
  484.     Fld(ThisFld, FLen) = Fld_Len.Text
  485.     Fld(ThisFld, FType) = Fld_Type.Text
  486.     Fld(ThisFld, FName) = Fld_Name.Text
  487.     Changed = 0
  488. End Sub
  489. Sub UpDateFlds ()
  490.     '
  491.     'Updates the conditions of the various boxes
  492.     'based on the type of box it its.
  493.     '
  494.     Select Case Fld_Type.Text
  495.         Case "Character"
  496.             Fld_Len.Enabled = -1
  497.             Fld_Len.Text = "0"
  498.             Fld_Dec.Enabled = 0
  499.             Fld_Dec.Text = "N/A"
  500.             
  501.             Fld_Log.Enabled = 0
  502.             Fld_Log.Text = "N/A"
  503.             
  504.         Case "Date"
  505.             Fld_Len.Enabled = 0
  506.             Fld_Len.Text = " 8"
  507.             
  508.             Fld_Dec.Enabled = 0
  509.             Fld_Dec.Text = "N/A"
  510.             
  511.             Fld_Log.Enabled = 0
  512.             Fld_Log.Text = "N/A"
  513.             
  514.         Case "Logical"
  515.             Fld_Len.Enabled = 0
  516.             Fld_Len.Text = " 1"
  517.             
  518.             Fld_Dec.Enabled = 0
  519.             Fld_Dec.Text = "N/A"
  520.             
  521.             Fld_Log.Enabled = 0
  522.             Fld_Log.Text = "TF"
  523.             
  524.         Case "Numeric"
  525.             Fld_Len.Enabled = -1
  526.             Fld_Len.Text = "0"
  527.             Fld_Dec.Enabled = -1
  528.             Fld_Dec.Text = "0"
  529.             
  530.             Fld_Log.Enabled = 0
  531.             Fld_Log.Text = "N/A"
  532.             
  533.         Case "Memo"
  534.             Fld_Len.Enabled = 0
  535.             Fld_Len.Text = " 10"
  536.             Fld_Dec.Enabled = 0
  537.             Fld_Dec.Text = "N/A"
  538.             Fld_Log.Enabled = 0
  539.             Fld_Log.Text = "N/A"
  540.          
  541.     End Select
  542. End Sub
  543. Sub VerifyData (Status, ThisFld)
  544.     '
  545.     'Verifies the forms data. If anything is bogus, returns Status
  546.     'as TRUE (-1) otherwise Status is FALSE (0).
  547.     '
  548.     '--- assume guilt
  549.     Status = -1
  550.     If DBF_Name.Text = "" Then
  551.         DoDefError "file name invalid"
  552.         DBF_Name.SetFocus
  553.         DBF_Name.SelStart = 0
  554.         DBF_Name.Sellength = 256
  555.         Exit Sub
  556.     End If
  557.     '--- check for name
  558.     fldname$ = Fld_Name.Text
  559.     If fldname$ = "" Then
  560.         DoDefError "field name invalid"
  561.         Fld_Name.SetFocus
  562.         Fld_Name.SelStart = 0
  563.         Fld_Name.Sellength = 256
  564.         Exit Sub
  565.     End If
  566.     '--- check names vailidty
  567.     If Not VerifyField(fldname$, ThisFld) Then
  568.         DoDefError "field name invalid"
  569.         Fld_Name.SetFocus
  570.         Fld_Name.SelStart = 0
  571.         Fld_Name.Sellength = 256
  572.         Exit Sub
  573.     End If
  574.     '--- check field name length
  575.     If Len(fldname$) > 12 Then
  576.         DoDefError "field name length invalid"
  577.         Fld_Name.SetFocus
  578.         Fld_Name.SelStart = 0
  579.         Fld_Name.Sellength = 256
  580.         Exit Sub
  581.     End If
  582.     '--- check field length
  583.     If Val(Fld_Len.Text) <= 0 Or Val(Fld_Len.Text) > 255 Then
  584.         DoDefError "field length invalid"
  585.         Fld_Len.SetFocus
  586.         Fld_Len.SelStart = 0
  587.         Fld_Len.Sellength = 256
  588.         Exit Sub
  589.     End If
  590.     '--- verify logical field is two characters
  591.     If Fld_Log.Enabled And Len(Fld_Log.Text) <> 2 Then
  592.         DoDefError "field length invalid"
  593.         Exit Sub
  594.     End If
  595.     Status = 0
  596. End Sub
  597. Function VerifyField (fldname As String, ThisFld)
  598.     '
  599.     'Returns -1(-1) if field name is valid otherwize returns FALSE (0)
  600.     '
  601.     Bad$ = "-!@#$%^&*() "                        'bad items
  602.     Count = Len(Bad$)                           'length
  603.       
  604.     '--- look for bad items
  605.     For X = 1 To Count
  606.       
  607.       If InStr(fldname, Mid$(Bad$, X, 1)) Then  'bad?
  608.         VerifyField = 0                         'set function FALSE
  609.         Exit Function                           'exit
  610.       End If
  611.     Next
  612.     '--- check field name for used
  613.     Count = 0                                   'reset count to 0
  614.     pFldName$ = String$(12, 0)                  'make a DBF fld name string
  615.     LSet pFldName$ = fldname$                   'pad out FldName$
  616.     fldname$ = pFldName$                        'swap 'em
  617.     Do
  618.         
  619.         Count = Count + 1
  620.         
  621.         pFldName$ = Fld(Count, FName)
  622.         If ThisFld <> Count And pFldName$ = fldname$ Then          'name used?
  623.             VerifyField = 0                 'set function FALSE
  624.             Exit Function                       'exit
  625.         Else                                    'name not used
  626.             Exit Do                             'exit loop
  627.         End If
  628.     Loop Until Fld(Count, FName) = "" Or Count = 128
  629.     '--- if you got here the fld is good
  630.     VerifyField = -1
  631. End Function
  632. Function VerifyType ()
  633.     '
  634.     'Check type
  635.     '
  636.     For X = 0 To Fld_Type.ListCount
  637.         If Fld_Type.List(X) = Fld_Type.Text Then
  638.             VerifyType = -1                     'set to no errors on
  639.             Exit Function                       'form & exit.
  640.         End If
  641.     Next
  642.     Fld_Type.SetFocus
  643.     Fld_Type.SelStart = 0
  644.     Fld_Type.Sellength = 256
  645. End Function
  646. Sub WhichFld_Change ()
  647.     '
  648.     'Indicate that field info has changed
  649.     '
  650.     Changed = -1
  651. End Sub
  652. Sub WhichFld_KeyPress (KeyASCII As Integer)
  653.     'set fld
  654.     If KeyASCII = 13 Then                       'enter key?
  655.         
  656.         KeyASCII = 0                            'for VB bug
  657.         ThisFld = Val(WhichFld.Text)            'get fld no.
  658.         
  659.         If ThisFld > 0 And ThisFld < 256 Then   'in range for DBF?
  660.             SaveFld ThisFld                     'save current field data
  661.             DisplayFld ThisFld                  'display selected field
  662.         Else                                    'no
  663.             'DoMAXError 3006                     'set error "invalid field size"
  664.         End If
  665.     End If
  666. End Sub
  667.