Results 1 to 4 of 4
  1. #1
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94

    Insert entire row from query into new row in another table

    I have a query with x number of fields. I would like to copy an entire record in the query into a new record in an unrelated table.

    The field names in the query and the table match but there are more fields in the table than in the query.

    How can I copy an entire row from the query into a new row in the table - without specifying each and every target field and value? I have run up against the 256 character limit of the SQL statement.

    eg: Query has field 1, 3, 4, 6, 8 & 10
    Table has fields 1,2,3,4,5,6,7,8,9,10

    Thx...

  2. #2
    apr pillai's Avatar
    apr pillai is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2010
    Location
    Alappuzha, India
    Posts
    209
    Copy the following Code into a Standard Module and save it:

    Code:
    Public Function CopyRecord(ByVal intblQry As String, ByVal outTbl As String)
    Dim db As Database, rst1 As Recordset, rst2 As Recordset
    Dim fldLoop1 As Field, fldLoop2 As Field, fldName As String, fldValue
    
    Set db = CurrentDb
    Set rst1 = db.OpenRecordset(intblQry, dbOpenDynaset)
    Set rst2 = db.OpenRecordset(outTbl, dbOpenDynaset)
    
    Do While Not rst1.EOF
       rst2.AddNew
       For Each fldLoop1 In rst1.Fields
           fldName = fldLoop1.Name
           fldValue = fldLoop1.Value
           For Each fldLoop2 In rst2.Fields
               If fldLoop2.Name = fldName Then
                   rst2.Fields(fldLoop2.Name).Value = fldValue
                   Exit For
               End If
           Next
        Next
        rst2.Update
        rst1.MoveNext
    Loop
    
    rst1.Close
    rst2.Close
    
    Set rst1 = Nothing
    Set rst2 = Nothing
    Set db = Nothing
    
    End Function
    Run the Code with the following Syntax:

    Code:
    CopyRecord "QueryName", "TableName"

  3. #3
    jo15765's Avatar
    jo15765 is offline Expert
    Windows XP Access 2000
    Join Date
    Nov 2010
    Location
    6 Feet Under
    Posts
    672
    You could also save the query. Then build an append query of the query you just saved, and use the query you initially saved as the source of your append query. That way it will append the records in the query.

  4. #4
    chris.williams is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2011
    Posts
    94
    Thanks - I will give these two solutions a shot.

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

Similar Threads

  1. Replies: 3
    Last Post: 09-18-2011, 03:46 PM
  2. Replies: 2
    Last Post: 05-03-2011, 01:02 AM
  3. Insert query into table FROM two table sources
    By svcghost in forum Queries
    Replies: 2
    Last Post: 11-05-2010, 09:10 AM
  4. Replies: 0
    Last Post: 02-24-2010, 12:56 AM
  5. Insert Query output into a table
    By ammu_sridhar in forum Programming
    Replies: 1
    Last Post: 06-12-2009, 01:09 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