Board index » delphi » Saving calculated field value to a database field

Saving calculated field value to a database field

Hi I rescently posted for help on working out a persons age using calculated
fields.
My calculation is finaly working but I noticed it doesnt save the value in
the age field to the field in my access database. Can a calculated field
save data to a database? If it can how do I get around this?

Thanx in advance

Quinton Viljoen

 

Re:Saving calculated field value to a database field


A calculated field is just that... It's calculated, not retrieved from a
database....
If what you want is to store this field.. you have to add the field to the
access table, and treat it like any other fields...
  If what you want is to calculate the guys age after they typed the bith
date you should place an event handler for the onExit event, on the control
that holds the birth date.
  For example if u have a TDBEdit control on your form, place this code on
the OnExit event:

var lDoPost:Boolean;
     Res:Word;
begin
  with myTable do begin

      if FieldByName('BirthDate').IsNull then
        Res:=0
      else
        Res:=CalculateAge(FieldByName('BirthDate').AsDateTime);
{
  CalculateAge is a function you should implement to calculate age from a
birthdate. Now you know how..  :-)

Quote
}

      if FieldByName('Age').AsInteger<>Res then begin

        if (State<>dsEdit)or(State<>dsInsert) then lDoPost:=True;
        if lDoPost then Edit;
        try
           FieldByName('Age').AsInteger:=Res;
        finally
           if lDoPost then Post;
        end;

      end;

   end;
end;

Then on the OnKeyUp event, place this code:

if key=vk_return then begin
   MyTableOnExit(sender);
//MyTableOnExit is the name of the OnExit handler... replace it.
end;

Remember to replace myTable , 'BirthDate' , 'Age' and MyTableOnExit for your
code...

happy coding...

Emiliano Sosa
Software dep.
Sierra S.R.L. www.gosierra.com
Buenos Aires Argentina

Re:Saving calculated field value to a database field


     You should avoid putting anything into the table that
can be calculated. It becomes a maintenance headache.
Age can be calculated either in delphi as a calculated field
or in Access itself as a query.

select year( date() ) - year(BirthDate ) -
       ( iif( (month( BirthDate ) > month(date() )) or
                   (month( BirthDate ) = month( date() ) And
                    day( BirthDate ) > day(date() ))
             , 1, 0)
       ) as Age
from YourTable    

     where Birthdate is the field in the table and date()
function returns the current date.

Sunil

Quote
"Quinton Viljoen" <Quint...@bensure.co.za> wrote:
>Hi I rescently posted for help on working out a persons age using calculated
>fields.
>My calculation is finaly working but I noticed it doesnt save the value in
>the age field to the field in my access database. Can a calculated field
>save data to a database? If it can how do I get around this?

>Thanx in advance

>Quinton Viljoen

Re:Saving calculated field value to a database field


Thanx Allot once again Emiliano, I thaught it would be possible to save to a
field by using a post like post as soon as the data is entered into the
field etc.. but I couldnt get it to work, Ive now got a delphi book that has
much bout the pascal language which I do knowe a little(back from my Turbo
pascal 7 days at school) anyways database programming im not new to I wirte
extensive apps in .asp but found it quite limited so now im trying delphi
eventualy when im done with my application it will be used by about 200
hundred people from around South-Africa all linked to a Mysql database
hosted on a website so ive realy got a hard project lined up, but thats what
makes it fun :-)

Have a nice day mate youve been  a real good help.

Quinton Viljoen

Re:Saving calculated field value to a database field


Thanx for your reply I suppose that is one way of running it but then I
would hav to run that query somehow everytime a record is captured. But I
will try it and consider it as a option

Thanx allot

Re:Saving calculated field value to a database field


it's not working because i made another typo
in this code:

  if (State<>dsEdit)or(State<>dsInsert) then lDoPost:=True;

replace it by:

  if (State<>dsEdit) AND (State<>dsInsert) then lDoPost:=True;

sorry

Why you wanna save the value to the db?   why can't it be just a calculated
field? will it be accessed from another application and that's why it must
be saved? If the data will be accessed only through your delphi app, you
should use a calculated field. It's easier.

Emiliano Sosa
Software dep.
Sierra S.R.L. www.gosierra.com
Buenos Aires Argentina

Re:Saving calculated field value to a database field


Yes I do knowe its easier that way but I hav to keep it in the db to if I
ever need to export the data to a new database for reports or something, was
just told that I have to save it, you knowe how it goes boss says u do , so
I have to

Thanx again

BTW when I type fast I also make lotsa typos hehe

Other Threads