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

    Form Open If File Name

    Hi Guy's

    How can i loop through a folder on form open to check file names of all files in the folder, lets say something like:

    34 files called:
    PO-123.pdf
    PO-456.pdf
    PO-112.pdf Job Completed
    PO-324.pdf
    PO-246.pdf Job Completed
    PO-367.pdf
    PO-732.pdf Job Completed


    etc etc..

    If any files are called job completed

    Move these 3 files to back up folder ?

    Thank you

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Something like:
    Code:
    Private Sub Form_Load()
        Dim strFile As String
        strFile = Dir("C:\some path\*.pdf")
        Do While strFile <> ""
            If strFile LIKE "*Completed" Then
                Name strFile As "drive\path" & Mid(strFile, InStrRev(strFile, "\"))
            End If
            strFile = Dir
        Loop
    End Sub

    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
    Join Date
    Jun 2010
    Location
    Belgium
    Posts
    1,043
    Hi,

    to loop through the files in a folder you can use the Dir function:

    Code:
     strFile = Dir(strfolder & "\PO*.pdf")
        Do While Len(strFile) > 0 
            [here you can test on the indication Job completed and move the file if job completed]
            strFile = Dir()
       Loop
    To move/rename the file you can use the Scripting.FileSystemObject. Example strFile = path + name of existing file, strLoc = new path + filename:

    Code:
    Public Function MoveFile(strFile As String, strLoc As String) As Boolean
    On Error GoTo Err_MoveFile
    
    
      Dim fsoImport As Object
      Dim floImportFile As Object
      
      Set fsoImport = CreateObject("Scripting.FileSystemObject")
      Set floImportFile = fsoImport.GetFile(strFile)
      If strFile <> strLoc Then
        floImportFile.Move strLoc
      End If
      MoveFile = True
    
    
    Exit_MoveFile:
        Set fsoImport = Nothing
        Set floImportFile = Nothing
        Exit Function
        
    Err_MoveFile:
        MoveFile = False
        Debug.Print Err.Number & ": " & Err.Description
        Resume Exit_MoveFile
    
    
    End Function

  4. #4
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,191
    Thank you for your replies, all done

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

Similar Threads

  1. Open text file in a form
    By sheusz in forum Forms
    Replies: 15
    Last Post: 10-15-2019, 03:46 AM
  2. Replies: 4
    Last Post: 07-25-2017, 12:35 PM
  3. Open word file off Access form
    By BorisGomel in forum Access
    Replies: 8
    Last Post: 07-12-2012, 12:01 PM
  4. Specified .WAV file not playing on Form Open
    By ANTHROPOIDLUVA in forum Forms
    Replies: 10
    Last Post: 05-31-2012, 11:57 PM
  5. Open pdf file from Access form
    By lios1984 in forum Access
    Replies: 7
    Last Post: 02-21-2012, 01:11 PM

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