Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8929549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:41:38+00:00 2026-06-15T08:41:38+00:00

I have this code that imports a csv file into an Access table. There

  • 0

I have this code that imports a csv file into an Access table.
There are several problems or ideas that are beyond my level of skills.
The first issue is the file does not append existing fields, instead the entire file is inserted create a duplicate including new headers.

The second issue is I would like to export only new or updated records in a csv file out of Access, not the entire table, I have not gotten there yet.

I need a little hand holding here.

Thank you everyone, this forum and you guys are the best. Always helpful and wailing to share your knowledge.

Option Compare Database
Option Explicit

Private Sub ImportFile()
Dim strPathFile As String, strFile As String, strPath As String
Dim strTable As String

Dim xlApp As Object, xlWb As Object, xlWs As Object
Dim lngRow As Long, lngLastRow As Long
Dim rst As DAO.Recordset

strPath = "C:\Transit\"
strTable = "Transit"
strFile = Dir(strPath & "*.csv")

Set rst = CurrentDb.OpenRecordset(strTable)

Do While Len(strFile) > 0

      strPathFile = strPath & strFile

        Set xlApp = CreateObject("Excel.Application")
        Set xlWb = xlApp.Workbooks.Open(strPathFile)
        Set xlWs = xlWb.Worksheets(1)

        lngLastRow = xlWs.UsedRange.Rows.Count

        For lngRow = 1 To lngLastRow

            rst.AddNew
            rst("Field1") = xlWs.Range("A" & lngRow).Value
            rst("Field2") = xlWs.Range("B" & lngRow).Value
            rst("Field3") = xlWs.Range("C" & lngRow).Value
            rst("Field4") = xlWs.Range("D" & lngRow).Value
            rst("Field5") = xlWs.Range("E" & lngRow).Value
            rst("Field6") = xlWs.Range("F" & lngRow).Value
            rst("Field7") = xlWs.Range("G" & lngRow).Value
            rst.Update

        Next

    xlApp.Quit
    Set xlApp = Nothing
    Set xlWb = Nothing
    Set xlWs = Nothing
    strFile = Dir()
Loop

rst.Close
Set rst = Nothing

End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T08:41:40+00:00Added an answer on June 15, 2026 at 8:41 am

    I would suggest creating a linked table to that file. Once you’ve done that, you can treat the linked Excel file like any other table, including running SELECT and action queries. In your case, you could create a simple Append query with the Excel spreadsheet as its destination, and limit it to only those records which haven’t been updated.

    Link external Excel file
    It will also be visible in the Query Designer’s Add Tables dialog box.


    Updated

    If the path to the Excel file is not fixed, you will need a little VBA to automate relinking the file.

    'Defined as a function instead of a Sub
    'Allows calling from an AutoExec macro, or setting as an eventhandler property
    Public Function Relink(path As String)
        Dim dbs As DAO.Database, tdf As DAO.TableDef
        Dim newConnect As String
        Dim connectParts As Variant, part As Variant, keyValuePair As Variant
        Dim key As String, value As String, delimiterPosition As Integer
    
        'Objects created from CurrentDb can only be referenced once; after that they cause an error
        'Therefore we need save a reference to the Database object in a variable
        Set dbs = CurrentDb
    
        'Each table has a corresponding TableDef (table definition) object
        Set tdf = dbs.TableDefs("LinkedSheet")
    
        'TableDef objects have a Connect property, containing a connection string for linked tables
        '(For non-linked tables this will be a zero-length string)
        'A connection string has the form key=value;key1=value1 etc.
        connectParts = Split(tdf.Connect, ";")
        For Each part In connectParts
            keyValuePair = Split(part, "=")
            If keyValuePair(0) = "DATABASE" Then
                keyValuePair(1) = path
            End If
            newConnect = newConnect & ";" & Join(keyValuePair, "=")
        Next
    
        'We need to remove the extra semicolon in the beginning
        newConnect = Mid(newConnect, 2)
    
        'Set the connection string, and save the change by calling RefreshLink
        tdf.Connect = newConnect
        tdf.RefreshLink
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code that reads from XML file. It gets five strings (groupId,
I have a script that generates a csv file using the following code: header('Content-type:
I have a package that imports 7 csv files and loads them into 7
I have a csv file that looks like this NumberofNullAsOfDates 0 I would like
Let's say I have some code (using CherryPy) that looks like this: import cherrypy
I have this code that I've edited: http://pastebin.com/vrqHek6S I've put in comments where I
I have this code that runs but never stops. class A { public static
I have this code that suppose to place the marker by the latlng public
I have this code that where I would normally use one line: if (tableView
I have this code that works in a unit test but doesn't work when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.