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):
[TABLE="width: 192"]
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]tbl_1[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD]ID[/TD]
[TD]St1[/TD]
[TD]AMT[/TD]
[/TR]
[TR]
[TD]A1[/TD]
[TD]A[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]A2[/TD]
[TD]A[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD]A3[/TD]
[TD]A[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD]A4[/TD]
[TD]A[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD]A5[/TD]
[TD]A[/TD]
[TD="align: right"]5[/TD]
[/TR]
</tbody>[/TABLE]


[TABLE="width: 192"]
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]tbl_2[/TD]
[TD="width: 64"][/TD]
[TD="width: 64"][/TD]
[/TR]
[TR]
[TD]ID[/TD]
[TD]St1[/TD]
[TD]AMT[/TD]
[/TR]
[TR]
[TD]B1[/TD]
[TD]B[/TD]
[TD="align: right"]1[/TD]
[/TR]
[TR]
[TD]B2[/TD]
[TD]B[/TD]
[TD="align: right"]2[/TD]
[/TR]
[TR]
[TD]B3[/TD]
[TD]B[/TD]
[TD="align: right"]3[/TD]
[/TR]
[TR]
[TD]B4[/TD]
[TD]B[/TD]
[TD="align: right"]4[/TD]
[/TR]
[TR]
[TD]B5[/TD]
[TD]B[/TD]
[TD="align: right"]5[/TD]
[/TR]
[TR]
[TD]A1[/TD]
[TD]B[/TD]
[TD="align: right"]6[/TD]
[/TR]
[TR]
[TD]A2[/TD]
[TD]B[/TD]
[TD="align: right"]7[/TD]
[/TR]
</tbody>[/TABLE]


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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

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