Results 1 to 12 of 12
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    latest date

    All, using Access 2010. I have some vba code to import from a folder a spreadsheet into my database using transferspreadsheet method. Right now the file I am importing is in a folder with no other files. But now; there can be multiple files of the same file with different dates for example:
    Code:
     2014 07 08 testfile
    2014 07 23 testfile
    2014 08 20 testfile
    2014 09 10 testfile
    How do I format my code to look at the files in the folder and import the file with the latest date? Thanks

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    Here's some code to import ALL excel files in a foldler and some to import ALL sheets in the workbook...

    [code]
    'import all files in folder
    '------------------
    Public Sub ImportAllXLFilesAndSheets(ByVal pvDir)
    '------------------
    Dim vFil, vTargT
    Dim i As Integer
    Dim sTbl As String, sSql As String
    Dim tdf As TableDef
    Dim colSheets As New Collection
    Dim vSheet, vQry, oFolder, oFile
    sTbl = "xlFile" 'linked xl file to import
    On Error GoTo errImp
    Set db = CurrentDb
    Select Case True
    Case pvDir = ""
    MsgBox "No source folder given", vbCritical, "Error"

    Case Else
    DoCmd.Hourglass True
    DoCmd.SetWarnings False
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set oFolder = fso.GetFolder(pvDir)
    For Each oFile In oFolder.Files
    If InStr(oFile, ".xls") > 0 Then 'only excel files
    vFil = pvDir & oFile.Name

    'get all the sheets in the workbook
    Set colSheets = getAllSheetsInWb(vFil)
    For Each vSheet In colSheets
    DeleteTbl sTbl 'remove prev xl file

    'ATTACH THE WORKBOOK via sheet
    DoCmd.TransferSpreadsheet acLink, sTbl, vFil, True, vSheet

    'append data
    DoCmd.OpenQuery "qaImportXlFile"
    Next 'sheet
    End If
    Next 'file
    End Select
    Set colSheets = Nothing
    Set oFile = Nothing
    Set oFolder = Nothing
    DoCmd.Hourglass False
    DoCmd.SetWarnings True
    Exit Sub
    errImp:
    MsgBox Err.Description, vbCritical, "ImportAllXLFilesAndSheets()" & Err
    Exit Sub
    Resume
    End Sub
    'remove the table from db
    '------------------
    Private Function DeleteTbl(ByVal pvTbl)
    '------------------
    Dim db As Database
    On Error Resume Next
    Set db = CurrentDb
    db.TableDefs.Delete pvTbl
    db.TableDefs.Refresh
    Set db = Nothing
    End Function
    'get all sheets in the workbook
    '------------------
    Private Function getAllSheetsInWb(ByVal pvFile) As Collection
    '------------------
    Dim xl As excel.Application
    Dim sSht As String
    Dim colSheets As New Collection
    Set xl = CreateObject("excel.application")
    With xl
    .Visible = True
    .Workbooks.Open pvFile
    For Each sSht In .Sheets
    colSheets.Add sSht, sSht
    Next
    .ActiveWorkbook.Close False
    .Quit
    End With
    Set xl = Nothing
    Set getAllSheetsInWb = colSheets
    End Function
    [/ode]

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I appreciate the response and code. But I need only the file with the latest date imported???

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    oops, I thought is was all..
    in the code that scans ALL files, check the date as you go, save the max date , then at the end, open THAT FILE.

    usage: vDate = getFileProperty(vFile, 5) 'get the date property of the file
    if vDate > vMax then
    vMaxFile = vFile
    endif

    Public Function getFileProperty(ByVal pvFile, ByVal pvAttrib)
    'PROPERTIES VALUES
    '5 date stamp
    ' 9 Author
    '10 Title Author
    '11 Subject Title
    Dim vDir, f
    Dim vDat
    getDirName pvFile, vDir, f
    sFileName = f
    sFolderPathspec = vDir
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.Namespace(sFolderPathspec)
    vDat = objFolder.GetDetailsOf(objFolder.Parsename(sFileNa me), pvAttrib)

    getFileProperty = vDat
    Set fs = Nothing
    Set f = Nothing
    End Function

  5. #5
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I'm so sorry Ranman; I can't decifer your code to incorporate it into my database. Can you walk me through where to put where? Thanks

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Here is an example picking a folder then checking for the latest date in the file name.
    The file name must begin with: yyyy mm dd where
    yyyy is the 4 digit year
    mm is the 2 digit month and
    dd is the 2 digit day.

    There must be a space between each of the date parts.
    "2014 09 01 test.xlsx" <= good
    "20140901 test.xlsx" <= bad
    "2014 9 1 test.xlsx" <= bad
    "2014 9 11 test.xlsx" <= bad

  7. #7
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Ok Steve. My brain is working a little better than last night. I'm using this code behind a cmd button:
    Code:
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
     "tbl_Empl", "C:\folder\Empl.xlsx", True
    How do I modifiy this code to reference the sfile?? Do I replace the "C:\folder\Empl.xlsx" with form1? I just don't know the correct way. Thanks
    Last edited by slimjen; 09-12-2014 at 06:24 AM.

  8. #8
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    This is what I came up with start: I keep the form1 open then behind my import cmd button:

    Code:
    Dim strFile As String
    strFile = Me.Form.form1
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
        "tbl_temp1", strFile, True
    I get error: Application Defined or object defined error

  9. #9
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Unless you changed my code, "strFile" should be "sFile". ("s" not "str")

    I would have to know what your process (code) currently is to import a file..

    Attached is another example (quit rolling your eyes! )

    Put the Excel files in a folder (remember which folder) and put the dB in a trusted location.
    Open the dB.
    Follow the steps

    Look at/trace the code.

  10. #10
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Ha Ha. Thanks

  11. #11
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    ok. this works great. Is there anyway to get the folder hardcoded instead of asking the user to browse for the folder? I really appreciate all you help.

  12. #12
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Ahh. I got it. Again; thanks for all your help!!

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

Similar Threads

  1. Value of latest date
    By v!ctor in forum Queries
    Replies: 3
    Last Post: 02-27-2013, 03:48 PM
  2. Default earliest date and latest
    By Compufreak in forum Access
    Replies: 3
    Last Post: 01-10-2013, 07:17 AM
  3. ComboBox always contain latest Monday date
    By tylerg11 in forum Forms
    Replies: 3
    Last Post: 08-30-2012, 01:33 PM
  4. How do you get the latest date?
    By radicrains in forum Queries
    Replies: 6
    Last Post: 10-27-2010, 08:59 PM
  5. Latest Date Records
    By Rick West in forum Queries
    Replies: 1
    Last Post: 09-25-2009, 11:16 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