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:
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.
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.