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



LVL 1
MikeADSTAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rarigoCommented:
 And didn't solve your problem!!!?

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

Mike

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.