Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create a calculated field at runtime and more

Posted on 2003-01-15
14
Medium Priority
?
1,098 Views
Last Modified: 2010-04-04
I use next code to create my runtime calculated field:

  Path:=ExtractFilePath(Application.ExeName);
  Table1.TableName:=Path+'Database\myOmy.dbf';
  Table1.IndexName:='myOmy';
  MyField := TFloatField.Create(Table1);

  with MyField do
   begin
    FieldName := 'MyField';
    Calculated := True;
    DataSet := Table1;
    Name := Table1.Name + FieldName;
    Table1.FieldDefs.Add(Name, ftString, 250, false);
   end;
   Table1.Open;

  But I need to use the others fields of the table and I don't know how to add these.

   My table have , for example, Field1 and Field2. If I try to use the calculated field with Field2, I get an error that the Field2 don't exist. How to add?

    In same time this calculated field is used inside a DBGrid. How to add a column at runtime for this field?
0
Comment
Question by:ginsonic
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
14 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7738395
try

Path:=ExtractFilePath(Application.ExeName);
 Table1.TableName:=Path+'Database\myOmy.dbf';
 Table1.IndexName:='myOmy';
 
  //
 Table1.FieldDefs.Update; //Retrieve all Fields
  //

 MyField := TFloatField.Create(Table1);
 with MyField do
  begin
   FieldName := 'MyField';
   Calculated := True;
   DataSet := Table1;
   Name := Table1.Name + FieldName;
   Table1.FieldDefs.Add(Name, ftString, 250, false);
  end;
  Table1.Open;

 not tested

meikl ;-)
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7738403
appendix

>If I try to use the calculated field with Field2
use the FieldByName-method

>In same time this calculated field is used inside a DBGrid
don't use predefined columns, the dbgrid does then automatically add your field

meikl ;-)
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7739623
I wish to show just this field inside my DBGrid
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 27

Expert Comment

by:kretzschmar
ID: 7740289
>I wish to show just this field inside my DBGrid
set on all other TFields the visible-property to false

meikl ;-)
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7740510
I think that will be easier to add a column and not to set more than 30 fields visibility to false :)

Still don't know how to create this at run-time :(
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7740561
>I think that will be easier to add a column and not to
>set more than 30 fields visibility to false :)

?really

for i := 0 to table1.fieldcount-2 do
  table1.fields[i].visible := false;

thats all

meikl ;-)
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7740933
Don't work with

Table1.FieldDefs.Update; //Retrieve all Fields

And still wish to KNOW how can add a column at runtime.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7740969
no delphi on hand yet,
will try this evening myself
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7742092
I use now:

  Path:=ExtractFilePath(Application.ExeName);
  Table1.TableName:=Path+'Database\myOmy.dbf';
  Table1.IndexName:='myOmy';
  Table1.Open;
  table1.FieldDefs.Update;
  Table1.Close;

  for i := 0 to Table1.FieldDefs.Count - 1 do
   {create persistent field that does not exist}
    if table1.FindField(table1.FieldDefs[i].Name) = nil then
      table1.FieldDefs.Items[i].CreateField(Table1);
  {create a calculated field}
  f := TStringField.Create(Table1);
  f.Name := 'Table1CalcField';
  f.FieldName := 'CalcField';
  f.Size:=250;
  f.Calculated := True;
  f.DataSet := Table1;
  Table1.Open;
  Table1.OnCalcFields:=CalcME;

  Grid1.Columns.Add.FieldName:='CalcField';
  Grid1.Columns.Add.Width:=250;
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7742652
well, tried myself a bit->not so easy

found following article

http://bdn.borland.com/article/0,1410,16467,00.html

meikl ;-)
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7746161
If you read my initial code is the same :)

The last code that I put work,but don't know if the DBGrid add column is very correct :)
0
 
LVL 27

Accepted Solution

by:
kretzschmar earned 1200 total points
ID: 7747447
looks like ok,
will validate
0
 
LVL 9

Author Comment

by:ginsonic
ID: 7882686
Thamks for asistance.
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7882760
ooops,
just forgotton this q,
sorry about this

thanks for the points :-))

hope it works now like a charme

meikl ;-)
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question