Link to home
Start Free TrialLog in
Avatar of anthonytr
anthonytrFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Append from one table to another

Hi,

I have the following code which imports an excel file into a temp table in my application.  I am trying to work out how I can then append/insert that data into my live table with an additional field value for each record being inserted.

Dim dbs As DAO.Database
Dim td As DAO.TableDef
Dim fd As Office.FileDialog
Dim BatchID As string

   ' set dbs to current database
   Set dbs = CurrentDb
   
   ' loop through all tables in current database
   For Each td In dbs.TableDefs
   ' check if Temp table is still in current database
      If td.Name = "DLPTemp" Then
        ' it is, so drop it
         dbs.Execute "Drop Table DLPTemp;"
      End If
   Next
    


            Set fd = Application.FileDialog(msoFileDialogFilePicker)

   With fd

      .AllowMultiSelect = False

      ' Set the title of the dialog box.
      .Title = "Please select the file to impot."

      ' Clear out the current filters, and add our own.
      .Filters.Clear
      .Filters.Add "Excel 2003", "*.xls"
      .Filters.Add "All Files", "*.*"

      ' Show the dialog box. If the .Show method returns True, the
      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
      If .Show = True Then
        txtFileName = .SelectedItems(1) 'replace txtFileName with your textbox

      End If
   End With
    
    
   ' import table
      DoCmd.TransferSpreadsheet acImport, , "DLPTemp", txtFileName, True
   
   ' execute the insert SQL from temp table to Export

BatchID = me.Batch_ID
   
   dbs.Execute "INSERT INTO dbo_tbl_reference_data ([Batch_ID], [Reference1], [Reference2], [Reference3], [Reference4], [Reference5], [Reference6]"
   
' THIS IS MY ATTEMPT TO INSERT THE DATA INTO MY LIVE TABLE
'The Batch_ID field is the only field which is not present in the Excel document or temp table.  For each record being inserted into tbl_reference_data i need to assign the Batch_ID.  
  
   ' drop table
   'dbs.Execute "Drop Table DLPTemp;"
   
  ' release objects
   
   Set dbs = Nothing
   Set td = Nothing

End Sub

Open in new window

Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

How do you assign the Batch ID? Is there some sort of logic involved in doing so, or is it just a random number/

If it's a Random number, just set your tables Batch_ID Column to AutoNumber, and Access will take care of it. You'd need to remove it from the INSERT string, of course.
Avatar of anthonytr

ASKER

Hi,

The batch_id has already been created by the time the user clicks the import button and is available on the form (although hidden).  So I need to import all the columns from the temp table with the addition of the batch_id.

The batch_id is just the FK not PK.

Anthony
ASKER CERTIFIED SOLUTION
Avatar of PatHartman
PatHartman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you PatHartman,

This answer has really helped.   I agree with you and I do prefer to know how to achieve something and actually know the reason behind a solution.

I do occasionally use QBE to create a query and then look at the SQL syntax to work out how to embed it into code.  I seem to be over thinking this problem.  

I didn’t know of querydefs in ms access and using them in code as well.  That is something I will use again.  Your explanation was very helpful indeed.  thank you!

Anthony
You are very welcome.  Please don't forget to close the question.

Also - I noticed a typo

set qd = qMyAppendQuery

should be

set qd = db!qMyAppendQuery
Just thought, if I am only linking to the excel spreadsheet, does it matter that this document could be called something different each time the import runs?
Sorry, I think I’m being a little dim here.  When you say “save it as a querydef” how is this achieved?
When you create a query using the QBE and save it, what you have saved is a querydef.  That is what saved queries are called.  The first time Access runs a saved querydef, it calculates an execution plan for how the query will do what it needs to do.  Then every other time the querydef is executed, Access uses the saved execution plan.  When you use embedded SQL, Access doesn't have a saved querydef to hold the execution plan it needs so it recalculates the execution plan each time the embedded SQL runs.  Calculating the plan is pretty quick and current versions of Access don't bloat when doing it so unless you are going to run a particular query multiple times each time the app opens, it probably won't make a whole lot of difference in application speed but it could.

When I link to spreadsheets whose actual name will be different each time I link (very typical), I standardize the name of the linked table.  Since my queries refer to the "table" name rather than the name of the actual spreadsheet, they will be fine even if you change the name of the physical spreadsheet when you link.

So, the workbook might be named C:\folder1\folder2\Invoice_20171231.xlsx and you might link that as tblInvoiceSheet_Link  Then in February, the file name might be C:\folder1\folder2\Invoice_2018_0131.xlsx but the "table" name will still be tblInvoiceSheet_Link so all your queries will still wor,k.