Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Updating a dBase table based on values from a multi-table query: Delphi 4.0 Professional

Posted on 2000-04-27
3
Medium Priority
?
390 Views
Last Modified: 2013-11-24
I need to update a field in a dBase table based upon calculations performed against several fields in different dBase tables. I can successfully update the table based on equations that use values that exist in the same table.  
e.g. UPDATE ''activity'' SET calc_act =id_src*1000
 
However  I need to alter the update sql  to use values from other tables.  
e.g. Update  ''activity'' SET calc_act =id_src*1000*Table2.field.value/Table3.field.value

 I need to alter the update to use values from other tables.  How do I accomplish this?  

 I can successfully join the tables using SQL and can display the result set in a tdbgrid.  But the result set does not appear to be "live" even though I have requested such.  So changes I enter to the grid do not transfer to the underlying tables.  Why?  How can I make this work?
0
Comment
Question by:Zachary
[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
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
shenqw earned 2000 total points
ID: 2758020
1.Use the follow line sql to update the table

update test
  set v=(select v from test2 where id=test.id)*(select v from test1 where id=test.id)
  where id in
       (select test1.id from test1 inner join test2 on test1.id=test2.id)


2. you must use TUpdateSQL.

i.  add tquery,tupdatesql1,tupdatesql2.....(many tupdatesqls as your table you want to update)
ii. set you tquery.sql.text:='select test1.id,test1.fieldvalue,test2.fieldvalue from table1 inner join table2 on table1 on table1.id=table2.id';
   and set TQuery.requestLive:=True;
iii. use tupdatesql1 to update table1. settings:
   tupdatesql1.deletesql.Text:='delete test1 where id=OLD_ID';
   tupdatesql1.insertsql.Text:='insert into test1 values(:NEW_ID,:NEW_V)';
   tupdatesql1.modifysql.Text:='UPDATE TEST1 SET V=:new_v where ID=:OLD_ID';
   
   the table2 and tupdatesql2 ......

iv. On TQuery.OnUpdateRecord event:

procedure TForm1.Query1UpdateRecord(DataSet: TDataSet;
  UpdateKind: TUpdateKind; var UpdateAction: TUpdateAction);
begin
  Query1.UpdateObject:=updatesql1;
  updatesql1.SetParams(UpdateKind);
  updatesql1.ExecSQL(UpdateKind);

  Query1.UpdateObject:=updatesql2;
  updatesql2.SetParams(UpdateKind);
  updatesql2.ExecSQL(UpdateKind);

  UpdateAction:=uaApplied;
end;


v. all of above,then you can do it like before in tdbgrid.

vi. don't modify the id value. or you will get wrong value.

//Based on NT4+DBase5+Delph5+Pack1.
//If you need , leave you e-mail, i will send the all project source code and the database.
0
 
LVL 3

Expert Comment

by:shenqw
ID: 2758090
Sorry ,something is wrong.

Here is a example that show how to use tupdatesql and cacheupdate.

2. you must use TUpdateSQL.

i.  add tquery,tupdatesql1,tupdatesql2.....(many tupdatesqls as your table you want to update)

ii. set you tquery.sql.text:='select test1.id,test1.v v1,test2.v v2 from table1 inner join table2 on table1 on table1.id=table2.id';
   and set TQuery.requestLive:=True; AND TQuery.CacheUpdate:=True;

iii. use tupdatesql1 to update table1. settings:
   tupdatesql1.deletesql.Text:='delete from test1 where id=:OLD_ID';
   tupdatesql1.insertsql.Text:='insert into test1(ID,V) values(:ID,:V1)';
   tupdatesql1.modifysql.Text:='UPDATE TEST1 SET V=:V1 where ID=:OLD_ID';
   
   the table2 and tupdatesql2 ......

   tupdatesql1.deletesql.Text:='delete from test2 where id=:OLD_ID';
   tupdatesql1.insertsql.Text:='insert into test2(ID,V) values(:ID,:V12)';
   tupdatesql1.modifysql.Text:='UPDATE TEST2 SET V=:V2 where ID=:OLD_ID';

iv. On TQuery.OnUpdateRecord event:

procedure TForm1.Query1UpdateRecord(DataSet: TDataSet;
  UpdateKind: TUpdateKind; var UpdateAction: TUpdateAction);
begin
  Query1.UpdateObject:=updatesql1;
  updatesql1.SetParams(UpdateKind);
  updatesql1.ExecSQL(UpdateKind);

  Query1.UpdateObject:=updatesql2;
  updatesql2.SetParams(UpdateKind);
  updatesql2.ExecSQL(UpdateKind);

  UpdateAction:=uaApplied;
end;


v. when you want to write the change into database ,do this

procedure TForm1.Button1Click(Sender: TObject);
begin
  Query1.ApplyUpdates;
  Query1.CommitUpdates;
end;

0
 

Author Comment

by:Zachary
ID: 2764894
Thanks alot!
And thanks for being so concise!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
MS Access 2003 or later To MySQL Migration Project Hello All, this is my second article in the category of MS-OFFICE Automation. In internet I am not able to find any comprehensive resource on the Migration of MS Access back-end to MySQL so I fin…
Viewers will learn the different options available in the Backstage view in Excel 2013.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

636 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