Insert or Update excel data to Access table via VBA

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sophannaly
    New Member
    • Mar 2014
    • 67

    Insert or Update excel data to Access table via VBA

    Hi,

    I would like to insert data from excel to access table. This is the code that I use to write data from excel to access
    Code:
    DoCmd.TransferSpreadsheet acImport, , tableName, filePath, True, range:="Sheet1!A:" & endCol
    But how to update if record already exist in access table, and if it is not exist, then insert directly?

    Best regards,
    Sophanna
  • jimatqsi
    Moderator Top Contributor
    • Oct 2006
    • 1293

    #2
    Sophanna,
    By changing 'acImport' in that code to 'acLink' (and providing a new, temporary table name) you will add the Excel sheet to your DB, and it will look like a table. You can run a query against it just like a table. So link the spreadsheet, write an update query (or write some VB code to accomplish same) and you're done.

    Jim

    Comment

    • sophannaly
      New Member
      • Mar 2014
      • 67

      #3
      Hi Jim,

      Thanks you so much for reply. So to do this, after importing this spreadsheet to temporary table with code DoCmd.TransferS preadsheet, then the next line code is a query about updating data from this temp table to existing table.

      Is it what solve this issue?

      Best regards,
      Sophanna

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32653

        #4
        The steps are the right ones Sophanna. Clearly, you must do each step correctly, but if you do that then it should work.

        Comment

        • sophannaly
          New Member
          • Mar 2014
          • 67

          #5
          @NeoPa, @Jim thanks you guys so much for reply. I will give a try and will come back if I have problem.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32653

            #6
            Let us know if it works too Sophanna, then I can make sure Jim's post is flagged as Best Answer.

            Comment

            Working...