Results 1 to 4 of 4
  1. #1
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,366

    Open yesterdays file

    Hi Guys, i am wanting open yesterdays file if today's file is not yet in the folder, the following debugs if it is trying to open yesterdays (yDate) file but opens fine when it is today's file (tDate)

    If Me.cboFileOptions = "Open Daily Update" Then


    strPathName = "T:\Shared\XL Files\Daily Update"
    tDate = Forms!frmMainMenu!txtUpdate
    yDate = DateAdd("d", -1, tDate)
    If Dir(strFilePath & FileName) <> "" Then
    FileName = "Update For" & " " & Format(tDate, "dd-mm-yy") & ".xlsx"
    Else
    FileName = "Update For" & " " & Format(yDate, "dd-mm-yy") & ".xlsx"
    End If
    PathToOpen = strPathName
    FileToOpen = FileName
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    xlApp.Workbooks.Open PathToOpen & FileToOpen, True, False
    End If

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Filename has not been defined yet here:
    If Dir(strFilePath & Filename) <> "" Then

    you cant test before it has a value.
    (and the foldername)
    Maybe:
    Code:
    If Me.cboFileOptions = "Open Daily Update" Then
        strPathName = "T:\Shared\XL Files\Daily Update\"
        tDate = Forms!frmMainMenu!txtUpdate
        yDate = DateAdd("d", -1, tDate)
        
        Filename = "Update For" & " " & Format(yDate, "dd-mm-yy") & ".xlsx"
        If Dir(strFilePath & Filename) = "" Then Filename = "Update For" & " " & Format(tDate, "dd-mm-yy") & ".xlsx"
        
        PathToOpen = strPathName
        FileToOpen = Filename
        Set xlApp = CreateObject("Excel.Application")
        xlApp.Visible = True
        xlApp.Workbooks.Open PathToOpen & FileToOpen, True, False
    End If

  3. #3
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    or:
    If FileExists(filename)

    Code:
    Public Function FileExists(ByVal pvFile) As Boolean
    Dim FSO
    Set FSO = CreateObject("Scripting.FileSystemObject")
    FileExists = FSO.FileExists(pvFile)
    Set FSO = Nothing
    End Function

  4. #4
    accesstos's Avatar
    accesstos is offline Expert
    Windows XP Access 2007
    Join Date
    Dec 2018
    Location
    Greece
    Posts
    551
    I would do it in this way:
    Code:
    '--[...]-----------------------------------------------------------------------------------------------------
        strFileName = Dir(strPathName & "UpdateFor_" & Format(Nz(Forms!frmMainMenu!txtUpdate, Date), "yyyymmdd"))
        i = 0
        Do While (Len(strFileName) = 0) And i < 6
            i = i + 1
            strFileName = Dir(strPathName & "UpdateFor_" & Format(Date - i, "yyyymmdd"))
        Loop
        If Len(strFileName) Then
            Set xlApp = CreateObject("Excel.Application")
            xlApp.Visible = True
            xlApp.Workbooks.Open PathToOpen & FileToOpen, True, False
        Else
            MsgBox "No updated files found!", vbExclamation
        End If
    '--[...]-----------------------------------------------------------------------------------------------------
    It looking for last updated file in the last week.
    I prefer the "yyyymmdd" format as suffix at (non spaces) file names.

    Best regards,

    John

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

Similar Threads

  1. Replies: 6
    Last Post: 09-14-2017, 07:31 AM
  2. Replies: 1
    Last Post: 06-18-2015, 12:50 PM
  3. Replies: 1
    Last Post: 11-28-2014, 06:56 PM
  4. Replies: 2
    Last Post: 06-25-2014, 10:29 PM
  5. Replies: 1
    Last Post: 09-27-2010, 10:10 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