Private Sub btnExport_Click()
On Error GoTo ErrHandler
'Variable Definitions
Dim qryExport As String
Dim exportFileName As String, exportFilePath As String, exportFile As String
Dim templateFileName As String, templateFilePath As String, templatefile As String
Dim listFileName As String, listFilePath As String, listFile As String
Dim listArchivePath As String, listArchiveName As String, listArchive As String
Dim ShtName As String
Dim ProcCheck As Long
Dim fso As Object
Dim FileDate As String
ProcCheck = 0 'Each positive check increments
'Query Export Information
qryExport = "qryAvailability"
exportFileName = "qryAvailability"
exportFilePath = "**REDACTED**"
exportFile = exportFilePath & exportFileName
'Template Information
templateFileName = "UATemplate.xlsm"
templateFilePath = exportFilePath
templatefile = templateFilePath & templateFileName
'List Information
ShtName = "UNIT AVAILABILITY LIST"
listFileName = "UNIT AVAILABILITY LIST.xlsx"
listFilePath = "**REDACTED**"
listFile = listFilePath & listFileName
'Archive Information
listArchivePath = "**REDACTED**"
FileDate = Format(Now, "yyyy-mm-dd hh-mm-ss")
listArchiveName = Left(listFileName, Len(listFileName) - 5) & " " & FileDate & ".xlsx"
listArchive = listArchivePath & listArchiveName
If FileExists(exportFile) = False Then 'If export file already exists, query export will fail.
ProcCheck = ProcCheck + 1
Else
Dim answer As Boolean
answer = MsgBox("The exported file already exists. Do you want to delete it?" _
, vbQuestion + vbYesNo + vbDefaultButton2 _
, "WARNING: EXPORT FILE ALREADY EXISTS")
If answer = vbNo Then
MsgBox ("Ok. Aborting Export.")
Else
'Delete old export file if it exists
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile exportFile
Set fso = Nothing
ProcCheck = ProcCheck + 1
End If
End If
'Check If Current List Open
If IsFileOpen(listFile) = True Then
MsgBox ("Cannot export. Current list is in use.")
Else
ProcCheck = ProcCheck + 1
End If
If ProcCheck = 2 Then
'Rename old list and move to archive
'This step is first to minimize time between checking if this file is open and attempting to delete it
If FileExists(listFile) Then
Call FileCopy(listFilePath, listFileName, listArchiveName, listArchivePath)
If FileExists(listArchive) Then
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile listFile
Set fso = Nothing
End If
End If
'Export the query to an excel spreadsheet
Call ExportQuery(qryExport, exportFile, exportFileName)
'Copy the template file
Call FileCopy(templateFilePath, templateFileName, listFileName)
'Transfer data from export to new copy of the template
Call FormatList(exportFile, templatefile, ShtName)
'Delete the generated export file
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile exportFile
Set fso = Nothing
'Copy New List to List Location
Call FileCopy(templateFilePath, listFileName, listFileName, listFilePath)
'Acknowledgment Message
MsgBox ("Export Successful!")
End If
ExitHandler:
'needed code
Exit Sub
ErrHandler:
Call ErrProcessor
Resume ExitHandler
End Sub