Board index » delphi » creating TadoDatsets variables dynamically

creating TadoDatsets variables dynamically


2005-06-09 01:18:56 PM
delphi72
I need to create one or more data sets dynamically in code. The number
created is dependent upon some parameters in a table. I need to name them
like "dstRpt1", "dstRpt2", etc. and be able to refer to them when setting up
parameters and such. Something like this:
for x := 1 to 3 do
begin
'dstRpt' + Trim(IntToStr(x)) := TAdoDataset.Create(nil);
TAdoDataSet('dstRpt' + Trim(IntToStr(x))).CommandType := cmdStoredProc;
TAdoDataSet('dstRpt' + Trim(IntToStr(x))).Connection := MyConn;
TAdoDataSet('dstRpt' + Trim(IntToStr(x))).CommandText := 'sp1;1';
TAdoDataSet("dstRpt" + Trim(IntToStr(x))).Parameters.Refresh;
... // more parameter settings, then prepared and active, etc.
... // add the dataset to the report (FR3)
end;
... // run the report
... // free the datasets
Obviously the above will not work. I don't know how to create it without
knowing the name during design time and dont' know how to refer to the
dataset once it is been created. I presume from my reading that I need to
use NEW and pointers but the example in the help file is limited and I don't
understand how to go about this.
Thanks,
Keith
 
 

Re:creating TadoDatsets variables dynamically

"Keith G Hicks" <XXXX@XXXXX.COM>writes
Quote
I need to create one or more data sets dynamically in code. The number
created is dependent upon some parameters in a table. I need to name them
like "dstRpt1", "dstRpt2", etc. and be able to refer to them when setting
up
parameters and such. Something like this:

for x := 1 to 3 do
begin
'dstRpt' + Trim(IntToStr(x)) := TAdoDataset.Create(nil);
TAdoDataSet('dstRpt' + Trim(IntToStr(x))).CommandType := cmdStoredProc;
TAdoDataSet('dstRpt' + Trim(IntToStr(x))).Connection := MyConn;
TAdoDataSet('dstRpt' + Trim(IntToStr(x))).CommandText := 'sp1;1';
TAdoDataSet("dstRpt" + Trim(IntToStr(x))).Parameters.Refresh;
... // more parameter settings, then prepared and active, etc.
... // add the dataset to the report (FR3)
end;
... // run the report
... // free the datasets

Obviously the above will not work. I don't know how to create it without
knowing the name during design time and dont' know how to refer to the
dataset once it is been created. I presume from my reading that I need to
use NEW and pointers but the example in the help file is limited and I
don't
understand how to go about this.

Thanks,

Keith
In general you do not refer to components by the name-property, but by de
variable (=pointer) that points to your object. (How do you say this
properly?).
Anyway, what you could try something like this:
var
MyDataSets: array[1..3] of TADODataSet; //I prefer array[0..2]
begin
...
...
MyDataset[x] := TADODataSet.create(nil);
MyDataset[x].CommandType := .....
...
...
MyDataset[x].free;
end;
If the number of dataset is not known, use something like a TList or
TObjectList instead of the array;
Hope this helps,
Maurice
 

Re:creating TadoDatsets variables dynamically

That makes complete sense to me. I will give that a try. Thanks Maurice. :)
"Maurice Telkamp" <maurice.telkamp_at_beeone.nl>writes
In general you do not refer to components by the name-property, but by de
variable (=pointer) that points to your object. (How do you say this
properly?).
Anyway, what you could try something like this:
var
MyDataSets: array[1..3] of TADODataSet; //I prefer array[0..2]
begin
...
...
MyDataset[x] := TADODataSet.create(nil);
MyDataset[x].CommandType := .....
...
...
MyDataset[x].free;
end;
If the number of dataset is not known, use something like a TList or
TObjectList instead of the array;
Hope this helps,
Maurice
 

Re:creating TadoDatsets variables dynamically

This works great by the way. And since I never know at design time how many
datasets a report might have I am just using a dynamic array. Thanks for the
suggestion. Much simpler than the whole "new" with dynamic variables thing.
:) -keith
"Maurice Telkamp" <maurice.telkamp_at_beeone.nl>writes
In general you do not refer to components by the name-property, but by de
variable (=pointer) that points to your object. (How do you say this
properly?).
Anyway, what you could try something like this:
var
MyDataSets: array[1..3] of TADODataSet; //I prefer array[0..2]
begin
...
...
MyDataset[x] := TADODataSet.create(nil);
MyDataset[x].CommandType := .....
...
...
MyDataset[x].free;
end;
If the number of dataset is not known, use something like a TList or
TObjectList instead of the array;
Hope this helps,
Maurice