?
Solved

Set mastersource at runtime

Posted on 2006-06-21
13
Medium Priority
?
761 Views
Last Modified: 2012-08-13
I have a mySQL database with a number of tables.  I would like to set the mastersource with one of three other tables depending on a user entry.

New NewDS
Past PastDS
Current CurrentDS

I would like to do it in such a way that I don't have to modify the code if I add a table to database.

I am leaning toward DAC for mySql and the database component has a gettablenames which will give me the list of tables by name.
How would I assign the mastersource for each table in the stringlist?

AJ
0
Comment
Question by:ajtsoukalas
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16951916
Sorry but I didn't get your question. are you talking about setting the master-details relationship at runtime?
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16952006
If it is so then
DetailTable.Close;
DetailTable.MasterSource := NewDS;// could be PastDS, CurrentDS etc.
DetailTable.MasterFields := 'FieldMappings';
DetailTable.Open;
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16952125
One more thing to note here is that the relative field in DetailTable must be set as an active index before you open the details table.
e.g.
NewDS->ID = DetailTable->CustomerID then

DetailTable.IndexFieldNames := 'CustomerID';
DetailTable.MasterFields := 'ID';
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:ajtsoukalas
ID: 16952163
atul parmar

Yes, but I would like to do it based on the stringlist provided by the database component

database.gettablenames('', tablesList);
for index := 1 to tablesList.Count-1 do
   begin
   datamodule.tablestring[index].Close;
   datamodule.tablestring[index].mastersource...
   datamodule.tablestring[index].MasterFields...
   datamodule.tablestring[index].Open;
   end;

What would be the specific code for  datamodule.tablestring[index].?

   
0
 

Author Comment

by:ajtsoukalas
ID: 16952190
for index := 0 to tablesList.Count-1 do
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16952390
So tablesList will have the list of master tables and then you want to set the mastersource of details table to the one of that tablelist. Right?

But your skeleton code is confusing. (If the tablesList will have one entry then it's fine but looping through it and assigning the values of tablesList as mastersource of details table serves no purpose)

Anyway, I guess that tablesList will number of table names and based on user selection you want to assign the corresponding mastersource to details table.

Then you will have one MasterTable and one DetailTable component.
var
  SelectedTableName : String;
begin
  SelectedTableName := tablesList[i]; //selection made based on user input
  //
  MasterTable.Close;
  MasterTable.TableName := SelectedTableName;
  MasterTable.Open;
  DetailTable.Close;
  DetailTable.MasterSource := MasterDS;
  DetailTable.IndexFieldNames := ''; // set index field name
  DetailTable.MasterFields := ''; // set master field
  DetailTable.Open;
end
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16952547
With your code

  // Following can be set at design time
  MasterDS.Dataset := datamodule.Mastertable;
  datamodule.DetailTable.MasterSource := MasterDS;
  datamodule.DetailTable.IndexFieldNames := ''; // set index field name
  datamodule.DetailTable.MasterFields := ''; //

database.gettablenames('', tablesList);

//
   for index := 1 to tablesList.Count-1 do
   begin
      // select appropriate table to master.
      e.g. if tablesList[i] matches user selection then
         SelectedTableName := tablesList[i];
   end;

      datamodule.DetailTable.Close;
      datamodule.MasterTable.Close;
      datamodule.MasterTable.TableName := SelectedTableName;
      datamodule.MasterTable.Open;
      datamodule.DetailTable.Open;
0
 

Author Comment

by:ajtsoukalas
ID: 16952576
Please bear with me as I have not described this as I meant to

There are 3 driver tables
New
Current
Past

Depending on the user request I need to change all of the mastersources of the detail tables to the selected datasource.  

I don't know how to substitute the information returned by the list to do the assignment.

if my datamodule is pkData and the list contains
empphones
empaddress
emprates

and the user selects New

pkData.HrKDatabase.GetTableNames('', tablesList);
for Index := 0 to tablesList.Count-1 do
   begin
   if userselect = 'New' then
       pkdata.'what would go here'.mastersource := NewDS
   else
   if userselect = 'Past' then
       pkdata.'what would go here'.mastersource := PastDS
   else
       pkdata.'what would go here'.mastersource := CurrentDS;
   end;
0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16952714
'what would go here' -> the name of the detail table. what you can not assign using tablesList. However you can go though all the tables of datamodule and check for the mastersource. if it is assigned then change it to new one.

e.g.
var
  x : integer;
begin
  for x := 0 to datamodule.ComponentCount - 1 do
  begin
    if datamodule.Components[0] is TTable then
    begin
      if assigned(TTable(datamodule.Components[x]).mastersource) then
        TTable(datamodule.Components[x]).mastersource := NewDS;
    end
  end
end;

The above snippet will go inside your for loop
0
 
LVL 10

Accepted Solution

by:
atul_parmar earned 2000 total points
ID: 16952732
Mate, now its time for me to move. I hope the above will help you.
0
 

Author Comment

by:ajtsoukalas
ID: 16952799
Thanks
I understand now.

Your code is more involved then being able to substitute the list names but it will work just as well.

Thanks,

AJ


0
 
LVL 10

Expert Comment

by:atul_parmar
ID: 16957587
I hope you might have corrected the line from
if datamodule.Components[0] is TTable then
to
if datamodule.Components[x] is TTable then
0
 

Author Comment

by:ajtsoukalas
ID: 16960747
Yes I did, thanks

AJ
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question