Results 1 to 2 of 2
  1. #1
    TheDeceived is offline Novice
    Windows Vista Access 2007
    Join Date
    Sep 2010
    Posts
    8

    INSERT INTO ... SELECT Subquery

    Hey all. I'm back again for some of your wisdom



    I'm trying to create a copy and paste function in my database using the INSERT INTO and SELECT subquery. Basically, the user should be able to copy an item and all of it's children to another parent group.

    The query which I have got so far is (pseudo code):

    Code:
    INSERT INTO Table1 (Table1.Column1, Table1.Column2, Table1.Column3)
    VALUES (ManualValue1, ManualValue2, SELECT Table1.Column3 FROM Table1 WHERE Table1.Column1 = Condition1 AND Table1.Column2 = Condition2
    What this should be doing is inserting two manual values into Column1 and Column2 of Table1, and then copying the value in Column3 of the same table where Column1 and Column2 are equal to a condition.

    It will probably help if I put it into an example. If I we name the table "CustomerNames", and the columns FirstName, MiddleName and Surname. Imagine one record already exists within this table:

    Code:
    FirstName | MiddleName | Surname
    "John" | "Adam" | "Smith"
    Now what I want to do is add another record in there where I can manually set the FirstName and MiddleName fields, but copy the Surname field. Based on the pseudo code I wrote above, I'd write the SQL statement as:

    Code:
    INSERT INTO CustomerNames (CustomerNames.FirstName, CustomerNames.MiddleName, CustomerNames.Surname)
    VALUES ('Paul', 'Mark', SELECT CustomerNames.Surname FROM CustomerNames WHERE CustomerNames.FirstName = 'John' AND CustomerNames.MiddleName = 'Adam'
    So the table would now look like this:

    Code:
    FirstName | MiddleName | Surname
    "John" | "Adam" | "Smith"
    "Paul" | "Mark" | "Smith"
    However, I keep getting an error from Access claiming that CustomerName.Surname does not exist.

    After spending hours looking through Google and this forum, I cannot find an answer as to how to properly setup an INSERT INTO ... SELECT SQL statement where you have manual + copied values being entered into a table.

    Could anyone point out where I'm going wrong here?

  2. #2
    weekend00 is offline I may not be right
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    1,295
    Code:
    INSERT INTO CustomerNames (CustomerNames.FirstName, CustomerNames.MiddleName, CustomerNames.Surname)
    SELECT 'Paul', 'Mark', CustomerNames.Surname FROM CustomerNames WHERE CustomerNames.FirstName = 'John' AND CustomerNames.MiddleName = 'Adam'

Please reply to this thread with any new information or opinions.

Similar Threads

  1. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  2. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 PM
  3. Replies: 4
    Last Post: 09-03-2010, 02:55 PM
  4. Replies: 0
    Last Post: 09-08-2009, 11:01 AM
  5. Problem with subquery
    By bakerdenn in forum Queries
    Replies: 1
    Last Post: 04-24-2009, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums