Board index » delphi » How to edit calculated field (urgent , please help)

How to edit calculated field (urgent , please help)

I would like to be able to edit the calculated field when new record is
inserting
(to translate this field into the database table field in 'BeforePost'
event...)
I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
cannot enter any data into the  calculated fields (ReadOnly field property
is FALSE !) - but I really need to do it.

Could anybody give me a solution ?
I will greatly appreciate any help.
Marek.

 

Re:How to edit calculated field (urgent , please help)


I dont think you can manually edit a calkulated field.
Have you tried to insert the value using OnCalcFields?

Regards
Roald
Norway

Marek <ver...@idirect.ca> wrote in article <36d39dd...@nemo.idirect.com>...

Quote
> I would like to be able to edit the calculated field when new record is
> inserting
> (to translate this field into the database table field in 'BeforePost'
> event...)
> I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
> cannot enter any data into the  calculated fields (ReadOnly field
property
> is FALSE !) - but I really need to do it.

> Could anybody give me a solution ?
> I will greatly appreciate any help.
> Marek.

Re:How to edit calculated field (urgent , please help)


Quote
On Wed, 24 Feb 1999 01:51:39 -0600, "Marek" <ver...@idirect.ca> wrote:
>I would like to be able to edit the calculated field when new record is
>inserting
>(to translate this field into the database table field in 'BeforePost'
>event...)
>I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
>cannot enter any data into the  calculated fields (ReadOnly field property
>is FALSE !) - but I really need to do it.

>Could anybody give me a solution ?
>I will greatly appreciate any help.
>Marek.

Calculated fields are just that: calculated.  You cannot write in them
a value other than the one you calculate in the OnCalcFields handler.

Calculated fields will not be written to the db as well, because they
are supposed to be calculated (redundant) when needed.

If "you really need to do it" then maybe you really don't want a
calculated field.  Explain to us what you are trying to do and we
might be able to put you on the right track.

Regards,

--
Marco Rocci
MicroEra srl
Turin, Italy

Re:How to edit calculated field (urgent , please help)


Hi, Marco
I appreciate your comments. Probably I was 'to short' in my previous posting
if you can spend few minutes looking at my problem I would be really
greatful...

 I exactly know what the calculated fields are and I know the main
idea (OnCalcFields event and so on...).
I just try to use them a little bit more wider...

The problem is: I have to handle old-big database (for many reasons cannot
change structure). I've  got field 'X' that I have to break down
into the two parts for screen presentation . I defined two calculated
fields: C1 and C2.  Inside OnCalcField I assign part of X to C1 and part of
X to C2 -
everything works OK.

Now my users need to enter some data. They want to be able to enter data
according to format C1 and C2. What I have to do: I have to allow them
typing inside C1 and C2 and just before 'POST' I have to connect (in special
way) values C1 and C2 and assign it to the real data base field: 'X'.

When my user select 'Insert' button from DBNavigator, they can see whole
empty record but cannot edit C1 and C2. I think there should be a way to
enter data into the calculated field when datasource is in dsEdit or
dsInsert state.

Warm thanks for any additional hints.
Sincerely, Marek.

Quote
Marco Rocci wrote in message <36da06af.12651...@news.tin.it>...
>On Wed, 24 Feb 1999 01:51:39 -0600, "Marek" <ver...@idirect.ca> wrote:

>>I would like to be able to edit the calculated field when new record is
>>inserting
>>(to translate this field into the database table field in 'BeforePost'
>>event...)
>>I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
>>cannot enter any data into the  calculated fields (ReadOnly field property
>>is FALSE !) - but I really need to do it.

>>Could anybody give me a solution ?
>>I will greatly appreciate any help.
>>Marek.

>Calculated fields are just that: calculated.  You cannot write in them
>a value other than the one you calculate in the OnCalcFields handler.

>Calculated fields will not be written to the db as well, because they
>are supposed to be calculated (redundant) when needed.

>If "you really need to do it" then maybe you really don't want a
>calculated field.  Explain to us what you are trying to do and we
>might be able to put you on the right track.

>Regards,

>--
>Marco Rocci
>MicroEra srl
>Turin, Italy

Re:How to edit calculated field (urgent , please help)


You are referring to something that I wished many times I had, a "temporary"
field. My way to fake it is as follows:

1. Create the fields in the SELECT statment by casting spaces or zeros
(depending on field type) as TEMPFIELD. Some servers have other mechanisms
that you can use to accomplish the same thing, this is Interbase:

SELECT REALFIELD1, REALFIELD2, CAST (" " AS CHAR(20)) AS TEMP1,
 CAST(0 AS INTEGER) AS TEMP2 FROM TABLE WHERE ...

The result set will contain all the fields you have selected, INCLUDING the
new temporary field(s). As far as any data-aware control is concerned, the
temp fields are part of the data retrieved from the table.

2. Before opening the query, disconnect controls, open the query, loop thru
the result set updating your new field(s), then reconnect controls. Of
course, if you can derive the temporary values as part of the query, via
some SQL wizardry, this step is unnecessary, but I suspect you need
something stronger than SQL to transform your field X into C1 and C2.

3. Use a TUpdateSQL (is that the name? the thingie you hook up to the query
that allows you to handcode the SQL) to do your updates, getting rid of the
temporary fields. I'm not sure about this one, just that I never used this
technique without it. Don't know what would happen if you try to send
updates back to the temporary fields.

Hope this helps.

Robert

Quote
Marek wrote in message <36d4733...@nemo.idirect.com>...
>Hi, Marco
>I appreciate your comments. Probably I was 'to short' in my previous
posting
>if you can spend few minutes looking at my problem I would be really
>greatful...

> I exactly know what the calculated fields are and I know the main
>idea (OnCalcFields event and so on...).
>I just try to use them a little bit more wider...

>The problem is: I have to handle old-big database (for many reasons cannot
>change structure). I've  got field 'X' that I have to break down
>into the two parts for screen presentation . I defined two calculated
>fields: C1 and C2.  Inside OnCalcField I assign part of X to C1 and part of
>X to C2 -
>everything works OK.

>Now my users need to enter some data. They want to be able to enter data
>according to format C1 and C2. What I have to do: I have to allow them
>typing inside C1 and C2 and just before 'POST' I have to connect (in
special
>way) values C1 and C2 and assign it to the real data base field: 'X'.

>When my user select 'Insert' button from DBNavigator, they can see whole
>empty record but cannot edit C1 and C2. I think there should be a way to
>enter data into the calculated field when datasource is in dsEdit or
>dsInsert state.

>Warm thanks for any additional hints.
>Sincerely, Marek.

>Marco Rocci wrote in message <36da06af.12651...@news.tin.it>...
>>On Wed, 24 Feb 1999 01:51:39 -0600, "Marek" <ver...@idirect.ca> wrote:

>>>I would like to be able to edit the calculated field when new record is
>>>inserting
>>>(to translate this field into the database table field in 'BeforePost'
>>>event...)
>>>I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
>>>cannot enter any data into the  calculated fields (ReadOnly field
property
>>>is FALSE !) - but I really need to do it.

>>>Could anybody give me a solution ?
>>>I will greatly appreciate any help.
>>>Marek.

>>Calculated fields are just that: calculated.  You cannot write in them
>>a value other than the one you calculate in the OnCalcFields handler.

>>Calculated fields will not be written to the db as well, because they
>>are supposed to be calculated (redundant) when needed.

>>If "you really need to do it" then maybe you really don't want a
>>calculated field.  Explain to us what you are trying to do and we
>>might be able to put you on the right track.

>>Regards,

>>--
>>Marco Rocci
>>MicroEra srl
>>Turin, Italy

Re:How to edit calculated field (urgent , please help)


Hi Marek,

I know exaclty what you mean and have had the same sort of problems. What I
usually do is this:
When the user presses a key while on the calculated field I move an extra TEdit
on the
exact location of the Grid cell and display the value( C1 or C2, extracted from
the original
table) in this field.
When the user is done editing this field I adjust the original field in the
table.
Call Refresh afterwards to update the calculated fields.

Cheers,
Ruben

Quote
Marek wrote:
> Hi, Marco
> I appreciate your comments. Probably I was 'to short' in my previous posting
> if you can spend few minutes looking at my problem I would be really
> greatful...

>  I exactly know what the calculated fields are and I know the main
> idea (OnCalcFields event and so on...).
> I just try to use them a little bit more wider...

> The problem is: I have to handle old-big database (for many reasons cannot
> change structure). I've  got field 'X' that I have to break down
> into the two parts for screen presentation . I defined two calculated
> fields: C1 and C2.  Inside OnCalcField I assign part of X to C1 and part of
> X to C2 -
> everything works OK.

> Now my users need to enter some data. They want to be able to enter data
> according to format C1 and C2. What I have to do: I have to allow them
> typing inside C1 and C2 and just before 'POST' I have to connect (in special
> way) values C1 and C2 and assign it to the real data base field: 'X'.

> When my user select 'Insert' button from DBNavigator, they can see whole
> empty record but cannot edit C1 and C2. I think there should be a way to
> enter data into the calculated field when datasource is in dsEdit or
> dsInsert state.

> Warm thanks for any additional hints.
> Sincerely, Marek.

> Marco Rocci wrote in message <36da06af.12651...@news.tin.it>...
> >On Wed, 24 Feb 1999 01:51:39 -0600, "Marek" <ver...@idirect.ca> wrote:

> >>I would like to be able to edit the calculated field when new record is
> >>inserting
> >>(to translate this field into the database table field in 'BeforePost'
> >>event...)
> >>I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
> >>cannot enter any data into the  calculated fields (ReadOnly field property
> >>is FALSE !) - but I really need to do it.

> >>Could anybody give me a solution ?
> >>I will greatly appreciate any help.
> >>Marek.

> >Calculated fields are just that: calculated.  You cannot write in them
> >a value other than the one you calculate in the OnCalcFields handler.

> >Calculated fields will not be written to the db as well, because they
> >are supposed to be calculated (redundant) when needed.

> >If "you really need to do it" then maybe you really don't want a
> >calculated field.  Explain to us what you are trying to do and we
> >might be able to put you on the right track.

> >Regards,

> >--
> >Marco Rocci
> >MicroEra srl
> >Turin, Italy

Re:How to edit calculated field (urgent , please help)


Quote
On Wed, 24 Feb 1999 16:54:16 -0800, "Marek" <m...@tradition.ca> wrote:
>Hi, Marco
>I appreciate your comments. Probably I was 'to short' in my previous posting
>if you can spend few minutes looking at my problem I would be really
>greatful...

> I exactly know what the calculated fields are and I know the main
>idea (OnCalcFields event and so on...).
>I just try to use them a little bit more wider...

>The problem is: I have to handle old-big database (for many reasons cannot
>change structure). I've  got field 'X' that I have to break down
>into the two parts for screen presentation . I defined two calculated
>fields: C1 and C2.  Inside OnCalcField I assign part of X to C1 and part of
>X to C2 -
>everything works OK.

>Now my users need to enter some data. They want to be able to enter data
>according to format C1 and C2. What I have to do: I have to allow them
>typing inside C1 and C2 and just before 'POST' I have to connect (in special
>way) values C1 and C2 and assign it to the real data base field: 'X'.

>When my user select 'Insert' button from DBNavigator, they can see whole
>empty record but cannot edit C1 and C2. I think there should be a way to
>enter data into the calculated field when datasource is in dsEdit or
>dsInsert state.

I see you have already received two different, but both good ways of
solving your problem.  I always use CachedUpdates, so I prefer
Robert's solution, because I just eliminate the two "temporary" fields
from the TUpdateObject I already have.

Ruben's solution might be simpler if you don't usually use
CachedUpdates.  Two real calculated fields to show in the grid.  Two
TEdits (not data-aware) to edit the values.  Some code to handle the
mess.

I suggest you use TDataSet.OnAfterScroll and TDataSet.OnNewRecord to
set the TEdit.Text properties, and the TEdit.OnChange or OnExit to
recalculate the new X field.

Regards,

--
Marco Rocci
MicroEra srl
Turin, Italy

Re:How to edit calculated field (urgent , please help)


I'm not sure whether this helps but let me explain how I coped with a
similar problem with displaying a weight. I wanted to display a weight as,
for example, 9-7 for 9 stone 7 pounds, but I wanted to store the value as
133 (number of pounds) in my database. The initial way was to generate the
stones and pounds as calculated fields, but as you have pointed out this
does not allow you to update the value.

So I used the Onsettext and ongettext methods of the field to ensure that
the field was always displayed as I required. These are documented in the
help files. The ongettext method allows you to display anything you want,
derived from the field. You may be able to display you field as, for
example, "C1 / C2" and get users to type in the full field. You will need to
validate their input though. This doesn't allow the field to be split into
two though, so it may not be applicable.

Steve

Quote
Marco Rocci wrote in message <36d512d9.2574...@tor-nn1-ca.netcom.ca>...
>On Wed, 24 Feb 1999 16:54:16 -0800, "Marek" <m...@tradition.ca> wrote:

>>Hi, Marco
>>I appreciate your comments. Probably I was 'to short' in my previous
posting
>>if you can spend few minutes looking at my problem I would be really
>>greatful...

>> I exactly know what the calculated fields are and I know the main
>>idea (OnCalcFields event and so on...).
>>I just try to use them a little bit more wider...

>>The problem is: I have to handle old-big database (for many reasons cannot
>>change structure). I've  got field 'X' that I have to break down
>>into the two parts for screen presentation . I defined two calculated
>>fields: C1 and C2.  Inside OnCalcField I assign part of X to C1 and part
of
>>X to C2 -
>>everything works OK.

>>Now my users need to enter some data. They want to be able to enter data
>>according to format C1 and C2. What I have to do: I have to allow them
>>typing inside C1 and C2 and just before 'POST' I have to connect (in
special
>>way) values C1 and C2 and assign it to the real data base field: 'X'.

>>When my user select 'Insert' button from DBNavigator, they can see whole
>>empty record but cannot edit C1 and C2. I think there should be a way to
>>enter data into the calculated field when datasource is in dsEdit or
>>dsInsert state.

>I see you have already received two different, but both good ways of
>solving your problem.  I always use CachedUpdates, so I prefer
>Robert's solution, because I just eliminate the two "temporary" fields
>from the TUpdateObject I already have.

>Ruben's solution might be simpler if you don't usually use
>CachedUpdates.  Two real calculated fields to show in the grid.  Two
>TEdits (not data-aware) to edit the values.  Some code to handle the
>mess.

>I suggest you use TDataSet.OnAfterScroll and TDataSet.OnNewRecord to
>set the TEdit.Text properties, and the TEdit.OnChange or OnExit to
>recalculate the new X field.

>Regards,

>--
>Marco Rocci
>MicroEra srl
>Turin, Italy

Re:How to edit calculated field (urgent , please help)


Thank you very much everybody !
I see my topic was a spark for the 'brain's storm'...

You are so friendly, (and so good in Delphi )  folks.
Now I have diferent problem: to many solutions :):):)

Good luck.
Marek.

Quote
Marek wrote in message <36d39dd...@nemo.idirect.com>...
>I would like to be able to edit the calculated field when new record is
>inserting
>(to translate this field into the database table field in 'BeforePost'
>event...)
>I use standard DBGrid, and after 'INSERT' (pressed TDBNavigator button) I
>cannot enter any data into the  calculated fields (ReadOnly field property
>is FALSE !) - but I really need to do it.

>Could anybody give me a solution ?
>I will greatly appreciate any help.
>Marek.

Re:How to edit calculated field (urgent , please help)


Hi Marco,

Personally I prefer the sql way, but in our company we use an Apollo database,
which
doesn't support any sql.(..if it was up to me...<sigh>). Also, the applications
we make
are completely "multimedia"-like, so no grey panels, buttons, etc. Just
everything is painted
with images and or (semi) transparent. This requires a lot of custom component
creation.
If you saw one of our apps, you would think they were DirectX, but they're not.
The solution
I suggested is a regular coding exercise for me.

Cheers from Holland,
Ruben

Quote
Marco Rocci wrote:
> On Wed, 24 Feb 1999 16:54:16 -0800, "Marek" <m...@tradition.ca> wrote:

> >Hi, Marco
> >I appreciate your comments. Probably I was 'to short' in my previous posting
> >if you can spend few minutes looking at my problem I would be really
> >greatful...

> > I exactly know what the calculated fields are and I know the main
> >idea (OnCalcFields event and so on...).
> >I just try to use them a little bit more wider...

> >The problem is: I have to handle old-big database (for many reasons cannot
> >change structure). I've  got field 'X' that I have to break down
> >into the two parts for screen presentation . I defined two calculated
> >fields: C1 and C2.  Inside OnCalcField I assign part of X to C1 and part of
> >X to C2 -
> >everything works OK.

> >Now my users need to enter some data. They want to be able to enter data
> >according to format C1 and C2. What I have to do: I have to allow them
> >typing inside C1 and C2 and just before 'POST' I have to connect (in special
> >way) values C1 and C2 and assign it to the real data base field: 'X'.

> >When my user select 'Insert' button from DBNavigator, they can see whole
> >empty record but cannot edit C1 and C2. I think there should be a way to
> >enter data into the calculated field when datasource is in dsEdit or
> >dsInsert state.

> I see you have already received two different, but both good ways of
> solving your problem.  I always use CachedUpdates, so I prefer
> Robert's solution, because I just eliminate the two "temporary" fields
> from the TUpdateObject I already have.

> Ruben's solution might be simpler if you don't usually use
> CachedUpdates.  Two real calculated fields to show in the grid.  Two
> TEdits (not data-aware) to edit the values.  Some code to handle the
> mess.

> I suggest you use TDataSet.OnAfterScroll and TDataSet.OnNewRecord to
> set the TEdit.Text properties, and the TEdit.OnChange or OnExit to
> recalculate the new X field.

> Regards,

> --
> Marco Rocci
> MicroEra srl
> Turin, Italy

Other Threads