Solved

PROBLEM: INSERT INTO & SELECT ................ WITH A TWIST

Posted on 2014-03-26
2
208 Views
Last Modified: 2014-03-26
Here is my SQL Statement:

str_SQL1 = "INSERT INTO tbl_2 (Field_1, Field_2) SELECT Field_1, Field_2 FROM tbl_1 WHERE tbl_1.Field_2 = " & Supplied_Variable_1 & ";"

It works fine copying one or more records from tbl_1 over to tbl_2 based on the filter Supplied_Variable_1.

But here is the twist:  I want to supply another variable (Supplied_Variable_2) into Field_2 of the target table (tbl_2)  rather than what is found in tbl_1.Field_2. How do I do this?

EXAMPLE:
Supplied_Variable_1 = 1200 (original query filter)
Supplied_Variable_2 = 2222

tbl_1
Field_1, Field_2
Abernathy, 1200
Baker, 1200
Coker, 1200
Dodds, 1200

tbl_2 (after INSERT INTO is Run)
Field_1, Field_2
Abernathy, 2222
Baker, 2222
Coker, 2222
Dodds, 2222

Last Question:  Assuming proper syntax, can you have a table run this "Append" on itself?

i.e.

str_SQL1 = "INSERT INTO tbl_1 (Field_1, Field_2) SELECT Field_1, Field_2 FROM tbl_1 WHERE tbl_1.Field_2 = " & Supplied_Variable_1 & ";"

Thanks for any help.
0
Comment
Question by:dgheck
  • 2
2 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39956077
Try this if field 2 is NUMERIC.

str_SQL1 = "INSERT INTO tbl_2 (Field_1, Field_2) SELECT Field_1, " & Supplied_Variable_2 & " FROM tbl_1 WHERE tbl_1.Field_2 = " & Supplied_Variable_1 & ";"

Open in new window


If field 2 is TEXT:

str_SQL1 = "INSERT INTO tbl_2 (Field_1, Field_2) SELECT Field_1, '" & Supplied_Variable_2 & " ' FROM tbl_1 WHERE tbl_1.Field_2 = " & Supplied_Variable_1 & ";"

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 39956080
<< Last Question:  Assuming proper syntax, can you have a table run this "Append" on itself? >>

Yes.  The syntax would be:

str_SQL1 = "INSERT INTO tbl_1 (Field_1, Field_2) SELECT Field_1, '" & Supplied_Variable_2 & " ' FROM tbl_1 WHERE tbl_1.Field_2 = " & Supplied_Variable_1 & ";"

Open in new window


(Just change the target table name)
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question