BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Adding calculated fields at run time

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Lutz Kutscher
Guest





PostPosted: Wed Dec 03, 2003 1:38 pm    Post subject: Adding calculated fields at run time Reply with quote



Hi,

I've got a form, that enables the user to select fields for a query:
PartNo, SalesCount, Date ...

If the user selects "PartNo" to be displayed in the result, I want to add a
field "PartName".
The data shown in PartName is loaded from a table that's not part of the
query,
so I wanted to add it as calculated field.
How can I add a calculated field at runtime?


--
_____________________________________

Lutz Kutscher
Pattburg Poetzsch GmbH & Co KG
Tel. +49 461 773 15 60
Fax +49 461 773 15 15
[email]LKutscher (AT) graensen (DOT) dk[/email]




Back to top
Ross B.
Guest





PostPosted: Wed Dec 03, 2003 3:08 pm    Post subject: Re: Adding calculated fields at run time Reply with quote




"Lutz Kutscher" <LKutscher (AT) Graensen (DOT) dk> wrote

Quote:
Hi,

I've got a form, that enables the user to select fields for a query:
PartNo, SalesCount, Date ...

If the user selects "PartNo" to be displayed in the result, I want to add
a
field "PartName".
The data shown in PartName is loaded from a table that's not part of the
query,
so I wanted to add it as calculated field.
How can I add a calculated field at runtime?


--
_____________________________________

Lutz Kutscher
Pattburg Poetzsch GmbH & Co KG
Tel. +49 461 773 15 60
Fax +49 461 773 15 15
[email]LKutscher (AT) graensen (DOT) dk[/email]





You can add calced fields at runtime. By creating and instance of the Field.

procedure AddCalcedField(aDataSet,aFieldName,aDataType);
F:TField;
begin
F:=TField.Create(aDataSet);
// F.FieldKind:=fkInternalCalc; <--- Look at the diff btw fkInternal and
ftCalcl
F.FieldKind:=fkCalc;
F.FieldName:=aFieldName;
case aDataType of
ftInteger:F.DataType:=ftInteger;
...
ftString:F.DataType:=ftString;
end;
.....

Note if you are opening and closing a query and possibly
selecting/deselecting multiple dynamically calced fields then be sure to
remove the fields you may no longer need while adding the ones you do need.

Ross B.





Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Wed Dec 03, 2003 5:44 pm    Post subject: Re: Adding calculated fields at run time Reply with quote



Lutz Kutscher wrote:
Quote:

I've got a form, that enables the user to select fields for a query:
PartNo, SalesCount, Date ...

If the user selects "PartNo" to be displayed in the result, I want to
add a field "PartName".
The data shown in PartName is loaded from a table that's not part of
the query,
so I wanted to add it as calculated field.
How can I add a calculated field at runtime?

If you are generating the query dynamically anyway, why not add it (a join)
to the query itself?

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"It is error alone which needs the support of government. Truth can
stand by itself." - Thomas Jefferson



Back to top
Lutz Kutscher
Guest





PostPosted: Thu Dec 04, 2003 2:08 pm    Post subject: Re: Adding calculated fields at run time Reply with quote

"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> schrieb im Newsbeitrag
news:3fce212e$1 (AT) newsgroups (DOT) borland.com...

Quote:
If you are generating the query dynamically anyway, why not add it (a
join)
to the query itself?
.... because the data in that field originates from a database on a different

server.
Anyway, I found a solution.
I added the following code to the BeforeOpen-Handler of the query:

Query.FieldDefs.Update;
if Query.FieldDefs.IndexOf('PartNo') > -1 then
begin
TDataSetCracker(Query).CreateFields;
with TStringField.Create(Query) do begin
Name := PartNameComponent; //defined as constant
FieldName := PartNameField;//defined as constant
DataSet := Query;
FieldKind := fkCalculated;
end;
end;

In the AfterClose-Handler I added:

Query.Fields.Clear;

.... now it works.

Quote:
Lutz Kutscher wrote:

I've got a form, that enables the user to select fields for a query:
PartNo, SalesCount, Date ...

If the user selects "PartNo" to be displayed in the result, I want to
add a field "PartName".
The data shown in PartName is loaded from a table that's not part of
the query,
so I wanted to add it as calculated field.
How can I add a calculated field at runtime?




Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.