Subject Re: dataq modules - calculated fields
From Gaetano De Luisi <gaetanodd@hotmail.com>
Date Mon, 06 Jul 2020 05:41:17 -0400
Newsgroups dbase.getting-started

Gaetano De Luisi Wrote:

> Hey guys, it's been years since i wrote any code in dBase and I am getting stuck on some very basic stuff :(
>
> Not sure if it matters, but I use dBase Plus 11.
>
> I have a table that include energy outputs of my solar panels and I want to convert this to power values. essentially, with 5-minute interval energy readings, I just need to multiply the field value by 12 to convert that to a power reading.
>
> So i threw the table in a DMD designer, went to the source editor and added the following code (the part about F= New Field() is what I added manually). this is opened and copied from Notepad to make sure I was looking at the actual file contents:
>
> ** END HEADER -- do not remove this line
> //
> // Generated on 05/07/2020
> //
> class energyDMDataModule of DATAMODULE
>    this.ENERGYDATA1 = new QUERY(this)
>    with (this.ENERGYDATA1)
>       left = 125.0
>       top = 45.0
>       width = 114.0
>       height = 181.0
>       sql = 'select * from "D:\Documents\dbase\SolarOctopus2\energydata.dbf"'
>       active = true
>    endwith
>         
>    this.rowset = this.energydata1.rowset
>
>         f = new Field()
>         f.fieldName := "Power"
>         f.readOnly  := true
>         f.Value ="test"
>         this.rowset.fields.add(f)
>
> endclass
>
>
> When I run the DMD, I do get what I expect, however, when i re-open the DMD source editor, some of the new lines of code I added have disappeared, despite the new field and "test" value still appearing in the browser object when I run the DMD. Here is what the source editor shows in dBase.
>
> ** END HEADER -- do not remove this line
> //
> // Generated on 06/07/2020
> //
> class energyDMDataModule of DATAMODULE
>    this.ENERGYDATA1 = new QUERY(this)
>    with (this.ENERGYDATA1)
>       left = 125.0
>       top = 45.0
>       width = 114.0
>       height = 173.0
>       sql = 'select * from "D:\Documents\dbase\SolarOctopus2\energydata.dbf"'
>       active = true
>    endwith
>
>    this.rowset = this.energydata1.rowset
>         this.rowset.fields.add(f)
>
>
> endclass
>
>
>
> The part that sets the value of the calculated field has disappeared from the source  code editor but is still in the actual file. What am I doing wrong?
>
> Also, if the field I want to multiply by 12 is named "energyProducedWh", how would I reference it to replace the value "test" with [12*energyProducedWh]?
>
> Thaqnks!
> Gaetano.

The following code seems to work. Do I always have to instantiate teh field in the query's onOpen event?

** END HEADER -- do not remove this line
//
// Generated on 06/07/2020
//
class energyDMDataModule of DATAMODULE
   this.ENERGYDATA1 = new QUERY(this)
   with (this.ENERGYDATA1)
      onOpen = class::ENERGYDATA1_ONOPEN
      left = 125.0
      top = 45.0
      width = 114.0
      height = 125.0
      sql = 'select * from "D:\Documents\dbase\SolarOctopus2\energydata.dbf"'
      active = true
   endwith

   with (this.ENERGYDATA1.rowset)
      with (fields["Power"])
         beforeGetValue = {|| this.parent["energyProducedWh"].value*12}
      endwith
   endwith

   this.rowset = this.energydata1.rowset

  

   function ENERGYDATA1_onOpen()
      f=new Field()
                f.fieldName:="Power"
                f.readOnly:=true
                
                this.rowset.fields.add(f)
      return

endclass