Help with running SQL Update and Insert queries, using two Excel tables within a workbook

winglessbuzzard

New Member
Joined
Jan 12, 2009
Messages
29
Guys & Gals,

I'm pretty decent with VBA, but I'm having a heck of a time figuring out how to use SQL insert and update queries on tables within a workbook. In my simple test, I have a workbook (Book1.xlsm) with one worksheet (Sheet1) which contains two very small tables (table objects):
tbl_1
IDSt1AMT
A1A1
A2A2
A3A3
A4A4
A5A5

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>


tbl_2
IDSt1AMT
B1B1
B2B2
B3B3
B4B4
B5B5
A1B6
A2B7

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>


I recreated these two exact tables in MS Access and got this syntax for the proper SQL code for an insert query and an append query:
  • INSERT INTO tbl_2 ( ID, St1, AMT ) SELECT tbl_1.ID, tbl_1.St1, tbl_1.AMT FROM tbl_1 LEFT JOIN tbl_2 ON tbl_1.[ID] = tbl_2.[ID] WHERE (((tbl_2.ID) Is Null));
  • UPDATE tbl_2 INNER JOIN tbl_1 ON tbl_2.ID = tbl_1.ID SET tbl_2.St1 = [tbl_1].[St1], tbl_2.AMT = [tbl_1].[AMT];

Can someone please write me some sample code that runs these two sql statements concurrently? I'd like to use this logic in a much bigger project, but I need the sample to work first.
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top