Results 1 to 9 of 9
  1. #1
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229

    INSERT with SELECT query.

    I have a SELECT query whose results I need to INSERT into another table. Currently I have made the query an Insert query but this makes testing and development difficult. Most times I just need to browse the records returned without inserting them as they already exist in the target table. I have kept a separate copy of the query as SELECT but keeping them both updated is also demanding. Is it possible to use an INSERT statement with Currentdb.Execute and pass the SELECT query as an argument/parameter instead of individual records?

  2. #2
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    You can create a parameterised query for the SELECT. Then, in your code, you do something like this:

    Code:
        'rsNew, rsAdd are DAO.RecordSet
        'qdfAdd is DAO.QueryDef
     
        Set qdfAdd = db.QueryDefs("YourParameterisedQuery")
        qdfAdd.Parameters!param1 = param1Value
        Set rsAdd = qdfAdd.OpenRecordset()
        rsAdd.MoveFirst
     
        Set rsNew = db.OpenRecordset("YourTableName")
        rsNew.AddNew
        rsNew!field1 = rsAdd!field1
        rsNew!field2 = rsAdd!field2
        rsNew!field3 = rsAdd!field3
        .....
        rsNew.Update

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    This is a small example of how to use Select with Insert Into used with currentdb.execute.


    Code:
    Dim strSQL As String
    strSQL = "INSERT INTO Table3 ( EX_1, Ticket ) SELECT Table2.EX_1, Table2.Ticket FROM Table2 WHERE (((Table2.Ticket)=2));"
    CurrentDb.Execute strSQL, dbFailOnError
    
    or 
    
    Dim strSQL As String
    strSQL = "Insert Into Table3(EX_1,Ticket)Select Table2.EX_1,Table2.Ticket From Table2 Where Ticket=2"
    CurrentDb.Execute strSQL, dbFailOnError

  4. #4
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Thank you both for your responses; however, please note that both your approaches are row-based, i.e. they process one row at a time. I need a set-based solution, one which does not need to loop through a recordset.

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,726
    Quote Originally Posted by goodguy View Post
    Thank you both for your responses; however, please note that both your approaches are row-based, i.e. they process one row at a time. I need a set-based solution, one which does not need to loop through a recordset.
    stmoong has given a recordset approach
    maximus is sql based

    What do you have in mind for "set based"? I would suggest that maximus has given a set approach.

  6. #6
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    If you're looking to insert multiple records at once, then you may use the SQL approach suggested by maximus.

    Also, reference from MSDN:
    http://msdn.microsoft.com/en-us/libr...ffice.12).aspx

    Actually, if you need to browse the data to be inserted during development, you can just click on the datasheet view, and not the Run button. This is same for the Delete.

  7. #7
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    Thanks, all of you.
    Maximus' solution can be set-based WITHOUT the WHERE clause.
    The Microsoft link on INSERT INTO syntax says that multiple records can be appended to a target table provided they are coming from a tableexpression i.e. one or more tables. I am appending results of a query to a table. Seems query is not supported.
    Syntax

    Multiple-record append query:
    INSERT INTO target [(field1[, field2[, …]])] [IN externaldatabase] SELECT [source.]field1[, field2[, …] FROM tableexpression
    The INSERT INTO statement has these parts: PartDescription targetThe name of the table or query to append records to. field1, field2Names of the fields to append data to, if following a target argument, or the names of fields to obtain data from, if following a source argument. externaldatabaseThe path to an external database. For a description of the path, see the IN clause. sourceThe name of the table or query to copy records from. tableexpressionThe name of the table or tables from which records are inserted. This argument can be a single table name or a compound resulting from an INNER JOIN, LEFT JOIN, or RIGHT JOIN operation or a saved query.

  8. #8
    stmoong is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Sep 2011
    Location
    Singapore
    Posts
    108
    From what I understand, the WHERE clauses in the SELECT query suggested by maximus is used to return a set of filtered records. So, in effect, it allows you to insert a set of filtered records.

    Also, you can use a SELECT * FROM some complicated query with join, as the documentation states that tableexpression is "The name of the table or tables from which records are inserted. This argument can be a single table name or a compound resulting from an INNER JOIN, LEFT JOIN, or RIGHT JOIN operation or a saved query."

  9. #9
    goodguy is offline Competent Performer
    Windows Vista Access 2007
    Join Date
    Dec 2010
    Location
    Zanzibar, Tanzania
    Posts
    229
    OK, will try.

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. Replies: 3
    Last Post: 09-13-2011, 07:58 PM
  3. select and insert using two seperate tables
    By benjammin in forum Queries
    Replies: 2
    Last Post: 02-20-2011, 04:52 PM
  4. INSERT INTO ... SELECT Subquery
    By TheDeceived in forum Programming
    Replies: 1
    Last Post: 10-19-2010, 09:29 AM
  5. Replies: 0
    Last Post: 09-08-2009, 11:01 AM

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