I'm trying to loop through files in folder and perform actions on each file based on what's setup in a special table. This will be an automated process that will download files for each automation based on what i specified, hence this convoluted solution.
The problem is with the first WHILE. It loops through the first file, does what's needed and then stops looping. my 2nd file "xwalks.xlsx" matches criteria for the first IF, but it's not processed there.
here's what's in the table for this ProjectID
Here are the files in the folder
LoadCategoryID explanations are below
1 All Files (download all files, rename, copy, delete from the folder
2 Files With String In The Name (only download files that have a string from above in the file name, rename, copy, delete from folder, and if LoopThroughFile is -1, then I'm running procedure (RunTest, it's commented out. if LoopThroughFile is 0, then the procedure will be run once after everything is done)
3 Listed Files (only download files that have the same name as the field in the screenshot, rename, copy, delete from folder, and if LoopThroughFile is -1, then I'm running procedure (RunTest, it's commented out. if LoopThroughFile is 0, then the procedure will be run once after everything is done)
one project can have all 3 options.
Public Function RunLoadFilesTest()
Application.SetOption "Confirm Action Queries", False
Application.SetOption "Confirm Record Changes", False
Application.SetOption "Confirm Document Deletions", False
Dim MyObj As Object, MySource As Object
Dim Rs1 As DAO.Recordset
Dim VExcelFileName As String, VRenameTo As String, VLoadXLSMFileName As String, NewFileName As String, CurFileName As String
Dim CurLoadFile As Variant
Dim VLoadCategoryID As Long, VLoopThroughFile As Long
Dim MainProjectName As String, UFProjectName As String
Dim RunDate As Date, StartRunTime As Date
Dim ProjectPath As String, FormattedDate As String, ProjectMonthlyPath As String, ProjectNetworkLoadPath As String, ProjectLocalLoadPath As String, ProjectPreviousPath As String
Dim ReportYear As Long, ReportQuarter As Long
ProjectPath = "\\dddd\AutomationResults\" & MainProjectName & "\"
ProjectMonthlyPath = ProjectPath & FormattedDate & "\"
RunDate = Format(DateAdd("q", 1, DateAdd("m", -2, DateSerial(Year(Date), (DatePart("q", Date) * 3) - 3, 1))) - 1, "dd-mmm-yyyy")
ReportYear = Year(RunDate)
ReportQuarter = DatePart("q", RunDate)
FormattedDate = ReportYear & "-Q" & ReportQuarter
ProjectMonthlyPath = ProjectPath & FormattedDate & "\"
ProjectNetworkLoadPath = ProjectPath & "_Load\"
ProjectLocalLoadPath = CurPath & "_Load\"
ProjectPreviousPath = ProjectPath & "_Previous\"
If Len(Dir$(ProjectLocalLoadPath & "*.*")) > 0 Then
Kill ProjectLocalLoadPath & "*.*"
End If
Debug.Print DCount("LoadFileID", "AP_LoadFiles", "LoopThroughFile=-1 And ProjectID=" & CurProjectID)
If DCount("LoadFileID", "AP_LoadFiles", "LoopThroughFile=-1 And ProjectID=" & CurProjectID) > 0 Then
CurLoadFile = Dir(ProjectNetworkLoadPath)
While (CurLoadFile <> "")
CurFileName = CurLoadFile
Set Rs1 = CurrentDb.OpenRecordset("SELECT * FROM AP_LoadFiles WHERE LoopThroughFile=-1 And ProjectID=" & CurProjectID & " Order by LoadFileID Desc")
Do Until Rs1.EOF
VLoadCategoryID = Rs1("LoadCategoryID")
VLoopThroughFile = Rs1("LoopThroughFile")
If Not IsNull(Rs1("RenameTo")) And Rs1("RenameTo") <> "" Then
NewFileName = Rs1("RenameTo")
Else
NewFileName = CurFileName
End If
If (VLoadCategoryID = 2 And CurFileName Like "*" & Rs1("ExcelFileName") & "*") Or (VLoadCategoryID = 3 And CurFileName = Rs1("ExcelFileName")) Or VLoadCategoryID = 1 Then
FileCopy ProjectNetworkLoadPath & CurFileName, ProjectLocalLoadPath & NewFileName
If Not IsNull(Rs1("LoadXLSMFileName")) And Rs1("LoadXLSMFileName") <> "" Then
ExcelApp.Workbooks.Open CurPath & Rs1("LoadXLSMFileName") & ".xlsm", True
ExcelApp.Visible = False
ExcelApp.Quit
End If
FileCopy ProjectNetworkLoadPath & CurFileName, ProjectPreviousPath & FormattedDate & "_" & CurFileName
If Len(Dir$(ProjectNetworkLoadPath & CurFileName)) > 0 Then
Kill ProjectNetworkLoadPath & CurFileName
End If
'RunTest
Debug.Print "2 looping " & CurFileName
If Len(Dir$(ProjectLocalLoadPath & CurFileName)) > 0 Then
Kill ProjectLocalLoadPath & CurFileName
End If
Exit Do
End If
Rs1.MoveNext
Loop
Rs1.Close
Set Rs1 = Nothing
CurLoadFile = Dir
Wend
End If
If DCount("LoadFileID", "AP_LoadFiles", "LoopThroughFile=0 And ProjectID=" & CurProjectID) > 0 Then
CurLoadFile = Dir(ProjectNetworkLoadPath)
While (CurLoadFile <> "")
CurFileName = CurLoadFile
Set Rs1 = CurrentDb.OpenRecordset("SELECT * FROM AP_LoadFiles WHERE LoopThroughFile=0 And ProjectID=" & CurProjectID & " Order by LoadFileID Desc")
Do Until Rs1.EOF
VLoadCategoryID = Rs1("LoadCategoryID")
VLoopThroughFile = Rs1("LoopThroughFile")
If Not IsNull(Rs1("RenameTo")) And Rs1("RenameTo") <> "" Then
NewFileName = Rs1("RenameTo")
Else
NewFileName = CurFileName
End If
If (VLoadCategoryID = 2 And CurFileName Like "*" & Rs1("ExcelFileName") & "*") Or (VLoadCategoryID = 3 And CurFileName = Rs1("ExcelFileName")) Or VLoadCategoryID = 1 Then
FileCopy ProjectNetworkLoadPath & CurFileName, ProjectLocalLoadPath & NewFileName
If Not IsNull(Rs1("LoadXLSMFileName")) And Rs1("LoadXLSMFileName") <> "" Then
ExcelApp.Workbooks.Open CurPath & Rs1("LoadXLSMFileName") & ".xlsm", True
ExcelApp.Visible = False
ExcelApp.Quit
End If
FileCopy ProjectNetworkLoadPath & CurFileName, ProjectPreviousPath & FormattedDate & "_" & CurFileName
End If
Rs1.MoveNext
Loop
Rs1.Close
Set Rs1 = Nothing
CurLoadFile = Dir
Wend
'RunTest
Debug.Print "2 run once after loaded all files"
If Len(Dir$(ProjectLocalLoadPath & CurFileName)) > 0 Then
Kill ProjectLocalLoadPath & CurFileName
End If
End If
If DCount("LoadFileID", "AP_LoadFiles", "ProjectID=" & CurProjectID) = 0 Then
Debug.Print "3 RUNNING For those that had no load instructions"
'RunTest
End If
Application.SetOption "Confirm Action Queries", True
Application.SetOption "Confirm Record Changes", True
Application.SetOption "Confirm Document Deletions", True
End Function