ajtsoukalas
asked on
Set mastersource at runtime
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
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
Sorry but I didn't get your question. are you talking about setting the master-details relationship at runtime?
If it is so then
DetailTable.Close;
DetailTable.MasterSource := NewDS;// could be PastDS, CurrentDS etc.
DetailTable.MasterFields := 'FieldMappings';
DetailTable.Open;
DetailTable.Close;
DetailTable.MasterSource := NewDS;// could be PastDS, CurrentDS etc.
DetailTable.MasterFields := 'FieldMappings';
DetailTable.Open;
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.IndexFieldName s := 'CustomerID';
DetailTable.MasterFields := 'ID';
e.g.
NewDS->ID = DetailTable->CustomerID then
DetailTable.IndexFieldName
DetailTable.MasterFields := 'ID';
ASKER
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[ind ex].Close;
datamodule.tablestring[ind ex].master source...
datamodule.tablestring[ind ex].Master Fields...
datamodule.tablestring[ind ex].Open;
end;
What would be the specific code for datamodule.tablestring[ind ex].?
Yes, but I would like to do it based on the stringlist provided by the database component
database.gettablenames('',
for index := 1 to tablesList.Count-1 do
begin
datamodule.tablestring[ind
datamodule.tablestring[ind
datamodule.tablestring[ind
datamodule.tablestring[ind
end;
What would be the specific code for datamodule.tablestring[ind
ASKER
for index := 0 to tablesList.Count-1 do
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.IndexFieldName s := ''; // set index field name
DetailTable.MasterFields := ''; // set master field
DetailTable.Open;
end
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.IndexFieldName
DetailTable.MasterFields := ''; // set master field
DetailTable.Open;
end
With your code
// Following can be set at design time
MasterDS.Dataset := datamodule.Mastertable;
datamodule.DetailTable.Mas terSource := MasterDS;
datamodule.DetailTable.Ind exFieldNam es := ''; // set index field name
datamodule.DetailTable.Mas terFields := ''; //
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.Clo se;
datamodule.MasterTable.Clo se;
datamodule.MasterTable.Tab leName := SelectedTableName;
datamodule.MasterTable.Ope n;
datamodule.DetailTable.Ope n;
// Following can be set at design time
MasterDS.Dataset := datamodule.Mastertable;
datamodule.DetailTable.Mas
datamodule.DetailTable.Ind
datamodule.DetailTable.Mas
database.gettablenames('',
//
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.Clo
datamodule.MasterTable.Clo
datamodule.MasterTable.Tab
datamodule.MasterTable.Ope
datamodule.DetailTable.Ope
ASKER
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.GetTabl eNames('', 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;
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.GetTabl
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;
'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 .Component s[x]).mast ersource) then
TTable(datamodule.Componen ts[x]).mas tersource := NewDS;
end
end
end;
The above snippet will go inside your for loop
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
TTable(datamodule.Componen
end
end
end;
The above snippet will go inside your for loop
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
I hope you might have corrected the line from
if datamodule.Components[0] is TTable then
to
if datamodule.Components[x] is TTable then
if datamodule.Components[0] is TTable then
to
if datamodule.Components[x] is TTable then
ASKER
Yes I did, thanks
AJ
AJ