Discussion:
Add field to ADO Table
(too old to reply)
Fred S. Lindow
2006-01-19 04:13:19 UTC
Permalink
I need to add a field to an existing ADOTable during run-time.

Pulled out every Delphi book I got in the office and no luck. Could not
find anything in the newsgroups either.

Best Regards,

Fred
Guillem
2006-01-19 13:29:40 UTC
Permalink
Post by Fred S. Lindow
I need to add a field to an existing ADOTable during run-time.
Pulled out every Delphi book I got in the office and no luck. Could
not find anything in the newsgroups either.
Best Regards,
Fred
without knowing which kind of field you want here you have an example
for a look-up one.

with ADOContacto do
begin
Close;
CommandText := 'whatever';
CommandType := cmdText;

FieldDefs.Update;
for i:= 0 to FieldDefs.Count - 1 do
if FindField(FieldDefs[i].Name) = nil then
FieldDefs.Items[i].CreateField(ADOMyTable);
F := TStringField.Create(ADOMyTable);
F.FieldName := 'myField';
F.DisplayLabel := 'myFieldLabel';
F.name := 'ADOmyNewField';
F.FieldKind := fkLookup;
F.DataSet := ADOMyTable;
F.KeyFields := 'myKeyField';
F.Size := 30;
F.LookupDataSet := ADOLookUpTable;
F.LookupKeyFields := 'myLookUpFields';
F.LookupResultField := 'myResultField';
Fields.Add(F);
open;
end;


plus i would suggest to avoid ADOTable and use only ADOQuery or
ADODataset for operations which produce result sets.

Good luck
--
Best regards :)

Guillem Vicens Meier
Dep. Informatica Green Service S.A.
www.clubgreenoasis.com
--
Contribute to the Indy Docs project: http://docs.indyproject.org
--
In order to contact me remove the -nospam
Bill Todd
2006-01-19 13:18:31 UTC
Permalink
Post by Fred S. Lindow
I need to add a field to an existing ADOTable during run-time.
Do you mean add a field to the underlying database table or do you mean
create a field object at runtime?
--
Bill Todd (TeamB)
Brian Bushay TeamB
2006-01-20 02:18:53 UTC
Permalink
Post by Fred S. Lindow
I need to add a field to an existing ADOTable during run-time.
Pulled out every Delphi book I got in the office and no luck. Could not
find anything in the newsgroups either.
To add fields to the underlying tables use SQL Alter table statement

To add persistent Tfields to the dataset use code like this

var
f : TField;
i : integer;
begin
Query1.FieldDefs.Update
Query1.Close;
for i := 0 to Query1.FieldDefs.Count - 1 do
//create persistent field that do not exist
if Query1.FindField(Query1.FieldDefs[i].Name) = nil then
Query1.FieldDefs.Items[i].CreateField(Query1);
//create a calculated field
f := TStringField.Create(Query1);
f.Name := 'Query1CalcField';
f.FieldName := 'CalcField';
f.DisplayLabel := 'CalcField';
f.Calculated := True;
f.DataSet := Query1;
Query1.Open;
end;
--
Brian Bushay (TeamB)
***@NMPLS.com

Continue reading on narkive:
Loading...