[COLOR="Navy"]Sub[/COLOR] CompactACDB()
[COLOR="Navy"]Dim[/COLOR] FSO [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR]
[COLOR="Navy"]Dim[/COLOR] AC [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sTempFilePath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sDatabaseFilePath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="SeaGreen"]'//Access Database to Compact[/COLOR]
sDatabaseFilePath = "C:\myTemp\TestDB5.mdb"
[COLOR="SeaGreen"]'//Rename Database[/COLOR]
[COLOR="Navy"]Set[/COLOR] FSO = CreateObject("Scripting.FileSystemObject")
[COLOR="Navy"]With[/COLOR] FSO
sTempFilePath = .GetTempName
sTempFilePath = Replace(sTempFilePath, "tmp", "mdb")
sTempFilePath = .GetParentFolderName(sDatabaseFilePath) & "\" & sTempFilePath
.MoveFile sDatabaseFilePath, sTempFilePath
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="SeaGreen"]'//Compact Database (compacts back into its original file location)[/COLOR]
[COLOR="Navy"]Set[/COLOR] AC = CreateObject("Access.Application")
AC.DBEngine.CompactDatabase sTempFilePath, sDatabaseFilePath
My_Exit:
[COLOR="SeaGreen"]'//Close Access[/COLOR]
[COLOR="Navy"]If[/COLOR] [COLOR="Navy"]Not[/COLOR] AC [COLOR="Navy"]Is[/COLOR] [COLOR="Navy"]Nothing[/COLOR] [COLOR="Navy"]Then[/COLOR]
AC.Quit
[COLOR="Navy"]Set[/COLOR] AC = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="SeaGreen"]'//If original file was renamed and failed to compact then restore it[/COLOR]
[COLOR="Navy"]If[/COLOR] [COLOR="Navy"]Not[/COLOR] FSO.FileExists(sDatabaseFilePath) [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]If[/COLOR] FSO.FileExists(sTempFilePath) [COLOR="Navy"]Then[/COLOR]
FSO.MoveFile sTempFilePath, sDatabaseFilePath
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
FSO.DeleteFile (sTempFilePath)
[COLOR="Navy"]Set[/COLOR] FSO = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
ErrHandler:
[COLOR="Navy"]Resume[/COLOR] My_Exit
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]