Solved

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

Posted on 2014-03-26
2
206 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now