Board index » delphi » Field Update, Get Fields, Add Fields?

Field Update, Get Fields, Add Fields?

I have discovered that when accessing a tables fields one uses the
FieldByName or Fields[I] method.

I am writing a program that modifies a table (in and Access mdb file).
Modifying by using a SQL string on a temp TQuery object works fine (ALTER
TABLE etc...).
However, when, during execution after the modification, I try to refer to
the new field in the table,
Delphi claims that it can't find it.

Now, if Delphi can only see the fields pulled in, (by right clicking the
table object and selecting fields editor)
this error makes sense and my question would be...
                                     Is there a way to pull into the TTable
the newly created field(s) during execution?

 

Re:Field Update, Get Fields, Add Fields?


Quote
>Now, if Delphi can only see the fields pulled in, (by right clicking the
>table object and selecting fields editor)
>this error makes sense and my question would be...
>                                     Is there a way to pull into the TTable
>the newly created field(s) during execution?

Yes if you create persistent fields that is all Delphi dataset components can
see.  

Code like this will add persistent fields for any fields from the table that are
not persistent fields
var
  f : TField;
  i : integer;
begin
 table1.FieldDefs.Update
  Table1.Close;
  for i := 0 to Table1.FieldDefs.Count - 1 do
   //create persistent field that do not exist
   if table1.FindField(table1.FieldDefs[i].Name) = nil then
      table1.FieldDefs.Items[i].CreateField(Table1);
End;
--
Brian Bushay (TeamB)
Bbus...@NMPLS.com

Re:Field Update, Get Fields, Add Fields?


if you  uses persistent fields (filling the fielddefs at design time) you
cannot alter the underlying fields in the table dinamically!
If you add fields this way, obviously Dataset Component can't access them
until you clear fielddefs and call Fielddefs.update.
Note that this way you loss any customization to the fields made in desig
time!

Regards

Eduardo
JDP <christoph...@ruthschris.com> escribi en el mensaje de noticias
98m8u6$...@bornews.inprise.com...

Quote
> I have discovered that when accessing a tables fields one uses the
> FieldByName or Fields[I] method.

> I am writing a program that modifies a table (in and Access mdb file).
> Modifying by using a SQL string on a temp TQuery object works fine (ALTER
> TABLE etc...).
> However, when, during execution after the modification, I try to refer to
> the new field in the table,
> Delphi claims that it can't find it.

> Now, if Delphi can only see the fields pulled in, (by right clicking the
> table object and selecting fields editor)
> this error makes sense and my question would be...
>                                      Is there a way to pull into the
TTable
> the newly created field(s) during execution?

Re:Field Update, Get Fields, Add Fields?


Thanks for the help, I thought you had to pull the fields in (make them
persistant) to access them.
I found that if I don't pull in the fields, I can still access them with the
usual FindField, FieldByName,Fields[i], etc.
Any additional advice is welcome.
Thanks again!!!!!

Quote
JDP <christoph...@ruthschris.com> wrote in message

news:98m8u6$m04@bornews.inprise.com...
Quote
> I have discovered that when accessing a tables fields one uses the
> FieldByName or Fields[I] method.

> I am writing a program that modifies a table (in and Access mdb file).
> Modifying by using a SQL string on a temp TQuery object works fine (ALTER
> TABLE etc...).
> However, when, during execution after the modification, I try to refer to
> the new field in the table,
> Delphi claims that it can't find it.

> Now, if Delphi can only see the fields pulled in, (by right clicking the
> table object and selecting fields editor)
> this error makes sense and my question would be...
>                                      Is there a way to pull into the
TTable
> the newly created field(s) during execution?

Re:Field Update, Get Fields, Add Fields?


you only instatiate fields in design time if you want to sutomize the
appearance, format, displaylabel, etc. in a fixed way.
Otherwise using fielddefs.update in runtime (to be sure the fields Delphi
see in an instance is updated) and then loop into fields array (fields[0] to
fields[fields.count-1] and do whatever you want with it...

Regards
Eduardo
JDP <christoph...@ruthschris.com> escribi en el mensaje de noticias
98o2kk$r...@bornews.inprise.com...

Quote
> Thanks for the help, I thought you had to pull the fields in (make them
> persistant) to access them.
> I found that if I don't pull in the fields, I can still access them with
the
> usual FindField, FieldByName,Fields[i], etc.
> Any additional advice is welcome.
> Thanks again!!!!!

> JDP <christoph...@ruthschris.com> wrote in message
> news:98m8u6$m04@bornews.inprise.com...
> > I have discovered that when accessing a tables fields one uses the
> > FieldByName or Fields[I] method.

> > I am writing a program that modifies a table (in and Access mdb file).
> > Modifying by using a SQL string on a temp TQuery object works fine
(ALTER
> > TABLE etc...).
> > However, when, during execution after the modification, I try to refer
to
> > the new field in the table,
> > Delphi claims that it can't find it.

> > Now, if Delphi can only see the fields pulled in, (by right clicking the
> > table object and selecting fields editor)
> > this error makes sense and my question would be...
> >                                      Is there a way to pull into the
> TTable
> > the newly created field(s) during execution?

Other Threads