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 

How can I declare a TField at runtime?

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop)
View previous topic :: View next topic  
Author Message
Marcio Ehrlich
Guest





PostPosted: Sun May 02, 2004 3:08 am    Post subject: How can I declare a TField at runtime? Reply with quote



I am facing a problem that suggests I would need to include some TField as a
property a form, depending on the SQL of a TQuery (it may or not return that
field).
For example, with this form which type is declared as:
TFormQRSelecao = class(TQuickRep)
if according to some condition my TQueryCasting returns a currency field
named "Extras", how can I include this declaration:
QueryCastingExtras: TCurrencyField;
so I will be able to later retrieve QueryCasting['Extras'] value?
Is this the question or I am making a big mess?
Thanks,
Marcio


Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sun May 02, 2004 3:46 am    Post subject: Re: How can I declare a TField at runtime? Reply with quote



Marcio Ehrlich wrote:
Quote:
For example, with this form which type is declared as:
TFormQRSelecao = class(TQuickRep)
if according to some condition my TQueryCasting returns a currency
field named "Extras", how can I include this declaration:
QueryCastingExtras: TCurrencyField;
so I will be able to later retrieve QueryCasting['Extras'] value?

If you have a query component where the Select statement may change with
regard to selected fields, then you do not want or need persistent field
components. By not having persistent fields, when you open TQueryCasting, it
will create TField objects for you for all the fields you have selected.
There are at least a couple of ways to determine if a field was selected or
not, one way is:

if Assigned(TQueryCasting .FieldByName('Extras')) then
ShowMessage(TQueryCasting .FieldByName('Extras').AsString);

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"True peace is not the absence of tension, but the presence of
justice." - Martin Luther King, Jr.



Back to top
Marcio Ehrlich
Guest





PostPosted: Sun May 02, 2004 4:44 pm    Post subject: Re: How can I declare a TField at runtime? Reply with quote



"Wayne Niddery [TeamB]" <wniddery (AT) chaffaci (DOT) on.ca> wrote:
Quote:
(...) when you open TQueryCasting, it
will create TField objects for you

Wayne,
That's what I expected to happen. Nevertheless, when a Band.BeforePrint was
called with something like:
QRLabelExtras.Caption := QueryCasting.FieldByName('Extras').AsString;
I got the "field not found" error.
Finally I came to one conclusion: if you can't include ALL fields at design
time, it is better to declare none.
Once you have declared even one field, when you open your query no other
TField object will be created and you will get that "field not found" error.
So, I erased all TFields objects from the Interface section and now the
application runs ok independently of the queries' fields.
Is that what was supposed to be?
Marcio





Back to top
Wayne Niddery [TeamB]
Guest





PostPosted: Sun May 02, 2004 5:34 pm    Post subject: Re: How can I declare a TField at runtime? Reply with quote

Marcio Ehrlich wrote:
Quote:

Finally I came to one conclusion: if you can't include ALL fields at
design time, it is better to declare none.
Once you have declared even one field, when you open your query no
other TField object will be created and you will get that "field not
found" error.

Correct. This is exactly the trade off of using persistent fields - those
are all that will exist. If the fields in your query are unchanging, then
persistent fields are fine (but still unecessary).

--
Wayne Niddery - Logic Fundamentals, Inc. (www.logicfundamentals.com)
RADBooks: http://www.logicfundamentals.com/RADBooks.html
"True peace is not the absence of tension, but the presence of
justice." - Martin Luther King, Jr.



Back to top
Marcio Ehrlich
Guest





PostPosted: Tue May 04, 2004 12:43 pm    Post subject: Re: How can I declare a TField at runtime? Reply with quote

"Brian Bushay TeamB" <BBushay (AT) Nmpls (DOT) com> wrote:
Quote:
//create persistent field that do not exist
if Query1.FindField(Query1.FieldDefs[i].Name) = nil then
Query1.FieldDefs.Items[i].CreateField(Query1);

Very interesting, Brian, thank you.
Marcio



Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (Desktop) 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.