home *** CD-ROM | disk | FTP | other *** search
/ Acorn RISC PD-CD 1 / Acorn RISC PD-CD 1.iso / utilities / powerbase / pbasedoc / calculate < prev    next >
Encoding:
Text File  |  1993-08-30  |  14.0 KB  |  262 lines

  1.                         Performing Calculations
  2.                         =======================
  3.  
  4. There are two mechanisms for performing calculations in Powerbase:-
  5.  
  6.         (1) Fields which automatically calculate and display a value derived
  7.             from other fields.
  8.         (2) Calculations performed on a column in a report.
  9.  
  10. Self-calculating fields
  11. =======================
  12. Fields defined to be of 'Calculated' type display a numeric result derived
  13. from other fields in the record. When the mouse pointer is moved over
  14. such a field it changes into a shape resembling a pocket calculator. Fields
  15. of 'Composite' type perform a similar function but display a textual "value"
  16. instead of a number. In this case the mouse pointer changes into a large
  17. "plus" sign. It isn't possible to type data directly into either of these
  18. special types of field. The latter do not, strictly, involve "calculations"
  19. at all but because 'Composite' and 'Calculated' fields work in the same way
  20. it is convenient to deal with them together.
  21.  
  22. It is possible to write your own functions in Basic and make use of them in
  23. 'Calculated' and 'Composite' fields. The system variable TIME$ is also
  24. useful in this connection.
  25.  
  26. 'Calculated' fields
  27. -------------------
  28. Click MENU over the field to bring up the 'Field' submenu and move right
  29. over the fourth choice ('Calculations'). You must have 'Manager' access
  30. rights in order to do this, otherwise the menu option is shaded. A dialogue
  31. box appears into which you can type the formula to be used to calculate the
  32. value of the field.
  33.  
  34. Suppose we have four normal numeric fields whose tags are No1, No2, No3 and
  35. No4 and a Calculated field called AVGE. The dialogue box will initially show:
  36.  
  37.                          AVGE=
  38.  
  39. Complete the formula so that it reads: AVGE=(No1+No2+No3+No4)/4 and click
  40. on 'OK' or type RETURN. From now on, whenever any of the numbers in No1,
  41. No2, No3 and No4 are entered or altered the contents of AVGE will be updated
  42. in accordance with the formula. To make formula entry easier, the dialogue
  43. box works like the Match window i.e. clicking on a field in the main window
  44. whilst the dialogue box is open causes the tag of the field to be copied
  45. into the formula.
  46.  
  47. The default setting is such that entering or altering a formula affects only
  48. those records which are added or altered after the formula entry/change. The
  49. changes can be made retrospective by setting the option button on the 
  50. formula entry window. On clicking OK you will then be asked to confirm that
  51. you want previous records to be made consistent with the formula you have
  52. just entered. Changes affect the current subfile only, but can be easily
  53. implemented in other subfiles by changing to the required subfile, calling
  54. up the formula entry window, selecting the option button and clicking OK.
  55.  
  56. You might think that this process is only applicable to numeric fields, but
  57. you would be wrong! A non-numeric field may be specified in the formula IF
  58. it is linked to a validation table. To be of any use there must be  numeric
  59. data in the column of the validation table immediately following the one to
  60. which the field is linked. (See 'Validation' for a description of
  61. validation tables and field-linking.) Suppose, for example, we have fields
  62. in a student record for A-level exam grades, the field tags being A1, A2,
  63. A3, A4. The grades are non-numeric but they map onto the numeric "points
  64. system" which universities use to control entry. The relationship of grades
  65. to points is as follows:-
  66.                          Grade          Points
  67.                          =====          ======
  68.                            A              10
  69.                            B               8
  70.                            C               6
  71.                            D               4
  72.                            E               2
  73.  
  74. A validation table could be set up with the grades in column 0 and the
  75. equivalent points in column 1. Each of fields A1-A4 would be linked to
  76. column 0. A further field of type 6 (Calculated) would be created to hold
  77. the points score. Let's suppose this has the tag PTS. Use the method given
  78. above to associate this field with the formula PTS=A1+A2+A3+A4. From now on
  79. entering or changing the letter grades in A1-A5 will cause the points score
  80. to be updated in PTS.
  81.  
  82. A further feature of Calculated fields is their use with Time fields (by
  83. which we mean editable Time fields in the 'Data' class, not the current time
  84. of day field provided in the 'Stamp' class (see below). If the tag of such a
  85. field is included in a calculation formula it will be converted into
  86. seconds. Thus a Calculated field (DIFF) could use two Time fields, TIM1 and
  87. TIM2, in the formula DIFF=TIM1-TIM2 and Powerbase would keep DIFF updated to
  88. show the difference in seconds between the two times. 
  89.  
  90. 'Composite' fields
  91. ------------------
  92. Click MENU over the field to bring up the 'Field' submenu and move right
  93. over the fifth choice ('Combine fields'). You must have 'Manager' access
  94. rights in order to do this, otherwise the menu option is shaded. A dialogue
  95. box appears into which you can type the formula to be used to "calculate"
  96. the value of the field. Formula entry is similar to that for 'Calculated'
  97. fields except that only string operations are allowed. The one most
  98. frequently used will be '+' but you may use the BASIC statements LEFT$,
  99. RIGHT$ and MID$, as well as literal strings. 
  100.  
  101. As an example of use you might have three fields in a database called SNAM,
  102. FNAM and MNAM for surname, forename and middle name. Addressing letters
  103. requires another field containing just the first two of these, but in the
  104. reverse order. You could therefore define a 'Composite' field called NAME
  105. and enter the formula:
  106.                          NAME=FNAM+" "+SNAM
  107.  
  108. Note the use of a literal string to place a space between the names.
  109.  
  110. Composite fields, like Calculated fields, may be used in conjunction with
  111. Time fields to perform genuine "clock arithmetic". Referring to the example
  112. above, using DIFF=TIM1-TIM2, if we attached the formula to a Composite
  113. field the latter would show the difference in hours, minutes and seconds.
  114. The method can be extended to jobs such as averaging times, e.g. with a
  115. formula such as AVE=(TIM1+TIM2+TIM3)/3.
  116.  
  117. As with 'Calculated' fields updating occurs after editing a field whose tag
  118. appears in the attached formula. Thus NAME would be updated after changes to
  119. FNAM or SNAM and DIFF after changes to TIM1 or TIM2. You can, however, have
  120. 'Composite' fields which make use of the Basic system variable TIME$. Thus a
  121. fied DAY could be linked to the formula DAY=LEFT$(TIME$,3) to make it show
  122. Mon, Tue etc. No field tags are referred to in the formula so when does DAY
  123. get updated? The answer for this, or any other 'Composite' field using
  124. TIME$, is that updating occurs immediately before displaying the record so
  125. that the information is correct at that time.
  126.  
  127. User functions
  128. ==============
  129. These are functions, written in Basic, which accept field tags as parameters
  130. and can be included in the calculation formulae of self-calculating fields.
  131. All such functions must return only their principal value - RETURN variables
  132. in the parameter list aren't allowed. The name of each function must begin
  133. with an upper-case 'U', e.g. FNUaverage. This is to avoid duplication of
  134. function names which already appear in Powerbase.
  135.  
  136. All the user functions to be used by a database must be included in a Basic
  137. program called UserFuncs which is stored in the database directory (not in
  138. the Powerbase directory). When the database is opened UserFuncs will be
  139. loaded as a LIBRARY and Powerbase can call the user functions just as
  140. readily as on its own functions.
  141.  
  142. The distribution disc includes a UserFuncs file containing two examples of
  143. user functions kindly submitted by David Lenthall. Users are invited
  144. to submit other user functions for possible inclusion in the function
  145. library. FNUnow (which takes no parameters) operates on the value of TIME$
  146. from the real-time clock and returns a string containing the current date in
  147. form DD-MM-YY. A record design could include a field of 'Composite' type, at
  148. least eight characters long and tagged as, for example, DATE. Associating
  149. the field with the formula DATE=FNUnow would cause the field to display the
  150. current date at all times.
  151.  
  152. The second function, FNUageinyrs accepts two parameters, each of which
  153. should be the tag of a field which holds a date in DD-MM-YY format, and
  154. returns the difference between the dates to the nearest year. The first date
  155. should be the earlier of the two. If your record design has a field for Date
  156. of Birth (let's call its tag DOB) you can use this function together with
  157. FNUnow to make a 'Composite' field (AGE) display a person's age in years by
  158. using the formula AGE=FNUageinyrs(DOB,FNUnow).
  159.  
  160. Considerable care is needed in constructing user functions since it is very
  161. easy to make Powerbase generate errors. In particular you must avoid giving
  162. a function a name which is the same as a field tag, or even one which
  163. contains a field tag as a substring. The two functions described could not,
  164. for instance, be used in a database which had a field tagged as "now" or
  165. "age", although "NOW" and "AGE" could be used.
  166.  
  167. 'Calculated' and 'Composite' fields are normally updated when changes are
  168. made to the fields whose tags appear in their calculation formulae. If a
  169. formula contains a user function then the field will be updated after
  170. changes to those fields whose tags appear in the parameter list of the
  171. function.
  172.  
  173. The previous discussion of 'Composite' fields referred to the fact that they
  174. are updated after altering fields whose tags appear in the calculation
  175. formula or, in the case of those using TIME$, immediatly before display.
  176. There is a slight problem in that user functions may use TIME$ without it
  177. appearing in the parameter list of the function - FNUnow is an example of
  178. this. The field AGE in the example given above will only be updated when DOB
  179. is edited. In other words it will show the person's age at the time you
  180. enter his/her date of birth but won't automatically change it a year later.
  181. To make the change automatic you could introduce TIME$ as a parameter in
  182. FNUnow, i.e. make it FNUnow(TIME$) even though the function doesn't need it.
  183. As soon as Powerbase sees TIME$ in the formula it will update AGE. The same
  184. trick of introducing TIME$ as a "dummy" parameter can be used for any user
  185. function associated with a field which you want updating immediately.
  186.  
  187. 'Stamp' fields
  188. --------------
  189. Although not used to perform calculations or string operations mention will
  190. be made here of this special class of field since, like 'Calculated' and
  191. 'Composite' fields, they are non-editable and Powerbase maintains their
  192. contents automatically and distinguishes them by changing the pointer to a
  193. "#" symbol. The contents of such a field may be any of the following:-
  194.  
  195.         (1) the record number in the database file
  196.         (2) a unique number for each record obtained by counting upwards
  197.             from a user-specified base value
  198.         (3) the time of day when a record was first entered
  199.         (4) the date on which a record was first entered. This may be in
  200.             any of the following forms:
  201.  
  202.                 (a) Sun,01 Aug 1993
  203.                 (b) 01-08-93
  204.                 (c) 01-08-1993
  205.  
  206.         (5) the date and time at which a record was first entered.
  207.         (6) the day on which a record was first entered. This may be either
  208.             day of week (e.g. Tue) or of month (e.g. 24).
  209.         (7) the month in which a record was first entered. This may be
  210.             either a string (e.g. Oct) or a number (e.g. 10).
  211.         (8) the year in which a record was first entered
  212.         (9) the date and time at which a record was last altered
  213.        (10) a sprite such as a company logo which appears on every record
  214.  
  215. When creating a field of type (2) you will observe that the fill-in field
  216. labelled 'Numeric min' becomes active. This is to allow you to enter the
  217. base address for the sequence number. Suppose, for example, you want each
  218. record to have a number from, say, 4000 upwards which remains attached only
  219. to the record to which it was originally assigned. To achieve this enter
  220. 4000 in the 'Numeric min' box when creating the field. The first record
  221. entered in the new database will then be given sequence number 4000, the
  222. next will be assigned 4001. Deleting the second record and inserting a new
  223. one gives the new record the number 4002 and leaves the database without a
  224. record with sequence number 4001. Thus the contents of such a field always
  225. indicate the order in which records were initially entered.
  226.  
  227. Fields which display the record number (1) will also indicate the order of
  228. entry until you delete a record and insert a new one. When a record is
  229. deleted (i.e. with the "dustbin" button, not merely moved to another
  230. subfile) its record number is released and will be reused by the next new
  231. record.
  232.  
  233. Displaying a logo on each record 
  234. --------------------------------
  235. The sprite for the logo should have a name not more than four letters long
  236. and should be included in a sprite file called 'UsrSprites'. This file is
  237. placed inside the database directory. When designing the screen select the
  238. 'Logo' type from the 'Stamp' class of fields and use the name of the
  239. required sprite as the tag. When the database is in use the sprite will
  240. appear on every record, provided the UsrSprites file is in place. If not
  241. then the field will appear as a rectangle which is empty apart from the name
  242. of the missing sprite.
  243.  
  244. You may use several logos on the screen at the same time. All the sprites
  245. must be present in UsrSprites.
  246.  
  247. All 'Stamp' fields (except 'Logo') may be included in a report. They may
  248. also be indexed.
  249.  
  250. Calculations on a column of a report
  251. ====================================
  252. There is an option on the 'Print' submenu called 'Numeric fields' which
  253. leads to a dialogue box listing all the fields (if any) in the record which
  254. are of type 3 ('Number') or 6 ('Calculated'). Associated with each field are
  255. four check boxes headed 'Count', 'Sum', ''Average' and 'Standard deviation'.
  256. These boxes are shaded until a field is included in a print selection. They
  257. then become "live" and any or all of them may be chosen by clicking with
  258. SELECT. The information requested is then added to the list footer when
  259. printing takes place.
  260.  
  261.  
  262.