Board index » delphi » Cant loop through fields

Cant loop through fields

I am having trouble with the syntax required to loop through multiple
database fields using a simple loop.
If one has 10 fields named 'Field1' to 'Field10', how can I append the
word 'Field' to an integer value being incremented by a simple loop to
address all 10 fields.
eg.
var
    I:Integer;
For I:=1 to 10 do
    'Field' + I := value;    <<<=== This the line that I cannot get to
work.

This must be a really simple problem, but I am having alot of trouble
with it.
If anyone can help, I will be very greatful.
Thanks.
Dean.

 

Re:Cant loop through fields


D...

Quote
> I am having trouble with the syntax required to loop through multiple
> database fields using a simple loop.
> If one has 10 fields named 'Field1' to 'Field10', how can I append the
> word 'Field' to an integer value being incremented by a simple loop to
> address all 10 fields.
> eg.
> var
>     I:Integer;
> For I:=1 to 10 do
>     'Field' + I := value;    <<<=== This the line that I cannot get to
> work.

I see two possible solutions

1)
for i := 1 to 10 do
        Value := TTable.FieldByName('Field'+IntToStr(i)).AsString; {or .AsBoolean,
etc..}

2)
for i := 1 to 10 do
        Vaule := TTable.Fields[i].AsString; {or .AsInteger, so on}

The first method is more widely used as you can change the order of your
table and still access the correct information, so long as you didn't
change the field names.

Craig

Re:Cant loop through fields


Table1.FieldByName('Field' + IntToStr(I)).AsString := 'X';

Bill

(Sorry but TeamB cannot answer support questions received via email.)
(To send me email for any other reason remove .nospam from my address.)

Other Threads