Subject Re: beforeGetValue event
From Al W <tdwsoh@aol.com>
Date Thu, 14 Dec 2017 10:46:24 -0500
Newsgroups dbase.getting-started
Attachment(s) MERV 3.docx

Hi Mervyn Bick
I have attempted to follow your leadership and used the following 4 steps derived from your text and creating a whatever table..
1.        If you are using the calculated field in a form a slightly different approach is appropriate. In a form a good place to create a calculated field is in a query's onOpen event handler.
2.        After you have placed a table on the form use the arrow button of the combobox on the Inspector to display the list of objects. Select the query object.  dBASE names queries with the tablename plus an ordinal number.  tablename1, tablename2, etc although you are free to change object names to suit yourself.
3.        Select the "event" tab and click on "onOpen" to select it.  Click on the "spanner" button and dBASE will open the sourcecode editor with an empty onOpen event handler.  Add the code, close the sourcecode editor and save the form.
4.        If TOTAL is in the table but not populated then
function WHATEVERTABLE1_onOpen()
    this.rowset.fields["total"].beforeGetValue := {||this.parent["wage
rate1"].value + this.parent["fringes1"].value}
    return

After implementing my version of these steps I still don’t get it to work. The attached copy of the file from the Source Editor/Designer shows where I am now.
Hope you can still help me
Thanks
AL W




Mervyn Bick Wrote:

> On 2017-11-23 12:39 AM, Al W wrote:
> > I\\\'M WORKING IN DBASE 11 PLUS AND AM UNABLE TO LOCATE THE beforeGetValue event AS DESCRIBED BY KATHY
> >
>
> Do you remember the old saying \"When all else fails, read the
> instructions.\"? :-)
>
> dBASE has a very comprehensive help file and if you select the \"Index\"
> tab and enter beforeGetValue you\'ll see a nice explanation.  There is
> also a link to an example.
>
> As Cathy has pointed out there is more than one way to create calculated
> fields.  Her example is more suited for use in a program.  If you are
> using the calculated field in a form a slightly different approach is
> appropriate.
>
> In a form a good place to create a calculated field is in a query\'s
> onOpen event handler.
>
> After you have placed a table on the form use the arrow button of the
> combobox on the Inspector to display the list of objects.  F11 if the
> Inspector is not open.  Select the query object.  dBASE names queries
> with the tablename plus an ordinal number.  tablename1, tablename2, etc
> although you are free to change object names to suit yourself.
>
> Select the \"event\" tab and click on \"onOpen\" to select it.  Click on the
> \"spanner\" button and dBASE will open the sourcecode editor with an empty
> onOpen event handler.  Add the code, close the sourcecode editor and
> save the form.
>
> function WHATEVERTABLE1_onOpen()
>     c = new field()
>     c.fieldName := \"Total\"
>     q.rowset.fields.add(c)
>     c.beforeGetValue := {||this.parent[\"wage rate\"].value +
> this.parent[\"fringes\"].value}
>     return
>
> In your message to Cathy it is not clear on if the field TOTAL is
> actually in the table but not populated.  If this is the case to is
> better to save the correct value when the record is created.
>
>      form.whatevertable1.rowset.beginAppend()
>      form.whatevertable1.rowset.fields[\'wage rate\'].value =
> form.entryfield1.value
>      form.whatevertable1.rowset.fields[\'fringes\'}.value =
> form.entryfield1.value
>      form.whatevertable1.rowset.fields[\'total\'].value =
> from.entryfield1.value + form.entryfield2.value
>      form.whatevertable.rowset.save()
>
> If TOTAL is in the table but not populated then
>
> function WHATEVERTABLE1_onOpen()
>     this.rowset.fields[\"total\"].beforeGetValue := {||this.parent[\"wage
> rate\"].value + this.parent[\"fringes\"].value}
>     return
>
>
> Spaces in field names are acceptable but they require special handling
> so it is better to avoid them altogether.  Instead of WAGE RATE rather
> use WAGE_RATE.  And avoid using SQL reserved words as field names as
> they also require special handling.  You\'ll find a list of them in the
> help file.
>
> If TOTAL isn\'t actually in the table an alternative method of
> calculating it is in the query\'s sql property.
>
> Open the form (or program) in the sourcecode editor and change the
> query\'s sql property from
>
>     sql = \'select * from \"whatevertable.dbf\"\'
>
> to
>
>    sql = \'select w.*,w.\"wage rate\"+fringes as TOTAL from
> \"whatevertable.dbf\" w\'
>
> When using the * wildcard to select all fields it must be qualified by
> using the tablename or a table correlation name (alias) if calculated
> fields are to be added.  In this case I\'ve used w as the alias as I
> usually use the first letter of the tablename but you can use anything
> you like.  Because WAGE RATE has a space in it it must be wrapped in
> quotes.  This in turn means it also needs to be qualified using the
> tablename or a table correlation name.
>
> Using this method makes the rowset read-only so it means a bit of fancy
> footwork if you need to edit the table but it has the advantage that you
> can order the rowset on the calculated value.  A rowset with a
> calculated field created using canGetRow() is editable but you can\'t
> create an index on the calculated value.
>
> Mervyn.
>
>
>
>
>
>
>
>
>
>
>
>
>
>