jonathanchye
Registered User.
- Local time
- Today, 15:55
- Joined
- Mar 8, 2011
- Messages
- 448
Hi all,
I am using the following code to update my FrontEnds. It seems to be working fine most of the time but I find that for larger FEs sometimes it will fail - thus creating a lot of backups of the FE on user's desktop.
As I understand it, this code will check for the source file and copy the source over the existing if the source file exists. Since this code sits in my FE form's On Load event won't it overwrite the currently opened file? I am thinking of deleting the destination file first but I don't think that will work as I have the file opened?
I suspect sometimes the FE gets corrupted while updating because the code completes before the new FE actually gets copied over. I've so far added a code to pause for 5 seconds after apiCopy but not sure if that will help.
I am using the following code to update my FrontEnds. It seems to be working fine most of the time but I find that for larger FEs sometimes it will fail - thus creating a lot of backups of the FE on user's desktop.
As I understand it, this code will check for the source file and copy the source over the existing if the source file exists. Since this code sits in my FE form's On Load event won't it overwrite the currently opened file? I am thinking of deleting the destination file first but I don't think that will work as I have the file opened?
I suspect sometimes the FE gets corrupted while updating because the code completes before the new FE actually gets copied over. I've so far added a code to pause for 5 seconds after apiCopy but not sure if that will help.
Code:
Option Compare Database
Option Explicit
Declare Function apiCopyFile Lib "KERNEL32" Alias "CopyFileA" _
(ByVal lpExistingFileName As String, _
ByVal lpNewFileName As String, _
ByVal bFailIfExists As Long) As Long Public Function UpdateFEVersion()
On Error GoTo ProcError
Dim strSourceFile As String
Dim strDestFile As String
Dim strAccessExePath As String
Dim lngResult As Long
'Create the source's path and file name.
strSourceFile = "\\server\share\YourFEDatabase.mde"
strDestFile = CurrentProject.FullName
'Determine path of current Access executable.
strAccessExePath = SysCmd(acSysCmdAccessDir) & "MSAccess.exe "
If Dir(strSourceFile) = "" Then 'Something is wrong and
' the file is not there.
MsgBox "The file:" & vbCrLf & Chr(34) & strSourceFile & _
Chr(34) & vbCrLf & vbCrLf & _
"is not a valid file name. Please see your Administrator.", _
vbCritical, "Error Updating To New Version..."
GoTo ExitProc
Else 'copy the new version of app over the existing one.
lngResult = apiCopyFile(strSourceFile, strDestFile, False)
End If
'Modify strDestFile slightly so that it can be used
' with the Shell function.
strDestFile = """" & strDestFile & """"
MsgBox "Application Updated. Please wait while the application" & _
" restarts.", vbInformation, "Update Successful"
'Load new version, then close old one.
Shell strAccessExePath & strDestFile & "", vbMaximizedFocus
DoCmd.Quit
ExitProc:
Exit Function
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, , _
"Error in UpdateFEVersion event procedure..."
Resume ExitProc
End Function