[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 206
  • Last Modified:

Setting Component Properties For All Components On A Form

I have a data module with a ton of TDataset descendent components (ApolloDataSet). I need to set the Database Property for each of them and then Open them. While I could write a long list of code setting and opening each one individually, I'd like to do this by iterating through all the datasets in the data module. This way, I could use the same technique to do other functions such as pack and reindex.

Here's what I have so far - this is a procedure in my data module:

procedure TDM.SetDatabase;
var
  f: integer;
begin
  for f := 0 to ComponentCount - 1 do
    if Components[f].InheritsFrom(TApolloDataSet) then begin
    //If component is a Dataset component
      Components[f].DatabaseName := ApDSetLParamDB_PATH.Text;
      Components[f].Open;
    end;  //if dataset
end;

What I get here is a compile error: Undeclared identifier: 'DatabaseName'
Undeclared identifier: 'Open'

If the above lines read (for example):

      ApDSetCity.DatabaseName := ApDSetLParamDB_PATH.Text;
      ApDSetCity.Open;

then all is fine.

What I need is to be able to access the properties and methods when the component has the form: Components[f]

I'm hoping the answer is an easy one like: Use TDM(Components[f]).DatabaseName
(This doesn't work either)

Anyway - it should be a handy technique once it is resolved.

If there is a better way to handle component properties and methods for all the components on a form, I am all ears.

Thanks for your help

MikeADST



0
MikeADST
Asked:
MikeADST
  • 3
  • 2
  • 2
  • +1
1 Solution
 
rarigoCommented:
Hi MikeADST,
  I think something like this must work.
 
  for n := 1 to ComponentCount - 1 do
   begin
     if ( Components[n-1] as TComponent ).ClassType = TApolloDataSet then
     begin
         Components[n].DatabaseName := ApDSetLParamDB_PATH.Text;
         Components[n].Open;
     end;
  end;



Sinceramente,
Reginaldo
0
 
intheCommented:
use the (components as tclassname).database := ...

for instance:

var
  f : integer;
begin
  for f := 0 to ComponentCount-1 do
    begin
      if( Components[f] is TEdit )then
      begin
        (Components[f] as TEdit).Text := '';
      end;
  end;
0
 
MikeADSTAuthor Commented:
inthe

Your Solution was Perfect!
I've learned about 2 new Delphi words: is and as. There is no way I could have picked it up from the help files.

Post your comment as an answer and the points are yours.

Thanks for your quick response!

MikeADST



Reginaldo

Your comment is a good start - however the expression:
  Components[n].DatabaseName
needed to be:
  (Components[n].DatabaseName as TApolloDataSet).DatabaseName

This is what I learned from inthe's comment above.

Thanks for your input.

MikeADST

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
MikeADSTAuthor Commented:
inthe

Your Solution was Perfect!
I've learned about 2 new Delphi words: is and as. There is no way I could have picked it up from the help files.

Post your comment as an answer and the points are yours.

Thanks for your quick response!

MikeADST



Reginaldo

Your comment is a good start - however the expression:
  Components[n].DatabaseName
needed to be:
  (Components[n].DatabaseName as TApolloDataSet).DatabaseName

This is what I learned from inthe's comment above.

Thanks for your input.

MikeADST

0
 
kretzschmarCommented:
hi mikeadst,

you can also do a conversion like

procedure TDM.SetDatabase;
var
  f: integer;
begin
  for f := 0 to ComponentCount - 1 do
   if (Components[f] is TApolloDataSet) then
   begin
     TApolloDataSet(Components[f]).DatabaseName := ApDSetLParamDB_PATH.Text;
     TApolloDataSet(Components[f]).Open;
   end;  //if dataset
end;

but i'm late and barry should get the points

merry XMas barry

meikl ;-)
0
 
intheCommented:
cheers  
and happy holidays to all :-)
Regards Barry
0
 
rarigoCommented:
 And didn't solve your problem!!!?

Reginaldo
0
 
MikeADSTAuthor Commented:
Thanks to all for your comments!
Experts-Exchange is the best!

Mike

0

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now