Results 1 to 3 of 3
  1. #1
    MTSPEER is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jun 2013
    Posts
    283

    Imorting file with most recent date

    Hello,



    I'm trying to select the most recent file to import into my table with the most recent date. There will be other files in the folder. The code I have here will import the file, but it has to be named the same. I'm just looking to retrieve the file with the most recent date. Any suggestions? Thanks


    1. Dim objFile As File
    2. Dim objFolder As Folder
    3. Dim objFSO As FileSystemObject
    4. Dim FolderToScan As String
    5. Dim NewestFile As String
    6. Dim NewestDate As Date
    7. Set objFSO = CreateObject("Scripting.FileSystemObject")
    8. FolderToScan = "\\10.3.0.144\RSH_Log\DATA"
    9. Set objFolder = objFSO.GetFolder(FolderToScan)
    10. NewestFile = ""
    11. NewestDate = #1/1/1970#
    12. For Each objFile In objFolder.Files
    13. If objFile.DateLastModified > NewestDate Then
    14. NewestDate = objFile.DateLastModified
    15. NewestFile = objFile.Name
    16. End If
    17. Next
    18. CurrentDb().Execute "DELETE * FROM tImport", dbFailOnError
    19. DoCmd.TransferText acImportDelim, "tImport Specification", "tImport", _
    20. "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt", False
    21. MsgBox "Data has been imported into the tImport table"

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,772
    Has to be same name every time because of the import specification? Why is that an issue? Name the file whatever you want.
    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.

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I'm trying to select the most recent file to import into my table with the most recent date
    What type of file? Text, CSV, Excel,....???

    There will be other files in the folder.
    What types of files? Same file type? Different file types? You might need another "IF...END IF" to check for the file extension before you check the file date.

    You must be more specific with your question.


    I am confused. You use the FSO to search the folder "\\10.3.0.144\RSH_Log\DATA", but you are importing from a different folder in the DoCmd.TransferText command: "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt"

    Usually you import the file from the same folder you search in.

    If the folder has ONLY text files and you want to import the latest file, concatenate the file name to the path:
    Code:
    Public Sub ImportFile()
        Dim objFile As File
        Dim objFolder As Folder
        Dim objFSO As FileSystemObject
        Dim FolderToScan As String
        Dim NewestFile As String
        Dim NewestDate As Date
    
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        FolderToScan = "\\10.3.0.144\RSH_Log\DATA"
        Set objFolder = objFSO.GetFolder(FolderToScan)
        NewestFile = ""
        NewestDate = #1/1/1970#
        For Each objFile In objFolder.Files
            If objFile.DateLastModified > NewestDate Then
                NewestDate = objFile.DateLastModified
                NewestFile = objFile.Name
            End If
        Next
        CurrentDb.Execute "DELETE * FROM tImport", dbFailOnError
    
        '--------------------------------------------
        '     testing - is the path and file name correct???
        MsgBox "The path and file name to be imported is " & vbNewLine & vbNewLine & FolderToScan & "\" & NewestFile
        '     Delete or comment out when testing is complete
        '--------------------------------------------
    
        '    DoCmd.TransferText acImportDelim, "tImport Specification", "tImport", "\\10.3.0.144\RSH_Log\DATA\4300R TEST DATA PLOTS\import.txt", False
        DoCmd.TransferText acImportDelim, "tImport Specification", "tImport", FolderToScan & "\" & NewestFile, False
        MsgBox "Data has been imported into the tImport table"
    
    
       Set objFSO = Nothing
    
    End Sub

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

Similar Threads

  1. Replies: 3
    Last Post: 03-11-2014, 07:32 PM
  2. Most recent Date query
    By SmartestIdiot in forum Queries
    Replies: 1
    Last Post: 01-11-2014, 07:56 AM
  3. Choose the row with the MOST RECENT date
    By taimysho0 in forum Queries
    Replies: 1
    Last Post: 05-14-2012, 02:35 PM
  4. Display Most Recent Date
    By jsimard in forum Queries
    Replies: 2
    Last Post: 06-23-2011, 02:44 PM
  5. Replies: 3
    Last Post: 05-11-2010, 02:31 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