on exit from itself - see below - I've left in a lot of irrelevant code in case it isn't !!(Irrelevant that is)
Private Sub Form_Open(Cancel As Integer)
Set myDB = CurrentDb()
rstStatsOpen = False
rstSuppOpen = False
Set rstTemp = myDB.OpenRecordset("Locations")
myErrorEmail = rstTemp!ErrorEmail
' validate error path
myErrPath = Trim(rstTemp!ErrorPath)
If Right(myErrPath, 1) <> "\" Then myErrPath = myErrPath & "\"
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(myErrPath) = False Then
MsgBox "Error File Folder " & myErrPath & " is missing", vbOKOnly, "Disaster-Conversion cancelled"
Forms![mainmenu]!ErrorNotFound = True
GoTo Convert_exit
Else
' folder there
If Forms![mainmenu]!ErrorNotFound = True Then ' was missing before
myMsg = "Error File Folder " & myFpath & " is now available, Conversion resumed"
myMsgType = "Info"
OpErrEmail
Forms![mainmenu]!ErrorNotFound = False
End If
End If
' validate log file
myLogFile = rstTemp!LogFile
myErrorEmail = rstTemp!ErrorEmail
rstTemp.close
Set rstTemp = Nothing
Set fs = CreateObject("Scripting.FileSystemObject")
myTemp = GetFname(myLogFile) ' to get just path
If fs.FolderExists(myFpath) = False Then
If Forms![mainmenu]!LogNotFound = False Then ' 1st time
myMsg = "Log File Folder " & myFpath & " is missing, Conversion cancelled"
myMsgType = "Fatal"
OpErrEmail
Forms![mainmenu]!LogNotFound = True
End If
GoTo Convert_exit
' but come back in later and auto check
Else 'folder there
If Forms![mainmenu]!LogNotFound = True Then ' was missing before
myMsg = "Log File Folder " & myFpath & " is now available, Conversion resumed"
myMsgType = "Info"
OpErrEmail
Forms![mainmenu]!LogNotFound = False
End If
Open myLogFile For Append As #3
End If
' clear last run and set up new one
DoCmd.SetWarnings False
mySQL = "DELETE * FROM LatestStats ;"
DoCmd.RunSQL mySQL
Set rstStats = myDB.OpenRecordset("LatestStats")
rstStatsOpen = True
Set rstSuppliers = myDB.OpenRecordset("SupplierDetails")
rstSuppOpen = True
'loop all SupplierNames in SupplierDetails
rstSuppliers.MoveFirst
Do
' check directories
myIPpath = Trim(rstSuppliers!ImportPath)
If Right(myIPpath, 1) <> "\" Then myIPpath = myIPpath & "\"
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(myIPpath) = False Then
If rstSuppliers!ImportNotFound = False Then ' 1st time
myMsg = "Input Folder " & myIPpath & " for " & rstSuppliers!SupplierName & " is missing, Customer ignored"
myMsgType = "Critical"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!ImportNotFound = True
rstSuppliers.Update
End If
GoTo Convert_NextSupplierName
Else ' folder there
If rstSuppliers!ImportNotFound = True Then ' was missing before
myMsg = "InputFolder " & myIPpath & " for " & rstSuppliers!SupplierName & " is now available, Customer conversion resumed"
myMsgType = "Info"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!ImportNotFound = False
rstSuppliers.Update
End If
End If
myOPpath = Trim(rstSuppliers!ExportPath)
If Right(myOPpath, 1) <> "\" Then myOPpath = myOPpath & "\"
If fs.FolderExists(myOPpath) = False Then
If rstSuppliers!ExportNotFound = False Then ' 1st time
myMsg = "Output Folder " & myOPpath & " for " & rstSuppliers!SupplierName & " is missing, Customer ignored"
myMsgType = "Critical"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!ExportNotFound = True
rstSuppliers.Update
End If
GoTo Convert_NextSupplierName
Else ' folder there
If rstSuppliers!ExportNotFound = True Then ' was missing before
myMsg = "Output Folder " & myOPpath & " for " & rstSuppliers!SupplierName & " is now available, Customer conversion resumed"
myMsgType = "Info"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!ExportNotFound = False
rstSuppliers.Update
End If
End If
myARCHpath = Trim(rstSuppliers!ArchivePath)
If Right(myARCHpath, 1) <> "\" Then myARCHpath = myARCHpath & "\"
If fs.FolderExists(myARCHpath) = False Then
If rstSuppliers!ArchiveNotFound = False Then ' 1st time
myMsg = "Output Copy Folder " & myARCHpath & " for " & rstSuppliers!SupplierName & " is missing, Customer ignored"
myMsgType = "Critical"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!ArchiveNotFound = True
rstSuppliers.Update
End If
GoTo Convert_NextSupplierName
Else ' folder there
If rstSuppliers!ArchiveNotFound = True Then ' was missing before
myMsg = "Output Copy Folder " & myARCHpath & " for " & rstSuppliers!SupplierName & " is now available, Customer conversion resumed"
myMsgType = "Info"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!ArchiveNotFound = False
rstSuppliers.Update
End If
End If
' now check template file exists e.g. C:\Argos\EIE\Templates\Evander.xls
If rstSuppliers![MoveFileOnly?] = True Then ' no template
Else
myTemplateFile = Trim(rstSuppliers!HdgsTemplate)
If Right(myTemplateFile, 4) <> ".xls" Then myTemplateFile = myTemplateFile & ".xls"
myTemp = GetFname(myTemplateFile) ' to get just path
myTemplateDodgy = False
If fs.FolderExists(myFpath) = False Then ' dodgy folder
myTemplateDodgy = True
Else
myFFound = Dir(myTemplateFile) ' this given an error at Argos if dodgy folder so tst folder first
If myFFound = "" Then myTemplateDodgy = True
End If
If myTemplateDodgy = True Then
If rstSuppliers!TemplateNotFound = False Then ' 1st time
myMsg = "Headings Template file " & myTemplateFile & " for " & rstSuppliers!SupplierName & " not found, Client ignored-please correct"
myMsgType = "Critical"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!TemplateNotFound = True
rstSuppliers.Update
End If
GoTo Convert_NextSupplierName
Else ' file there
If rstSuppliers!TemplateNotFound = True Then ' was missing before
myMsg = "Headings Template file " & myTemplateFile & " for " & rstSuppliers!SupplierName & " is now available, Customer conversion resumed"
myMsgType = "Info"
OpErrEmail
rstSuppliers.Edit
rstSuppliers!TemplateNotFound = False
rstSuppliers.Update
End If
End If
myTemplateCR = Trim(rstSuppliers!HdgsTemplateCR)
If Len(myTemplateCR) > 0 Then 'optional
' cant be bothered to have another switch for the credit - will just get loads of emails!
If Right(myTemplateCR, 4) <> ".xls" Then myTemplateCR = myTemplateCR & ".xls"
myFFound = Dir(myTemplateCR)
If myFFound = "" Then
myMsg = "Headings Template Credit file " & myTemplateCR & " for " & rstSuppliers!SupplierName & " not found, Client ignored-please correct"
myMsgType = "Critical"
OpErrEmail
GoTo Convert_NextSupplierName
End If
End If
End If
' check directories end
myDateCell = Trim(rstSuppliers!DateCell)
myPrefix = Trim(rstSuppliers!FilePrefix)
'1st - loop all csv files in folder, store in stats
myIpPattern = myIPpath & myPrefix & "*.csv"
myFFound = Dir(myIpPattern, 1) ' include RO files but not sub directories or hidden or system
If myFFound <> "" Then 'some csvs found
myGoodFiles = 0
' check adr there
myTemp = Dir(myIPpath & myPrefix & ".adr", 1)
If myTemp = "" Then ' cant continue
myMsg = rstSuppliers!FilePrefix & ".adr for " & rstSuppliers!SupplierName & " not found, Client ignored-please correct"
myMsgType = "Critical"
OpErrEmail
GoTo Convert_NextSupplierName
Else
myFFound = Dir(myIpPattern, 1) ' find first one again
Do
myGoodFiles = myGoodFiles + 1
myFullFile = myIPpath & myFFound
'strips path and ext, sets myipext
MyJustfile = GetFname(myFullFile)
With rstStats
.AddNew
!SupplierName = rstSuppliers!SupplierName
!InputFile = MyJustfile & ".csv"
!FullFile = myFullFile
!IPdate = Now()
!OrigDate = FileDateTime(myFullFile) ' Returns "2/12/93 4:35:47".
.Update
End With
myFFound = Dir() 'same as last
Loop While myFFound <> ""
rstSuppliers.Edit
rstSuppliers!LatestFiles = myGoodFiles
rstSuppliers!LatestDate = Now()
rstSuppliers.Update
End If
Else ' no .csv files found
GoTo Convert_NextSupplierName
End If
rstGoodOpen = False
If myGoodFiles = 0 Then GoTo Convert_NextSupplierName
' no named input files found
' now process rststats for this SupplierName in date order
mySQL = "SELECT * FROM LatestStats WHERE SupplierName = '" & rstSuppliers!SupplierName & "' And ignore = False "
mySQL = mySQL & "ORDER BY OrigDate, InputFile; "
Set rstGoodStats = myDB.OpenRecordset(mySQL)
rstGoodOpen = True
Do
' ** set up next file to process
myFullFile = rstGoodStats!FullFile
'strips path and ext
MyJustfile = GetFname(myFullFile)
myIPfile = MyJustfile & ".csv"
myOPfile = MyJustfile & ".xls"
myTempXLSFile = myIPpath & myOPfile
'log it
Print #3, Now() & " Found File " & myIPfile
Me.Repaint ' or requery?
CheckAndProcessFile
rstGoodStats.MoveNext
Loop While Not rstGoodStats.EOF
MoveADR:
' move ip adr to op - at end of Customer not file !!
myTemp = myIPpath & myPrefix & ".adr"
myTemp2 = myOPpath & myPrefix & ".adr"
FileCopy myTemp, myTemp2
Kill myTemp
Convert_NextSupplierName:
Me.Repaint
Me.Recalc
rstSuppliers.MoveNext
If rstSuppliers.EOF Then Exit Do
Loop
Convert_exit:
DoCmd.SetWarnings True
If rstStatsOpen = True Then
rstStats.close
Set rstStats = Nothing
End If
If rstGoodOpen = True Then
rstGoodStats.close
Set rstGoodStats = Nothing
End If
If rstSuppOpen = True Then
rstSuppliers.close
Set rstSuppliers = Nothing
End If
Set fs = Nothing
myDB.close
Set myDB = Nothing
On Error GoTo 0
DoCmd.close ' close this form
Close #3
End Sub