Subject Re: How to calulate field value in a Grid?
From Mervyn Bick <invalid@invalid.invalid>
Date Fri, 30 Jul 2021 17:32:25 +0200
Newsgroups dbase.getting-started

On 2021/07/30 16:42, Milind Nighojkar wrote:
> Thanks for the reply Akshat,
>
> Aging and Estimated Field both are table fields...
>
> I am not aware how to add calulated fields in grid .....
>
> Any pointer what could be the code on Lost focus ?
>
> Regards
> Milind Nighojkar

If you include Aging as a field in the table you will need to update the
values every time you run the program.

You can place this code in the header section of your form i.e above the
line to  update the Aging field eachtime the form is run.

** END HEADER -- do not remove this line

cDate = set('date')
set date to american
cmd = "update your_table_name  set Aging = '"+dtoc(date())+"' -
Estimated_Date"
&cmd
set date to &cDate

If you edit the Estimated_Date you will need to update the Aging field
manually.

The alternative is not to include the Aging field in the table but to
create it as a calculated field.  Use the query's onOpen event handler
to do this.


    function queryname_onOpen()
      f = new Field()
      f.fieldName := 'Aging'
      f.beforeGetValue := {||date()-this.parent['Estimated_Date'].value }
      this.rowset.fields.add( f )
      return

Calculated fields are read-only so if you edit the Estimated_date field
you will need to requery() the query so that dBASE can recalculate the
Aging field.

I prefer the calculated field but the choice is yours.

Mervyn.