Results 1 to 6 of 6
  1. #1
    bdotr22 is offline Novice
    Windows 11 Access 2016
    Join Date
    Jun 2025
    Posts
    12

    SubForm dataset append to table


    I have a form and subform set up and it displays the data I want. the subform uses a query "qryItemList subform" based on the main form values. This part works fine.

    Click image for larger version. 

Name:	Form2.jpg 
Views:	28 
Size:	89.2 KB 
ID:	53127

    Once I get to this stage I want to append all records in "qryItemList subform" to a table "tblProdLines" which has all of the same columns. I tried a very basic append query which is triggered by the 'Add RP Lines' button, but it seems there is more to it as this appends 0 records.

    Click image for larger version. 

Name:	Query2.jpg 
Views:	28 
Size:	14.7 KB 
ID:	53124

    Obviously I am missing something here. Not sure if this is something that can be done just in basic access or needs Visual Basic. Any guidance would be greatly appreciated.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Conventional syntax is:

    INSERT INTO tablename(field1, field2) VALUES(something1, something2)

    or

    INSERT INTO tablename(field1, field2) SELECT field1, field2 FROM tableORquery

    Does your table have a primary key field? Syntax you tried (using wildcards) won't work.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    bdotr22 is offline Novice
    Windows 11 Access 2016
    Join Date
    Jun 2025
    Posts
    12
    It just has a default primary ID, just a basic number unique identifier.

  4. #4
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    430
    you can also use Code and add it to the click event your "Add RP Lines" button to add the records:
    Code:
    Private Sub cmdAddRPLines_Click()
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
        Dim fd As DAO.Field
        Dim i As Integer
        Set db = CurrentDb
        ' remove this line if you need
        If MsgBox("Delete previous records from tblProdLines before appending records?", vbQuestion + vbYesNo) = vbYes Then
            db.Execute "delete * from tblProdLines;"
        End If
        Set rs = db.OpenRecordset("tblProdLines", dbOpenDynaset)
        With Me.qryItemList_subform.Form.RecordsetClone
            If .RecordCount < 1 Then
                Exit Sub
            End If
            .MoveFirst
            Do Until .EOF
                i = i + 1
                rs.AddNew
                For Each fd In .Fields
                    rs.Fields(fd.Name) = fd.Value
                Next
                rs.Update
                .MoveNext
            Loop
        End With
        rs.Close: Set rs = Nothing
        Set db = Nothing
        MsgBox i & " record(s) added to tblProdLines."
    End Sub

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Quote Originally Posted by bdotr22 View Post
    It just has a default primary ID, just a basic number unique identifier.
    I should qualify my earlier comment. Your syntax won't work because of primary key - if primary key and corresponding field are included. Changing ID field to not be primary key would result in duplicate ID values (even if it is autonumber type).

    Actually, there is a way to make it work.
    First, your query cannot have field corresponding to table primary key field included.
    Next, primary key field in tblProdLines must be last field in table designer.
    All fields in both data sources must be in the same order.

    Then syntax would be:
    INSERT INTO tblProdLines SELECT * FROM qryItemList
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    bdotr22 is offline Novice
    Windows 11 Access 2016
    Join Date
    Jun 2025
    Posts
    12
    Quote Originally Posted by June7 View Post
    I should qualify my earlier comment. Your syntax won't work because of primary key - if primary key and corresponding field are included. Changing ID field to not be primary key would result in duplicate ID values (even if it is autonumber type).

    Actually, there is a way to make it work.
    First, your query cannot have field corresponding to table primary key field included.
    Next, primary key field in tblProdLines must be last field in table designer.
    All fields in both data sources must be in the same order.

    Then syntax would be:
    INSERT INTO tblProdLines SELECT * FROM qryItemList
    From initial testing that seems to have worked. Thanks

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

Similar Threads

  1. Replies: 6
    Last Post: 02-08-2020, 07:31 AM
  2. Autocreate new dataset in connected table?
    By Hyoryn in forum Access
    Replies: 2
    Last Post: 01-08-2017, 05:21 PM
  3. Replies: 1
    Last Post: 07-30-2015, 05:03 AM
  4. Replies: 3
    Last Post: 10-28-2014, 12:23 AM
  5. Replies: 1
    Last Post: 03-09-2010, 10:23 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