home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / database / p4w_all.zip / TI1502.ASC < prev   
Text File  |  1993-05-12  |  11KB  |  397 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Paradox for Windows                   NUMBER  :  1502
  9.   VERSION  :  1.0
  10.        OS  :  WIN
  11.      DATE  :  May 12, 1993                             PAGE  :  1/6
  12.  
  13.     TITLE  :  Performing a Calculation Based on a Calculated Field
  14.  
  15.  
  16.  
  17.  
  18.   Intended Audience:
  19.   ObjectPAL programmers
  20.  
  21.   Prerequisites:
  22.   Basic knowledge of ObjectPAL data structures and control
  23.   structures.  How to enter ObjectPAL code in a built-in method and
  24.   a custom method.  How to create a calculated field.
  25.  
  26.   Purpose of the TI:
  27.   This Technical Information Sheet describes how to perform a
  28.   calculation that is based on a calculated field a method to do
  29.   this.
  30.  
  31.   It is sometimes desirable to perform a calculation that is based
  32.   on a calculated field.  For example, suppose you have a Selling
  33.   Price and a Quantity field in your detail table, you created a
  34.   calculated field in your detail table that calculates Selling
  35.   Price times Quantity for every record in the detail table, and
  36.   now you want to create a calculated field that calculates the sum
  37.   of Selling Price times Quantity.  You will find that you cannot
  38.   accomplish this task with a calculated field.  This Technical
  39.   Information Sheet describes two techniques achieve this result.
  40.  
  41.  
  42.   EXAMPLE 1
  43.  
  44.   The first, and simplest, way to do this is to create a field in
  45.   your detail table called Total, and then create changeValue
  46.   methods on the Quantity and the Selling Price fields which place
  47.   the multiplication of the two fields in your Total field.  You
  48.   can then create a calculated field which summarizes your Total
  49.   field.  A good explanation of how to create the changeValue
  50.   methods on the Selling Price and Quantity Fields may be found in
  51.   Chapter 7 of the Learning ObjectPAL manual in the section titled
  52.   "Supplying values".
  53.  
  54.  
  55.   EXAMPLE 2
  56.  
  57.   The second technique is much more complex and the remainder of
  58.   this Technical Information Sheet is dedicated to it.  The primary
  59.   advantage of this technique is that it does not require you to
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Paradox for Windows                   NUMBER  :  1502
  75.   VERSION  :  1.0
  76.        OS  :  WIN
  77.      DATE  :  May 12, 1993                             PAGE  :  2/6
  78.  
  79.     TITLE  :  Performing a Calculation Based on a Calculated Field
  80.  
  81.  
  82.  
  83.  
  84.   store the total for each record in your table.  The trade-off is
  85.   complex ObjectPAL code to achieve the result.
  86.  
  87.   This example is based on using the Orders and Lineitem tables
  88.   that are installed in your sample directory with Paradox for
  89.   Windows.  The quantity field in the Lineitem table is named
  90.   "Qty", so the remainder of references to the quantity field will
  91.   refer to Qty.
  92.  
  93.      1.  Create a form and link the Orders table to the Lineitem
  94.          table to follow this example.
  95.  
  96.      2.  Create a variable in the var window of the page object of
  97.          your form.  This variable will hold the difference between
  98.          values entered in the current record and the values stored
  99.          in the current record.  The code in your var window should
  100.          look like the following:
  101.  
  102.               Var
  103.                    num number
  104.               endVar
  105.  
  106.      3.  Next, create a changeValue method on the Qty and Selling
  107.          Price fields.  The end result of this method is to
  108.          calculate the difference between the current values
  109.          entered into the Qty and Selling Price fields, and the
  110.          values stored in the table.  This difference is stored in
  111.          the variable you've already created in the var window of
  112.          the page.
  113.  
  114.          Here's the changeValue method for the Selling Price field.
  115.  
  116.               method changeValue(var eventInfo ValueEvent)
  117.               var
  118.                  ar DynArray[] anytype
  119.                  tc TCursor
  120.               endVar
  121.  
  122.               ;This method gets called before the calculated
  123.               ;field. The purpose of this method is to calculate
  124.               ;the difference between the values in the table
  125.               ;and the current values.
  126.           
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Paradox for Windows                   NUMBER  :  1502
  141.   VERSION  :  1.0
  142.        OS  :  WIN
  143.      DATE  :  May 12, 1993                             PAGE  :  3/6
  144.  
  145.     TITLE  :  Performing a Calculation Based on a Calculated Field
  146.  
  147.  
  148.  
  149.  
  150.               doDefault
  151.               tc.attach(self)
  152.  
  153.               ;TCursor is used instead of a UIObject because
  154.               ;TCursor.copyToArray can use a DynArray, and a
  155.               ;DynArray is easier to use.
  156.  
  157.               tc.copyToArray(ar)
  158.  
  159.               ;CopyToArray copies the unposted records to the
  160.               ;array.
  161.  
  162.               tc.close()                    ;close the TCursor
  163.  
  164.               ;if ar.size()>0 then
  165.  
  166.               if not self.recordStatus("new") then
  167.                    num=(self.value*Qty)-(ar["Selling Price"]*
  168.                        ar["Qty"])
  169.  
  170.               ;This means an existing record is being modified.
  171.               ;This calculation calculates the difference
  172.               ;between the posted and unposted values.
  173.  
  174.               else
  175.                    num=self.value*Qty
  176.               ;This means a new record
  177.               endIf
  178.               endmethod
  179.  
  180.  
  181.          NOTE: The semicolon ";" indicates that a comment follows.
  182.  
  183.  
  184.          Here's the changeValue method for the Qty field.  It is
  185.          similar to the changeValue method for the Selling Price
  186.          field.
  187.  
  188.               method changeValue(var eventInfo ValueEvent)
  189.               var
  190.                    ar DynArray[] anytype
  191.                    tc TCursor
  192.               endVar
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Paradox for Windows                   NUMBER  :  1502
  207.   VERSION  :  1.0
  208.        OS  :  WIN
  209.      DATE  :  May 12, 1993                             PAGE  :  4/6
  210.  
  211.     TITLE  :  Performing a Calculation Based on a Calculated Field
  212.  
  213.  
  214.  
  215.  
  216.               ;See the changeValue method of the Selling Price
  217.               ;field for comments on this code.
  218.  
  219.               doDefault
  220.               tc.attach(self)
  221.               tc.copyToArray(ar)
  222.               tc.close()
  223.  
  224.               ;if ar.size()>0 then
  225.  
  226.               if not self.recordStatus("new") then
  227.                    num=(self.value*Selling_Price)-
  228.                        (ar["Selling Price"]*ar["Qty"])
  229.               else
  230.                    num=self.value*Selling_Price
  231.               endIf
  232.               endmethod
  233.  
  234.  
  235.      4.  Next, attach the following code to the action method of
  236.          your Lineitem table object.  This code makes the variable
  237.          holding the difference between the posted value in the
  238.          table and the value entered into the current record zero
  239.          if the record is written to disk.
  240.  
  241.               method action(var eventInfo ActionEvent)
  242.               if eventInfo.id()=dataPostRecord or
  243.                  eventInfo.id()=dataUnlockRecord then
  244.                  num=0
  245.               ;If the record is posted, make the difference
  246.               ;between the posted and the unposted equal to 0.
  247.               endIf
  248.               endmethod
  249.  
  250.      5.  Create a field object to contain the sum of Selling Price
  251.          times Qty.  Inspect the field, and give it a name of Calc.
  252.  
  253.      6.  Create a custom method attached to your table object.
  254.          Regardless of what else it does, this custom method always
  255.          returns a value of 1.  To improve efficiency, if the
  256.          current record is not a modified record or the first
  257.          record of the table, this method returns a value of 1.  If
  258.          the current record is modified, or is the first record of
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Paradox for Windows                   NUMBER  :  1502
  273.   VERSION  :  1.0
  274.        OS  :  WIN
  275.      DATE  :  May 12, 1993                             PAGE  :  5/6
  276.  
  277.     TITLE  :  Performing a Calculation Based on a Calculated Field
  278.  
  279.  
  280.  
  281.  
  282.          the table, it first performs a scan loop and calculates
  283.          the result of Selling Price times Qty for all records
  284.          posted to the table.  Then, it adds in the number
  285.          calculated as the difference between the current posted
  286.          record, and entered values, and enters that value into a
  287.          field named Calc.
  288.  
  289.  
  290.               method doit() number
  291.               var
  292.                    tc   tcursor
  293.                    temp number
  294.               endVar
  295.  
  296.               ;This custom method is called by the calculated
  297.               ;field.  It calculates the formula for all existing
  298.               ;records and then adds in the difference between the
  299.               ;posted and unposted values for the current record
  300.               ;which the changeValue method calculated.  To
  301.               ;maximize performance performance, if you are not on
  302.               ;the first record or a modified record, nothing needs
  303.               ;to be done.
  304.  
  305.               if self.recNo<>1 and not
  306.                  self.recordStatus("modified") then
  307.                  return 1
  308.               endIf
  309.               temp=0
  310.  
  311.               ;Process all records that currently written to disk
  312.  
  313.               tc.attach(self)
  314.               scan tc:
  315.                    temp=tc.Qty*tc."Selling Price"+temp
  316.               endScan
  317.               tc.close()
  318.  
  319.               ;Num is a variable in the var window of the Lineitem
  320.               ;table.  It is calculated in the changeValue method
  321.               ;of the Qty and Selling Price fields.
  322.  
  323.               if num.isAssigned() then
  324.                    calc=temp+num
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.   PRODUCT  :  Paradox for Windows                   NUMBER  :  1502
  339.   VERSION  :  1.0
  340.        OS  :  WIN
  341.      DATE  :  May 12, 1993                             PAGE  :  6/6
  342.  
  343.     TITLE  :  Performing a Calculation Based on a Calculated Field
  344.  
  345.  
  346.  
  347.  
  348.               else
  349.                    calc=temp
  350.               endIf
  351.           
  352.               ;Reinitialize the difference between the posted and
  353.               ;unposted and unposted values back to 0.
  354.  
  355.               num=0
  356.               return 1
  357.               endmethod
  358.  
  359.      7.  Finally, create your calculated field that calculates Qty
  360.          times Selling Price for each record.  The calculation
  361.          should look as follows:
  362.  
  363.               [file."Selling Price"]*[file.Qty]*doit()
  364.  
  365.  
  366.   Note to users with a modem: a form that contains a working
  367.   example of this code may be found on the Borland Download BBS and
  368.   in Library 4 of the PDOXWIN forum in CompuServe.  The filename is
  369.   CALC.ZIP.  For information on Borland's Online Services, refer to
  370.   Technical Information Sheet 9604.
  371.  
  372.  
  373.   DISCLAIMER: You have the right to use this technical information
  374.   subject to the terms of the No-Nonsense License Statement that
  375.   you received with the Borland product to which this information
  376.   pertains.
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.