|
Home > Archive > Programming with dBASE > December 2005 > calculated field type
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
calculated field type
|
|
| Moses Hanna 2005-12-28, 8:23 pm |
| Hi
I want to add a calculated field.
The new field is the total of 4 numeric fields in the same table
I coded the following in the query's on open event
the query name is jdetails1
function jdetails1_onOpen
f = new Field()
f.fieldName := "Totals"
f.beforeGetValue := {|| this.parent["val1"].value
+this.parent["val2"].value + this.parent["val3"].value +
this.parent["val4"].value }
this.rowset.fields.add( f )
return
The Field type is coming character and the result is not numeric
any help to get the calc field value as a numeric total of 4 fields?
Thanks
Moses
| |
| *Lysander* 2005-12-29, 7:23 am |
| In article <otHN94BDGHA.1800@news-server>, mosesjhanna@hotmail.com=20
says...
> The Field type is coming character and the result is not numeric
> any help to get the calc field value as a numeric total of 4 fields?
There's a more easy way, though maybe Q&D, but works perfectly for me in=20
all scenarios:
Put the calculated field already into your sql-statement!
jdetails1.sql =3D ;
"SELECT "+;
" val1, "+;
" val2, "+;
" val3, "+;
" val4, "+;
" val1 + val2 + val3 + val4 AS f_totals "+;
"FROM "+;
" The_Table "
This will create a new calculated field named "f_totals" which is read-
only and "virtual", which means that it is only generated at runtime and=20
will not really be saved into the table.
--=20
ciao,
Andr=E9
| |
| Michael Nuwer [dBVIPS] 2005-12-29, 9:23 am |
|
I have seen this problem before, but I can not reproduce it at this
time. So I'm working from memory. Try adding a length property to the code:
f = new Field()
f.fieldName := "Totals"
f.length := 8 // <<<<<<
f.beforeGetValue := ...
Moses Hanna wrote:
> Hi
> I want to add a calculated field.
> The new field is the total of 4 numeric fields in the same table
> I coded the following in the query's on open event
> the query name is jdetails1
>
> function jdetails1_onOpen
> f = new Field()
> f.fieldName := "Totals"
> f.beforeGetValue := {|| this.parent["val1"].value
> +this.parent["val2"].value + this.parent["val3"].value +
> this.parent["val4"].value }
> this.rowset.fields.add( f )
> return
>
> The Field type is coming character and the result is not numeric
> any help to get the calc field value as a numeric total of 4 fields?
> Thanks
> Moses
>
>
| |
| Ronnie MacGregor 2005-12-30, 11:23 am |
| In article <otHN94BDGHA.1800@news-server>, mosesjhanna@hotmail.com
says...
> I want to add a calculated field.
> The new field is the total of 4 numeric fields in the same table
> I coded the following in the query's on open event
> the query name is jdetails1
>
> function jdetails1_onOpen
> f = new Field()
> f.fieldName := "Totals"
> f.beforeGetValue := {|| this.parent["val1"].value
> +this.parent["val2"].value + this.parent["val3"].value +
> this.parent["val4"].value }
> this.rowset.fields.add( f )
> return
>
> The Field type is coming character and the result is not numeric
> any help to get the calc field value as a numeric total of 4 fields?
You could be falling foul of dBASE automatic type conversion here.
Untested, but my understanding is that when you create a calculated
field, dBASE has no way of knowing what data type is required, defaults
to char - "empty string", and the beforeGetValue will then convert a
numeric value to the already defined string or char data type.
Try :
f.value = 0 // numeric zero
or
f.value = null
in your definition code to force, or allow, the correct data type, and
see if this solves your problem.
--
Ronnie MacGregor
Scotland
www.dBASEdeveloper.co.uk
|
|
|
|
|