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):
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
<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:
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):
tbl_1 | ||
ID | St1 | AMT |
A1 | A | 1 |
A2 | A | 2 |
A3 | A | 3 |
A4 | A | 4 |
A5 | A | 5 |
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
tbl_2 | ||
ID | St1 | AMT |
B1 | B | 1 |
B2 | B | 2 |
B3 | B | 3 |
B4 | B | 4 |
B5 | B | 5 |
A1 | B | 6 |
A2 | B | 7 |
<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.