Subject Re: beforeGetValue event
From Mervyn Bick <invalid@invalid.invalid>
Date Thu, 14 Dec 2017 19:51:07 +0200
Newsgroups dbase.getting-started
Attachment(s) whatevertable.wfm

On 2017-12-14 5:46 PM, Al W wrote:
> 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

"Whatever" and "whatevertable" are placeholders as I didn't know what
the actual names were.  The principles apply to any table.

I've created a little table which I've named whatevertable.dbf mainly to
match the name of your form.  This helps me keep example forms and
tables together on my computer.  Having spaces in field names is
allowable but at times they need special handling.  As I needed a table
with which to test your code I created a new table.  In this I've named
the field wage_rate.

By placing the code in the form's header section it will be created in
the same folder as the form.

Although dBASE and Windows will allow it, you shouldn't place code or
tables in C:\Users\MyPC\AppData\Local\dBASE\Plus11\Bin.  Rather create a
new folder for your dBASE work.  Perhaps c:\dBase

While you are still learning how to use dBASE it simplifies things to
have code and data in the same folder.  Once you start with larger
applications where you have many data files it is, however, preferable
to keep code and data in separate folders.

Don't worry if the constructor code for the query in the attached form
looks "different".  This is the format used by older versions of dBASE.
Because the dBASE designer now uses the full path for tables in a query
I have a little program that strips the path out and, at the same time,
changes the code so that the code will also run on older versions of dBASE.

There were a couple of reasons why your form wouldn't run.  You hadn't
created the new field in the query's onOpen event handler.  You also
hadn't assigned the event handler to the event in the query's
constructor code.

A revised version of your form is attached.

Mervyn.












if file('whatevertable.dbf')
   drop table whatevertable
endif

if not file('whatevertable.dbf')
   create table whatevertable  (id autoinc,trade character(15),wage_rate numeric(10,2),;
     fringes numeric(10,2))

   insert into whatevertable  (trade,wage_rate,fringes) values ("Electrician",120.00,12.00)
   insert into whatevertable  (trade,wage_rate,fringes) values ("Plumber",110.00,11.00)
   insert into whatevertable  (trade,wage_rate,fringes) values ("Brick layer",90.00,9.00)
   insert into whatevertable  (trade,wage_rate,fringes) values ("Plasterer",90.00,9.00)
endif

** END HEADER -- do not remove this line
//
// Generated on 14/12/2017
//
parameter bModal
local f
f = new WHATEVERTABLEForm()
if (bModal)
   f.mdi = false // ensure not MDI
   f.readModal()
else
   f.open()
endif

class WHATEVERTABLEForm of FORM
   with (this)
      onOpen = class::FORM_ONOPEN
      colorNormal = "Aliceblue"
      height = 25.4545
      left = 0.0
      top = 0.0
      width = 192.2857
      text = "Whatevertable"
      scrollBar = 2        // Auto
   endwith

   this.WHATEVERTABLE1 = new QUERY()
   this.WHATEVERTABLE1.parent = this
   with (this.WHATEVERTABLE1)
      onOpen = class::WHATEVERTABLE1_onOpen
      left = 34.0
      width = 12.0
      height = 1.0
      sql = 'select * from "whatevertable.dbf"'
      active = true
   endwith

   this.TEXTTRADE1 = new TEXT(this)
   with (this.TEXTTRADE1)
      height = 1.0
      left = 80.0
      top = 8.0
      width = 5.7143
      wrap = false
      colorNormal = "BtnText/Aliceblue"
      alignVertical = 2        // Bottom
      fontName = "Arial Narrow"
      fontSize = 11.0
      fontBold = true
      fontItalic = true
      text = "Trade"
   endwith

   this.ENTRYFIELDTRADE1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELDTRADE1)
      dataLink = form.whatevertable1.rowset.fields["trade"]
      height = 1.0
      left = 80.0
      top = 9.0
      width = 32.0
      fontName = "Arial Narrow"
      fontSize = 12.0
   endwith

   this.ENTRYFIELDWAGE_RATE1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELDWAGE_RATE1)
      dataLink = form.whatevertable1.rowset.fields["wage_rate"]
      height = 1.0
      left = 93.0
      top = 10.5
      width = 8.0
      fontName = "Arial Narrow"
      fontSize = 12.0
   endwith

   this.ENTRYFIELDFRINGES1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELDFRINGES1)
      dataLink = form.whatevertable1.rowset.fields["fringes"]
      height = 1.0
      left = 93.0
      top = 11.5
      width = 8.0
      fontName = "Arial Narrow"
      fontSize = 12.0
   endwith

   this.ENTRYFIELDTOTAL1 = new ENTRYFIELD(this)
   with (this.ENTRYFIELDTOTAL1)
      dataLink = form.whatevertable1.rowset.fields["total"]
      height = 1.0
      left = 93.0
      top = 12.5
      width = 8.0
      fontName = "Arial Narrow"
      fontSize = 12.0
   endwith

   this.TITLE1 = new TEXT(this)
   with (this.TITLE1)
      height = 2.0
      left = 1.0
      top = 0.0
      width = 21.1429
      variableHeight = true
      colorNormal = "Black/Aliceblue"
      fontSize = 14.0
      fontBold = true
      fontItalic = true
      text = "Whatevertable"
   endwith

   this.rowset = this.whatevertable1.rowset

   function form_onOpen()

      return

   function WHATEVERTABLE1_onOpen()
      c = new field()
      c.fieldName := "Total"
      this.rowset.fields.add(c)
      this.rowset.fields["total"].beforeGetValue := {||this.parent["wage_rate"].value + this.parent["fringes"].value}
    return
  
endclass