I have an application that checks if there is a new version available. If so, I launch another application (using Shell) to copy the new exe file from a Network drive down to the workstation.
I first terminate the original application from within the second application before i "Delete" the exe file and then copy the the file from the Network (the new exe file) onto the workstation. After copying, I re-launch the first application (using Shell again) and then terminate the second application.
Below is the code that I have and it works only if I have the MsgBox. Without it, it throws an Error 70 - Permission Denied error. I was thinking that without the MsgBox, the thread might not have terminated just yet and that the program is still in memory. That's why the exe file may be locked and thus giving the Permission Denied error. I've tried using Sleep to delay the deletion process to no avail.
Does anybody know the reason why this is happening? I can live with what I have right now but I would like to know how this MsgBox can affect the deletion process.
Thanks a lot!
Dim fso As New FileSystemObject
Dim fu As File, fi As File
Dim a As Integer
Dim iPath As String
On Error GoTo Err_Handler
'get update exe & installed exe
Set fu = fso.GetFile(g_ini_UNLAB_Update & "\" & PROGRAM_NAME)
Set fi = fso.GetFile(App.Path & "\" & PROGRAM_NAME)
'wait 1 second then copy new version of the application
SetMsg "Please wait... Copying updated version of the Application."
iPath = fi.Path ': Set fi = Nothing 'save installed path to variable
'without the msgbox below, this code will throw an error 70 - Permission Denied
a = MsgBox("Application about to be upgraded! Continue?", vbQuestion + vbYesNo, Me.Caption)
If a = vbNo Then Shell iPath & " 0", vbNormalFocus: GoTo Terminate
'Kill iPath 'delete old version
fu.Copy iPath 'copy new version
SetMsg "Latest version of the application installed."
SetMsg "Please wait... relaunching latest version of the application."
'launch a new thread of latest application version
Shell iPath, vbNormalFocus
'destroy file system objects
Set fso = Nothing: Set fu = Nothing: Set fi = Nothing
'terminate this program
MsgBox Err.Number & " - " & Err.Description & " - " & Err.Source