Results 1 to 10 of 10
  1. #1
    Join Date
    May 2012
    Posts
    10

    VBA How do I add/append a recordset to another recordset or table

    Hi,

    > I am going to be looping a query a couple of hundred times and want the recordset from each loop to be kept/stored. I am comfortable with the looping but I do not know how or where to store the information.
    > Should I store the query recordset in a second recordset or a table? Each loop I will add records to this recordset/table so it will become increasingly larger. The recordset/table structure will always be the same
    > How do I get the information from my query recordset into a second recordset/table using vba & sql?



    >I have removed the loop and the majority of the code to simplify it to a single instance to make it more digestible

    >Any comments or ideas to help are appreciated.

    Thanks in advance

    Ron

    Code:
    Sub Loopin()  
    Dim QueryA As DAO.Recordset
    Dim dbs As DAO.Database
    Dim strSQLA As String
    Dim tblResults As DAO.Recordset
    
    
    
    
    Set dbs = CurrentDb
    
    
    'Query SQL String for Day
    strSQLA = "SELECT [Product Code] " & _
    "FROM S2;"
    
    
    
    
    'Set Recordset from Query which is data to Import
    Set QueryA = dbs.OpenRecordset(strSQLA)
    
    
    '??Set Recordset for the Table to Insert into?? Do I have to do this
    Set tblResults = dbs.OpenRecordset("R1")
    
    
    'Add Results from Recordset QueryA into my results recordset/table??
    
    
    'If I am going to be doing many loops and adding many times should I add to a recordset continoulsy then a table or keep adding to a table how do i do this?
    
    
    
    
    
    
    End Sub

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    I would just use an Append query, if it is a case of just copying the same data.?

    Not sure you actually need to, but that is your choice.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by elusiveluderick View Post
    I am going to be looping a query a couple of hundred times and want the recordset from each loop to be kept/stored.
    Tables store data, so you need a table.

    I would change the variable name from "tblResults" to "rsResults" since you are naming a variable for a recordset.
    The table to save all the looped data could/would be named "tblResults"

    And you need a few clean up lines at the end
    Code:
        'clean up
        QueryA.Close
        rsResults.Close    '<- renamed
        Set QueryA = Nothing
        Set rsResults = Nothing     '<- renamed
        Set dbs = Nothing
    End Sub

  4. #4
    Ron is offline Novice
    Windows 10 Access 2007
    Join Date
    Sep 2020
    Posts
    5
    Quote Originally Posted by Welshgasman View Post
    I would just use an Append query, if it is a case of just copying the same data.?

    Not sure you actually need to, but that is your choice.
    Thanks for the input. Do you know if I can "Append" a Recordset instead of an SQL Append Query of a table or is that not a thing?

  5. #5
    Ron is offline Novice
    Windows 10 Access 2007
    Join Date
    Sep 2020
    Posts
    5
    Quote Originally Posted by ssanfu View Post
    Tables store data, so you need a table.

    I would change the variable name from "tblResults" to "rsResults" since you are naming a variable for a recordset.
    The table to save all the looped data could/would be named "tblResults"

    And you need a few clean up lines at the end
    Code:
        'clean up
        QueryA.Close
        rsResults.Close    '<- renamed
        Set QueryA = Nothing
        Set rsResults = Nothing     '<- renamed
        Set dbs = Nothing
    End Sub
    Thanks. Will do.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    Quote Originally Posted by Ron View Post
    Thanks for the input. Do you know if I can "Append" a Recordset instead of an SQL Append Query of a table or is that not a thing?
    You can add records with a recordset and from a recordset, but I was thinking of this way?

    https://docs.microsoft.com/en-us/off...oft-access-sql
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  7. #7
    Join Date
    May 2012
    Posts
    10
    Thanks alot for the help. With your assistance I ended up working it out. I have posted a simplified version of the code for anybody that it may assist in the future.
    Essentially the process I have used is:
    1) Have a table whose purpose is to temporarily hold data in my database "tblTemp"
    2) Clear the data from the table "tblTemp" using a delete query
    3) Run append query "qryTemp" to move data to "tblTemp"
    4) Run append query "qryResults" to my results table "tblResults"
    5) Loop Steps 2 to 4 until all required data is in results table

    Code:
    Option Compare Database
    
    Sub Help()
    Dim strSQLA As String
    Dim strSQLDelete As String, strSQLAppend As String
    Dim RCount As Integer
    Dim dbs As DAO.Database
    
    
    Set dbs = CurrentDb
    
    
    For RCount = 1 To 10
    
    
    'Thanks Welshgasman & ssanfu for the help
    
    
    'Query SQL String for Deleting Data in Temporary Table
    strSQLDelete = "DELETE tblTemp.* FROM tblTemp;"
    
    
    'Run Delete Query
    dbs.Execute strSQLDelete, dbFailOnError
    
    
    'Query SQL String for Appending Data into Temporary Table NOTE that source table chnages on every loop P1>P2>P3...>P10 until end of loop
    strSQLAppend = "INSERT INTO tblTemp ( [Field1], [Field2] ) " & _
    "SELECT P" & RCount & ".FieldA, P" & RCount & ".FieldB " & _
    "FROM P" & RCount & ";"
    
    
    'Run Append Query
    dbs.Execute strSQLAppend, dbFailOnError
    
    
    'Query SQL String for Appending from Temporary Table to Results Table
    strSQLAppend = "INSERT INTO tblResults ( [Field1], [Field2] ) " & _
    "SELECT tblTemp.Field1, tblTemp.Field2 " & _
    "FROM tblTemp;"
    
    
    'Run Append Query
    dbs.Execute strSQLAppend, dbFailOnError
    
    
    
    
    Next RCount
    
    
    
    
    End Sub

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,936
    Well I'm glad you got a solution, but normally a temporary table has to be used to clean the data.? You are merely putting the data from source to temp to results?

    Cut out the middleman and just take from source direct to results?
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    Join Date
    May 2012
    Posts
    10
    The solution is greatly simplified.
    The results require a couple of levels of calculations that require queries before going into the results table. The queries are also a bit long and to keep my sanity I broke a couple of the queries up that probably could have been combined. My computer complains less than my brain so it is working out so far.
    There is actually initially 12 tables and 3 calculation queries for each loop I run. The source tables change for each loop. I hold the results of the queries in temp tables before running a final append query. This gets looped 252 times and the tables are immense so it takes about 4 hours to run. So I just let it go overnight.

  10. #10
    Join Date
    May 2012
    Posts
    10
    I am sure you would find ways to make my code 1000% more efficient but as long as it works I am stoked.

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

Similar Threads

  1. ADO Append 4 Excel Sheets of Data to one Recordset
    By philipq in forum Programming
    Replies: 3
    Last Post: 10-26-2014, 09:17 AM
  2. append records to a table from recordset
    By linoreale in forum Access
    Replies: 2
    Last Post: 10-13-2014, 10:40 AM
  3. Replies: 6
    Last Post: 12-03-2013, 11:14 PM
  4. Replies: 2
    Last Post: 03-08-2012, 12:59 PM
  5. Replies: 1
    Last Post: 11-13-2009, 03:03 AM

Tags for this Thread

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