home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / object10 / quote.frm < prev    next >
Text File  |  1994-11-30  |  17KB  |  508 lines

  1. VERSION 2.00
  2. Begin Form frmQuote 
  3.    BorderStyle     =   3  'Fixed Double
  4.    Caption         =   "Quotes"
  5.    ClientHeight    =   6450
  6.    ClientLeft      =   1815
  7.    ClientTop       =   1800
  8.    ClientWidth     =   5715
  9.    Height          =   7140
  10.    Left            =   1755
  11.    LinkTopic       =   "Form2"
  12.    MaxButton       =   0   'False
  13.    ScaleHeight     =   6450
  14.    ScaleWidth      =   5715
  15.    Top             =   1170
  16.    Width           =   5835
  17.    Begin CommandButton cmdExit 
  18.       Caption         =   "Exit"
  19.       Height          =   495
  20.       Left            =   4200
  21.       TabIndex        =   12
  22.       Top             =   5520
  23.       Width           =   1215
  24.    End
  25.    Begin TextBox txtNewPerson 
  26.       Height          =   375
  27.       Left            =   3600
  28.       TabIndex        =   11
  29.       Top             =   4560
  30.       Width           =   1815
  31.    End
  32.    Begin CommandButton cmdAddPerson 
  33.       Caption         =   "Add:"
  34.       Height          =   375
  35.       Left            =   2880
  36.       TabIndex        =   10
  37.       Top             =   4560
  38.       Width           =   615
  39.    End
  40.    Begin CommandButton cmdAddCategory 
  41.       Caption         =   "Add:"
  42.       Height          =   375
  43.       Left            =   240
  44.       TabIndex        =   7
  45.       Top             =   4560
  46.       Width           =   615
  47.    End
  48.    Begin TextBox txtNewCat 
  49.       Height          =   375
  50.       Left            =   960
  51.       TabIndex        =   8
  52.       Top             =   4560
  53.       Width           =   1695
  54.    End
  55.    Begin CommandButton cmdAddQuote 
  56.       Caption         =   "Add Quote"
  57.       Enabled         =   0   'False
  58.       Height          =   375
  59.       Left            =   3360
  60.       TabIndex        =   3
  61.       Top             =   960
  62.       Width           =   1215
  63.    End
  64.    Begin CommandButton cmdDeleteQuote 
  65.       Caption         =   "Delete"
  66.       Height          =   375
  67.       Left            =   3360
  68.       TabIndex        =   4
  69.       Top             =   1440
  70.       Width           =   1215
  71.    End
  72.    Begin Frame Frame2 
  73.       Caption         =   "Quote"
  74.       Height          =   2055
  75.       Left            =   240
  76.       TabIndex        =   16
  77.       Top             =   120
  78.       Width           =   4455
  79.       Begin TextBox TheQuote 
  80.          DataField       =   "quote"
  81.          DataSource      =   "quotedata"
  82.          Height          =   1095
  83.          Left            =   120
  84.          MultiLine       =   -1  'True
  85.          TabIndex        =   2
  86.          Top             =   840
  87.          Width           =   2775
  88.       End
  89.       Begin OraData quotedata 
  90.          AllowMoveLast   =   -1  'True
  91.          AutoBinding     =   -1  'True
  92.          Caption         =   "Reading"
  93.          Connect         =   ""
  94.          DatabaseName    =   ""
  95.          Height          =   270
  96.          HiddenName      =   "quotedata"
  97.          Left            =   120
  98.          Options         =   0
  99.          ReadOnly        =   0   'False
  100.          RecordSource    =   ""
  101.          TabIndex        =   5
  102.          Top             =   360
  103.          TrailingBlanks  =   0   'False
  104.          Width           =   2775
  105.       End
  106.    End
  107.    Begin ListBox perlist 
  108.       Height          =   1395
  109.       Left            =   2880
  110.       TabIndex        =   9
  111.       Top             =   3000
  112.       Width           =   2535
  113.    End
  114.    Begin ListBox catlist 
  115.       Height          =   1395
  116.       Left            =   240
  117.       TabIndex        =   6
  118.       Top             =   3000
  119.       Width           =   2415
  120.    End
  121.    Begin CommandButton cmdDropTables 
  122.       Caption         =   "Drop Tables"
  123.       Height          =   495
  124.       Left            =   2160
  125.       TabIndex        =   1
  126.       Top             =   5520
  127.       Width           =   1455
  128.    End
  129.    Begin CommandButton cmdCreateTables 
  130.       Caption         =   "Create Tables"
  131.       Enabled         =   0   'False
  132.       Height          =   495
  133.       Left            =   480
  134.       TabIndex        =   0
  135.       Top             =   5520
  136.       Width           =   1455
  137.    End
  138.    Begin Frame Frame1 
  139.       Caption         =   "Table Management"
  140.       Height          =   1095
  141.       Left            =   240
  142.       TabIndex        =   13
  143.       Top             =   5160
  144.       Width           =   3615
  145.    End
  146.    Begin Label Label3 
  147.       Caption         =   "Quote Filter:"
  148.       FontBold        =   -1  'True
  149.       FontItalic      =   0   'False
  150.       FontName        =   "MS Sans Serif"
  151.       FontSize        =   9.75
  152.       FontStrikethru  =   0   'False
  153.       FontUnderline   =   -1  'True
  154.       Height          =   255
  155.       Left            =   2040
  156.       TabIndex        =   17
  157.       Top             =   2400
  158.       Width           =   1455
  159.    End
  160.    Begin Label label1 
  161.       Caption         =   "Category:"
  162.       Height          =   255
  163.       Left            =   960
  164.       TabIndex        =   14
  165.       Top             =   2640
  166.       Width           =   1215
  167.    End
  168.    Begin Label Label2 
  169.       Caption         =   "Person:"
  170.       Height          =   255
  171.       Left            =   3600
  172.       TabIndex        =   15
  173.       Top             =   2640
  174.       Width           =   1215
  175.    End
  176.    Begin Menu mFile 
  177.       Caption         =   "&File"
  178.       Begin Menu mFileExit 
  179.          Caption         =   "E&xit"
  180.       End
  181.    End
  182.    Begin Menu mAbout 
  183.       Caption         =   "&About"
  184.       Begin Menu mAboutQuote 
  185.          Caption         =   "About Quote"
  186.       End
  187.    End
  188. End
  189.  
  190. Sub catlist_Click ()
  191.     Call QueryQuotes
  192.  
  193.     ' if we've got a real value, we can enable addnew
  194.     If catlist <> "any" And perlist <> "any" Then
  195.         cmdAddQuote.Enabled = True
  196.     Else
  197.         cmdAddQuote.Enabled = False
  198.     End If
  199.  
  200. End Sub
  201.  
  202. Sub cmdAddCategory_Click ()
  203.  
  204.     If Len(newcat) <= 0 Then
  205.         Exit Sub
  206.     End If
  207.  
  208.     ' we're adding a new category
  209.  
  210.     ' construct the sql statement and add to the database
  211.     ' note that the primary key (catnum) is generated by the trigger
  212.     ' we created when we created the table
  213.     sql$ = "insert into qcats (category) values ('"
  214.     sql$ = sql$ + newcat
  215.     sql$ = sql$ + "')"
  216.     OraDatabase.DbExecuteSQL (sql$)
  217.  
  218.     ' add to the list
  219.     catlist.AddItem newcat
  220.     newcat = ""
  221.  
  222. End Sub
  223.  
  224. Sub cmdAddPerson_Click ()
  225.     If Len(newperson) <= 0 Then
  226.         Exit Sub
  227.     End If
  228.  
  229.     ' we're adding a new category
  230.  
  231.     ' construct the sql statement and add to the database
  232.     ' note that the primary key (catnum) is generated by the trigger
  233.     ' we created when we created the table
  234.     sql$ = "insert into qpersons (pname) values ('"
  235.     sql$ = sql$ + newperson
  236.     sql$ = sql$ + "')"
  237.     OraDatabase.DbExecuteSQL (sql$)
  238.  
  239.     ' add to the list
  240.     perlist.AddItem newperson
  241.     newcat = ""
  242.  
  243. End Sub
  244.  
  245. Sub cmdAddQuote_Click ()
  246.     Dim mydyn As object
  247.  
  248.     thequote.Visible = True ' make sure quote can be typed
  249.     
  250.     ' add a new record to the quote dynaset
  251.     quotedata.Recordset.DbAddNew
  252.  
  253.     ' we want to fill in the catnum and pnum fields
  254.     ' this requires a quick query to the database
  255.     ' we can get both numbers at once
  256.  
  257.     sql$ = "select catnum, pnum from qcats,qpersons where category = '"
  258.     sql$ = sql$ + catlist
  259.     sql$ = sql$ + "' and pname = '"
  260.     sql$ = sql$ + perlist
  261.     sql$ = sql$ + "'"
  262.     Set mydyn = OraDatabase.DbCreateDynaset(sql$) ' get the data
  263.     mydyn.DbMoveFirst                            ' move to first record
  264.     quotedata.Recordset.Fields("catnum").Value = mydyn.Fields("catnum")
  265.     quotedata.Recordset.Fields("pnum").Value = mydyn.Fields("pnum")
  266.  
  267.     ' set the focus to the quote text
  268.     thequote.SetFocus
  269. End Sub
  270.  
  271. Sub cmdCreateTables_Click ()
  272.     
  273.     ' The schema we're creating here is a little more complex than is really
  274.     ' needed for this application.  We've "normalized" the quotes data so that
  275.     ' the repeated person and category information is placed in separate tables.
  276.     ' Here, the extra tables don't have much in them - just a string.  In a real
  277.     ' application there would be more data per row of the extra tables, with a
  278.     ' corresponding increase in the efficiency of storage and access.
  279.     ' The tables are broken out here as an example of one way to write a
  280.     ' multiple-table application.
  281.  
  282.     ' create the quotes table
  283.     sql$ = "create table quotes "
  284.     sql$ = sql$ + "(qnum number not null primary key, "
  285.     sql$ = sql$ + "catnum number not null, "                ' foreign key to category table
  286.     sql$ = sql$ + "pnum number not null, "                  ' foreign key to person table
  287.     sql$ = sql$ + "quote varchar2(200) not null)"           ' the quote
  288.     OraDatabase.DbExecuteSQL (sql$)
  289.  
  290.     ' create a sequence to be used to give the quotes (& persons & categories) unique values
  291.     OraDatabase.DbExecuteSQL ("create sequence quoteseq")
  292.  
  293.     ' create the category table
  294.     sql$ = "create table qcats "
  295.     sql$ = sql$ + "(catnum number not null primary key, "  ' category key
  296.     sql$ = sql$ + "category varchar2(20) not null)"        ' the category
  297.     OraDatabase.DbExecuteSQL (sql$)
  298.  
  299.     ' create the person table
  300.     sql$ = "create table qpersons "
  301.     sql$ = sql$ + "(pnum number not null primary key, "    ' person key
  302.     sql$ = sql$ + "pname varchar2(20) not null)"           ' person's name
  303.     OraDatabase.DbExecuteSQL (sql$)
  304.  
  305.     ' now create triggers on the tables so that the table indices always get unique values on insert
  306.     sql$ = "create trigger quotesinsert "                       ' create a trigger
  307.     sql$ = sql$ + "before insert on quotes for each row "       ' to be fired on each row insert
  308.     sql$ = sql$ + "declare ii number; begin "
  309.     sql$ = sql$ + "select quoteseq.nextval into ii from dual; "  ' get the next value from the sequence
  310.     sql$ = sql$ + ":new.qnum := ii; "                            ' and set our key to it
  311.     sql$ = sql$ + "end;"
  312.     OraDatabase.DbExecuteSQL (sql$)
  313.  
  314.     ' do the same for the other tables
  315.     sql$ = "create trigger qcatsinsert "                       ' create a trigger
  316.     sql$ = sql$ + "before insert on qcats for each row "       ' to be fired on each row insert
  317.     sql$ = sql$ + "declare ii number; begin "
  318.     sql$ = sql$ + "select quoteseq.nextval into ii from dual; "  ' get the next value from the sequence
  319.     sql$ = sql$ + ":new.catnum := ii; "                            ' and set our key to it
  320.     sql$ = sql$ + "end;"
  321.     OraDatabase.DbExecuteSQL (sql$)
  322.     sql$ = "create trigger qpersonsinsert "                       ' create a trigger
  323.     sql$ = sql$ + "before insert on qpersons for each row "       ' to be fired on each row insert
  324.     sql$ = sql$ + "declare ii number; begin "
  325.     sql$ = sql$ + "select quoteseq.nextval into ii from dual; "  ' get the next value from the sequence
  326.     sql$ = sql$ + ":new.pnum := ii; "                            ' and set our key to it
  327.     sql$ = sql$ + "end;"
  328.     OraDatabase.DbExecuteSQL (sql$)
  329.  
  330.     ' fill the category table
  331.     sqla$ = "insert into qcats (category) values "
  332.     sql$ = sqla$ + "('romance')"  ' this will be 1
  333.     OraDatabase.DbExecuteSQL (sql$)
  334.     sql$ = sqla$ + "('animals')"
  335.     OraDatabase.DbExecuteSQL (sql$)
  336.     sql$ = sqla$ + "('politics')"
  337.     OraDatabase.DbExecuteSQL (sql$)
  338.     ' if any are added the quotes table inserts need to be changed (see below)!
  339.  
  340.     ' fill the person table
  341.     sqla$ = "insert into qpersons (pname) values "
  342.     sql$ = sqla$ + "('shakespeare')"  ' this will be sequence number 4
  343.     OraDatabase.DbExecuteSQL (sql$)
  344.     sql$ = sqla$ + "('blake')"
  345.     OraDatabase.DbExecuteSQL (sql$)
  346.     sql$ = sqla$ + "('poe')"
  347.     OraDatabase.DbExecuteSQL (sql$)
  348.     sql$ = sqla$ + "('lincoln')"
  349.     OraDatabase.DbExecuteSQL (sql$)
  350.     sql$ = sqla$ + "('burns')"
  351.     OraDatabase.DbExecuteSQL (sql$)
  352.     sql$ = sqla$ + "('dickinson')"
  353.     OraDatabase.DbExecuteSQL (sql$)
  354.  
  355.     ' fill the quotes table
  356.     ' note that we are using explicit values for the foreign keys (catnum & pnum)
  357.     ' it would be better if we looked up the number for the persons and the categories
  358.     ' so that if another category was added the person numbers would still be valid
  359.     sqla$ = "insert into quotes (catnum, pnum, quote) values "
  360.     sql$ = sqla$ + "(1, 4, 'shall I compare thee to a summer''s day?')"
  361.     OraDatabase.DbExecuteSQL (sql$)
  362.     sql$ = sqla$ + "(2, 5, 'tyger, tyger, burning bright, in the forests of the night')"
  363.     OraDatabase.DbExecuteSQL (sql$)
  364.     sql$ = sqla$ + "(2, 6, 'quoth the raven, ''nevermore''')"
  365.     OraDatabase.DbExecuteSQL (sql$)
  366.     sql$ = sqla$ + "(1, 4, 'let me not to the marriage of true minds admit impediment')"
  367.     OraDatabase.DbExecuteSQL (sql$)
  368.     sql$ = sqla$ + "(1, 8, 'my love is like a red, red rose')"
  369.     OraDatabase.DbExecuteSQL (sql$)
  370.     sql$ = sqla$ + "(2, 4, 'eye of newt and toe of frog, wool of bat and tongue of dog')"
  371.     OraDatabase.DbExecuteSQL (sql$)
  372.     sql$ = sqla$ + "(3, 7, 'public opinion in this country is everything')"
  373.     OraDatabase.DbExecuteSQL (sql$)
  374.     sql$ = sqla$ + "(3, 7, 'the ballot is stronger than the bullet')"
  375.     OraDatabase.DbExecuteSQL (sql$)
  376.     sql$ = sqla$ + "(1, 9, 'i cannot live with you, it would be life, and life is over there, behind the shelf')"
  377.     OraDatabase.DbExecuteSQL (sql$)
  378.     sql$ = sqla$ + "(2, 9, 'bees are black with gilt surcingles, buccaneers of buzz')"
  379.     OraDatabase.DbExecuteSQL (sql$)
  380.  
  381.     ' fix up enabled state
  382.     cmdCreateTables.Enabled = False
  383.     cmdDroptables.Enabled = True
  384.     perlist.Enabled = True
  385.     catlist.Enabled = True
  386.     cmdDeleteQuote.Enabled = True
  387.  
  388.     ' fill the list boxes
  389.     Call FillLists
  390. End Sub
  391.  
  392. Sub cmdDeleteQuote_Click ()
  393.     nrecs% = quotedata.Recordset.RecordCount
  394.     quotedata.Recordset.DbDelete
  395.  
  396.     If nrecs% = 1 Then
  397.         ' we've deleted the only one
  398.         thequote.Visible = False
  399.     Else
  400.         ' let's navigate to another record
  401.         ' the combination of these two always get us
  402.         '   to the next record (or previous if there is no next)
  403.         quotedata.Recordset.DbMovePrevious
  404.         quotedata.Recordset.DbMoveNext
  405.     End If
  406.  
  407. End Sub
  408.  
  409. Sub cmdDropTables_Click ()
  410.     
  411.     ' any errors will probably be that we're dropping something that doesn't exist
  412.     ' just keep going.
  413.     On Error Resume Next
  414.  
  415.     ' drop the quotes tables
  416.     OraDatabase.DbExecuteSQL ("drop table quotes")
  417.     OraDatabase.DbExecuteSQL ("drop table qcats")
  418.     OraDatabase.DbExecuteSQL ("drop table qpersons")
  419.  
  420.     ' drop the triggers and sequence
  421.     OraDatabase.DbExecuteSQL ("drop trigger quotesinsert")
  422.     OraDatabase.DbExecuteSQL ("drop trigger qcatsinsert")
  423.     OraDatabase.DbExecuteSQL ("drop trigger qpersonsinsert")
  424.     OraDatabase.DbExecuteSQL ("drop sequence quoteseq")
  425.  
  426.     ' clear the lists
  427.     catlist.Clear
  428.     perlist.Clear
  429.  
  430.     ' fix up interface
  431.     cmdCreateTables.Enabled = True
  432.     cmdDroptables.Enabled = False
  433.     perlist.Enabled = False
  434.     catlist.Enabled = False
  435.     cmdDeleteQuote.Enabled = False
  436.     thequote = ""
  437.  
  438. End Sub
  439.  
  440. Sub cmdExit_Click ()
  441.  Call mFileExit_Click
  442. End Sub
  443.  
  444. Sub Form_Load ()
  445.     ' set up the data control
  446.     quotedata.Connect = Connect$
  447.     quotedata.DatabaseName = DatabaseName$
  448.  
  449.     On Error GoTo handleerr
  450.  
  451.     ' fill list boxes
  452.     Call FillLists
  453.  
  454.     ' start the quotes
  455.     Call QueryQuotes
  456.  
  457.     Exit Sub
  458.  
  459.  
  460. handleerr:
  461.     ii% = Err
  462.     If Err = 440 Then ' couldn't open dynasets - tables don't exist?
  463.         cmdDroptables.Enabled = False
  464.         cmdCreateTables.Enabled = True
  465.         catlist.Enabled = False
  466.         perlist.Enabled = False
  467.         cmdDeleteQuote.Enabled = False
  468.     Else
  469.         ' don't know what to do with this error
  470.         MsgBox Error$
  471.     End If
  472.     Exit Sub
  473.     
  474. End Sub
  475.  
  476. Sub mAboutQuote_Click ()
  477.  
  478.  frmAbout.Show MODAL
  479.  
  480. End Sub
  481.  
  482. Sub mFileExit_Click ()
  483.  End
  484. End Sub
  485.  
  486. Sub perlist_Click ()
  487.     Call QueryQuotes
  488.  
  489.     ' if we've got a real value, we can enable addnew
  490.     If catlist <> "any" And perlist <> "any" Then
  491.         cmdAddQuote.Enabled = True
  492.     Else
  493.         cmdAddQuote.Enabled = False
  494.     End If
  495.  
  496. End Sub
  497.  
  498. Sub quotedata_Validate (action As Integer, save As Integer)
  499.     If action = 5 Then
  500.         ' starting to add a record
  501.         quotedata.Caption = "Adding"
  502.     ElseIf action = 3 Then
  503.         ' Moving (could be done with adding record)
  504.         quotedata.Caption = "Reading"
  505.     End If
  506. End Sub
  507.  
  508.