How to copy fields from table a and insert them into table b

I am useing Delphi 7
I have access 2003 database.
I have customerinfo table in it.
I have estimates table in it.
I need to copy
FullName,Address,PhoneNumber,ZipCode,VIN,HaleWork,DentWork,Year,Make,Model,Miles from customerinfo table to estimates table. Field names are the same in both tables.
I need example code ASAP so this is 500 pts.
Grant FullenAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rfwoolfConnect With a Mentor Commented:
Okay here's the updated SQL statement based on the information you have presented executes correctly in access, but you should double-check)
UPDATE estimates, customerinfo
SET estimates.Fullname = customerinfo.Fullname, 
estimates.Address = customerinfo.Address,
estimates.PhoneNumber = customerinfo.PhoneNumber,
estimates.ZipCode = customerinfo. ZipCode,
estimates.VIN = customerinfo.VIN,
estimates.HaleWork = customerinfo.HaleWork,
estimates.DentWork = customerinfo.DentWork,
estimates.Year = customerinfo.Year,
estimates.Make = customerinfo.Make,
estimates.Model = customerinfo.Model,
estimates.Miles = customerinfo.Miles
WHERE (estimates.EstimateID = customerinfo.EstimateID);

Open in new window

0
 
rfwoolfCommented:
Well there are two ways of doing this: SQL, and using datasets in Delphi.
The SQL way where inside delphi you send the SQL message to the server is the best because all the work is done on the server - and all the information "stays" on the server.
But you can also do this in Delphi, where the data will have to be transported from the server, to your datasets in delphi, processed, and sent back to your server. This is only good if you are in a big hurry and you are only doing this procedure once. Otherwise it is a stupid idea.

I will do the Delphi way first because it's easier and quicker FOR ME, but I will try come back later with the SQL version. You could also ask your question in the SQL zone.

Right, so the best/easiest way to talk to an access database is using ADO.

Put two TADOTables on your form.
Using the one ADOTAble, build a connection string to connect to your access table (MDB file).
Example: Click on ADOTable1, in object inspector click on Connection String, then elpisis(...)
By "Use Connection String" click "Build Button"
Select Microsoft Jet 4.0 OLE DB PRovider
-Click NExt
Under "Select or enter database name" click the elipsis button(...) and navigate to your access database (MDB file).
Click Test Connection button.
When done, copy the connection string from your ADOTable 1 to ADOTable2.

Click on ADOtable1 and under 'Table' set the table name to 'customerinfo'
On ADOTAble2, under 'table' set the table name to 'Estimates'

Now, put a button on your form, double-click it, and put this code:
begin
  ADOTable1.first;
  while ADOTAble1.Eof = false do
  begin
    with ADOTable2 do begin
      insert;
      Fieldbyname('Fullname').value := ADOTable1.Fieldbyname('Fullname').Value;
      Fieldbyname('Address').value := ADOTable1.Fieldbyname('Address').Value;
      Fieldbyname('PhoneNumber').value := ADOTable1.Fieldbyname('PhoneNumber').Value;
      Fieldbyname('ZipCode').value := ADOTable1.Fieldbyname('ZipCode').Value;
      Fieldbyname('VIN').value := ADOTable1.Fieldbyname('VIN').Value;
      Fieldbyname('HaleWork').value := ADOTable1.Fieldbyname('HaleWork').Value;
      Fieldbyname('DentWork').value := ADOTable1.Fieldbyname('DentWork').Value;
      Fieldbyname('Year').value := ADOTable1.Fieldbyname('Year').Value;
      Fieldbyname('Make').value := ADOTable1.Fieldbyname('Make').Value;
      Fieldbyname('Model').value := ADOTable1.Fieldbyname('Model').Value;
      Fieldbyname('Miles').value := ADOTable1.Fieldbyname('Miles').Value;
      post;
    end;
    ADOTable1.Next;
  end;
end;

Notes:
- You can improve upon the field matches like this
      Fieldbyname('Miles').asInteger := ADOTable1.Fieldbyname('Miles').asInteger;
it is recommended that you study the field types of all these fields to ensure you they are the same in each table. For example if one field type is a String, and the other field type is an integer, you may have problems.
0
 
rfwoolfCommented:
Sorry I forgot, your tables must be open. You can do this in code at the beginning of your button's onclick event by saying
ADOTAble1.Open;
ADOTable2.Open;

Or you can do this at design time - click on the ADOTable1 and 2 and in object inspector, under 'Active', set to True.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
rfwoolfCommented:
Now you can also try the SQL way.
This time you put a TADOQuery on your form, and you will still have to build a Connection String like before.
Then try this SQL (you can put it in the SQL property of the ADOQuery in the object inspector, or you can code it like this:
ADOQuery1.SQL.Text := 'ALL MY SQL GOES HERE';

then when you are ready to execute the query, you just say
ADOQUery1.ExecSQL;
INSERT INTO ESTIMATES(FullName, Address, PhoneNumber, ZipCode, VIN, HaleWork, DentWork, Year, Make, Model, Miles) SELECT FullName, Address, PhoneNumber, ZipCode, VIN, HaleWork, DentWork, Year, Make, Model, Miles FROM CUSTOMERINFO;

Open in new window

0
 
Grant FullenAuthor Commented:
CRAP i forgot to tell you only where the EstimateID in table a matches the EstimateID in table 2.
0
 
rfwoolfCommented:
You didn't even mention EstimateID as a field.
0
 
rfwoolfCommented:
Tell me, why would your customerinfo table have an EstimateID?
Are you explaining this correctly? So you have only 2 relevant tables here: Estimates and Customerinfo. Both of those tables have an EstimateID field. You want to copy the values of those fields from one table to the other WHERE the EstimateID fields are the same?
0
 
Grant FullenAuthor Commented:
Sorry I miss the EstimateID field in each table. This is the auto field that connects each estimate to the correct User Info.
Thanks for your help. I will try it now.
0
 
Grant FullenAuthor Commented:
Ok i put the sql in the querys sql text and then i try to set active to true.
I get error.
No value given for one or more required parameters.
UPDATE estimates, customerinfo
SET estimates.Fullname = customerinfo.Fullname, 
estimates.Address = customerinfo.Address,
estimates.PhoneNumber = customerinfo.PhoneNumber,
estimates.ZipCode = customerinfo.ZipCode,
estimates.VIN = customerinfo.VIN,
estimates.HaleWork = customerinfo.HaleWork,
estimates.DentWork = customerinfo.DentWork,
estimates.Year = customerinfo.Year,
estimates.Make = customerinfo.Make,
estimates.Model = customerinfo.Model,
estimates.Miles = customerinfo.Miles
WHERE (estimates.EstimateID = customerinfo.EstimateID);

Open in new window

0
 
rfwoolfCommented:
That's odd. It works for me, but I'm not using Access 2003 - I'm probably using 2000.
Try it inside access and see what happens.
0
 
rfwoolfCommented:
Carefully check all your field names. If it doesn't see something as a field it will think it's a parameter, so check carefully that you have spelled all your column names correctly and that those columns do exist in each table.
You can also try testing it by only doing one field, like this
UPDATE estimates, customerinfo
SET estimates.Fullname = customerinfo.Fullname
WHERE (estimates.EstimateID = customerinfo.EstimateID);

Open in new window

0
 
Grant FullenAuthor Commented:
Thanks i am looking.

0
 
Grant FullenAuthor Commented:
Ok i checked spelling and stuff.

AQCopyInfo: commandtext does not return a result set
UPDATE Estimates, customerinfo
SET Estimates.Fullname = customerinfo.Fullname
WHERE (Estimates.EstimateID =  customerinfo.EstimateID);

Open in new window

0
 
rfwoolfCommented:
That's correct. You get queries that return a result set, and queries that do not. (If you go back to learning SQL they tell you there is DDL, DML, etc etc). Yours is an update query, so it won't return a result set. Ideally you should be EXECUTING the query, and not OPENING it.
AQCopyINfo.ExecSQL.
0
 
Grant FullenAuthor Commented:
Thanks for your patience. Great Job
0
All Courses

From novice to tech pro — start learning today.