Results 1 to 6 of 6
  1. #1
    BrockWade is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    63

    export subform's datasheet to temp table with VBA

    Thanks in advance for any help. I am trying to export my subform's 'datasheet view' data to 'temp table' (it's a real table and I have code to clear it out to receive new data). I'm trying to loop through the below. This code works fine for exporting the first row of the datasheet, but I need it to do all the rows (between 50 and 100 rows usually display). Can someone coach me along with looping this?:

    Dim strInsertBillingIntoTemp As String

    strInsertBillingIntoTemp = "INSERT INTO Billing_Temp " & _
    "(peopleId, audited,billYear,charge,pastDue," & _
    "totalDue, peopleName, fee ) " & _
    "VALUES( '" & _
    Forms![reviewBilling]![reviewBillingSub].Form![People ID].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![cbxAudited].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![Bill Year].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![Charge].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![Past Due].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![Total Due].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![Name].value & "','" & _
    Forms![reviewBilling]![reviewBillingSub].Form![Fee Type].value & "');"



    DoCmd.RunSQL strInsertBillingIntoTemp

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Far more efficient if feasible would be an SQL statement like:

    INSERT INTO...
    SELECT...
    FROM...
    WHERE...

    That assumes you can select from the subform's source table and apply a criteria that would return the same records displayed on the subform. That would get you all the records in one pass, rather than one-by-one.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    This code is behind main form? I always name subform container different from the object it holds, like ctrDetails.

    Aside from what Paul suggests, your options are to physically loop through the records of the subform (you will see cursor move to each record on the form) or to use the form's RecordsetClone to loop records (user won't see anything).

    Why does code reference cbxAudited and not a field name like the other lines?
    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.

  4. #4
    BrockWade is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Dec 2013
    Posts
    63
    The database was inherited by me so I've not had a chance to convert the existing names of fields to something better and granted the temp table I put together doesn't have the best naming either. The data that feeds the subform is a complex multi-table query that of course creates a one way street so I can't use that to update so I needed a temporary table to hold the current records basically I just need to allow the user to check and uncheck these boxes and because the subform cannot be set to "data entry" that has to be done to a clone of the displayed data placed into a temporary table to allow updates using a separate independent form/subform. So I just need a way to loop through the data displayed and write it to a temp table. Like I said I can use the basic code I have which is working but it only writes the first row to the temp table.

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    If the subform is based on a query, you can use the append query syntax I posted with the query in the FROM clause, and you don't even need a WHERE clause.

    INSERT INTO...
    SELECT...
    FROM QueryName
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,931
    Paul's approach could be feasible. Maybe could use the subform query for an UPDATE or INSERT action.

    CurrentDb.Execute "INSERT INTO Billing_Temp(peopleId, audited, billYear, charge, pastDue, totalDue, peopleName, fee) SELECT [people Id], audited, [bill Year], charge, [past Due], [total Due], [Name], [fee type] FROM queryname"

    CurrentDb.Execute "UPDATE tablename Set somefield=something WHERE ID IN queryname"

    Otherwise, looping RecordsetClone:

    With Me.ctrDetails.Form.RecordsetClone
    While Not .EOF
    ...
    .MoveNext
    Wend
    End With

    Still don't know why your posted code references combobox name instead of field. Referencing the control name complicates.

    Advise no spaces or special characters/punctuation (underscore is exception) in naming convention. Nor reserved words as names - Name is a reserved word.

    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.

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

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2012, 03:15 PM
  2. Replies: 8
    Last Post: 08-26-2012, 11:11 PM
  3. Export to Excel from unbound subform datasheet?
    By dnme in forum Import/Export Data
    Replies: 7
    Last Post: 05-28-2012, 01:50 PM
  4. Replies: 13
    Last Post: 02-26-2012, 08:28 AM
  5. Replies: 2
    Last Post: 12-20-2011, 10: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