Frothingslosh
Premier Pale Stale Ale
- Local time
- Today, 09:40
- Joined
- Oct 17, 2012
- Messages
- 3,276
We've seen a number of requests lately about automatically distributing database front end files after a new version is released. This is a modified version of code taken elsewhere on the web; I did not write the vast majority of it, only making a couple updates (mainly to keep the code from deleting the front-end if the file is run with no network connection).
Edit: I have been told the original code is by Bob Larson, just to give credit where credit is due.
To make it work, you need:
The way it works is that the tool first checks to see if the master file is the one being executed by comparing the path of the currently running version of your application against the master location from 3 above. If a local copy is being run instead, it then compares version numbers. If they do not match, it writes a batch file that will delete the local copy of the tool, then copy the master copy from the folder at 3 above, and then restart your application.
Word of warning - make absolutely sure your master copy has the same version number as is found in 2, or you will wind up stuck in a delete/update loop.
Anyway, here's the code for the version checker:
And this is the code for the actual update program. It should be in the same module as the version checking program. It has been tightened up slightly from the original, and reliance on module-level declarations has been removed. The batch file has also been cleaned up.
Edit: I have been told the original code is by Bob Larson, just to give credit where credit is due.
To make it work, you need:
- A table in the front end called tbl-fe_version, with a field fe_version_number.
- A table linked into the front end called tbl-version_fe_master, with a field fe_version_number.
- A table linked into the front end called tbl-version_master_location, with a field s_masterlocation.
The way it works is that the tool first checks to see if the master file is the one being executed by comparing the path of the currently running version of your application against the master location from 3 above. If a local copy is being run instead, it then compares version numbers. If they do not match, it writes a batch file that will delete the local copy of the tool, then copy the master copy from the folder at 3 above, and then restart your application.
Word of warning - make absolutely sure your master copy has the same version number as is found in 2, or you will wind up stuck in a delete/update loop.
Anyway, here's the code for the version checker:
Code:
Public Function CheckFrontEnd() As Integer
[COLOR=seagreen]' ************************************************************[/COLOR]
[COLOR=seagreen]' Created by : Scott L Prince[/COLOR]
[COLOR=seagreen]' Parameters : None[/COLOR]
[COLOR=seagreen]' Result : Determines if backend can be reached, and if front end is the current version.[/COLOR]
[COLOR=seagreen]' Returns : 0 - Misc Error[/COLOR]
[COLOR=seagreen]' : 1 - No current version found in Version Manager file[/COLOR]
[COLOR=seagreen]' : 2 - Front end being run from master location[/COLOR]
[COLOR=seagreen]' : 3 - Master file path not found in Version Manager file[/COLOR]
[COLOR=seagreen]' : 999 - Front end current[/COLOR]
[COLOR=seagreen]' Date : 5-30-14[/COLOR]
[COLOR=seagreen]' Remarks : Based on previously-existing code by Bob Larson posted at StackOverflow[/COLOR]
[COLOR=seagreen]' Changes :[/COLOR]
[COLOR=seagreen]' ***************[/COLOR][COLOR=seagreen]*********************************************[/COLOR]
Dim FrontEndVersion As String [COLOR=seagreen]'Front end version number[/COLOR]
Dim MasterVersion As String [COLOR=seagreen]'Master version number[/COLOR]
Dim MasterPath As String [COLOR=seagreen]'Location of the master FE file[/COLOR]
Dim BatchPath As String [COLOR=seagreen]'Location of the batch file that does the actual update[/COLOR]
[COLOR=seagreen]'Determine master version number.[/COLOR]
MasterVersion = DLookup("fe_version_number", "tbl-version_fe_master")
[COLOR=seagreen]'Determine if the database containing the version information can be accessed.[/COLOR]
Select Case MasterVersion
Case "" [COLOR=seagreen]'Master version number cannot be found, or backend/version manager is missing.[/COLOR]
CheckFrontEnd = 1
Case Else [COLOR=seagreen]'Version data found.[/COLOR]
[COLOR=seagreen]'Look up the path for the master file location.[/COLOR]
MasterPath = DLookup("s_masterlocation", "tbl-version_master_location")
[COLOR=seagreen]'Determine if the master file is being run rather than a local copy.[/COLOR]
If Nz(MasterPath, "") = "" Then
[COLOR=seagreen]'No master path was found. Return error value.[/COLOR]
CheckFrontEnd = 3
ElseIf MasterPath = CurrentProject.Path Then
[COLOR=seagreen]'The actual master file is the one being executed.[/COLOR]
CheckFrontEnd = 2
Else
[COLOR=seagreen] 'Master file path found and is not being run. Determine the version number of the active front end.[/COLOR]
FrontEndVersion = DLookup("fe_version_number", "tbl-fe_version")
[COLOR=seagreen] 'Compare the version number in the front end to the master version number.[/COLOR]
Select Case (FrontEndVersion = MasterVersion)
Case True [COLOR=seagreen]'Version numbers match.[/COLOR]
[COLOR=seagreen]'Return "OKAY" result.[/COLOR]
CheckFrontEnd = 999
Case False [COLOR=seagreen]'Version numbers do not match.[/COLOR]
[COLOR=seagreen] 'Create the path for the batch file used to update the front end.[/COLOR]
BatchPath = CurrentProject.Path & "\UpdateDbFE.cmd"
[COLOR=seagreen]'Check for an already-existing BatchPath, and kill it if it exists.[/COLOR]
If Dir(BatchPath) <> "" Then Kill BatchPath
[COLOR=seagreen]'Notify the user that the application will update.[/COLOR]
MsgBox "UPDATE REQUIRED" & vbCrLf & vbCrLf & _
"Your program is not the latest version." & vbCrLf & vbCrLf & _
"The front-end needs to be updated. The program will now close and then should reopen automatically.", _
vbCritical
[COLOR=seagreen] 'Execute 'UpdateFrontEnd'.[/COLOR]
UpdateFrontEnd CurrentProject.Path & "\" & CurrentProject.Name, MasterPath
End Select
End If
End Select
End Function
And this is the code for the actual update program. It should be in the same module as the version checking program. It has been tightened up slightly from the original, and reliance on module-level declarations has been removed. The batch file has also been cleaned up.
Code:
[COLOR=black]Private Sub UpdateFrontEnd(ByVal LocalFilePath As String, _[/COLOR]
[COLOR=black] ByVal MasterFileFolder As String)[/COLOR]
Dim BatchFile As String
Dim MasterFilePath As String
Dim Restart As String
[COLOR=seagreen]'Set the file name and location for the file to copy[/COLOR]
MasterFilePath = MasterFileFolder & "\" & CurrentProject.Name
[COLOR=seagreen]'Set the file name of the batch file to create[/COLOR]
BatchFile = CurrentProject.Path & "\UpdateDbFE.cmd"
[COLOR=seagreen]'Set the restart file name[/COLOR]
Restart = """" & LocalFilePath & """"
[COLOR=seagreen]'Create the batch file[/COLOR]
Open BatchFile For Output As #1
Print #1, "@Echo Off"
Print #1, "ECHO Deleting old file..."
Print #1, ""
Print #1, "ping 127.0.0.1 -n 5 -w 1000 > nul"
Print #1, ""
Print #1, "Del """ & LocalFilePath & """"
Print #1, ""
Print #1, "ECHO Copying new file..."
Print #1, "Copy /Y """ & MasterFilePath & """ """ & LocalFilePath & """"
Print #1, ""
Print #1, "ECHO Starting Microsoft Access..."
Print #1, "START /I " & """MSAccess.exe"" " & Restart
Close #1
[COLOR=seagreen] 'Run the batch file[/COLOR]
Shell BatchFile
[COLOR=seagreen] 'Close the current application so batch file can execute.[/COLOR]
DoCmd.Quit
End Sub
Last edited: