Solved

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

Posted on 2014-03-26
2
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 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