[COLOR="Navy"]Option Explicit[/COLOR]
[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] sBackupPath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sDBPath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sTempPath [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sTemp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] errMsg [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] col [COLOR="Navy"]As[/COLOR] VBA.Collection
[COLOR="Navy"]Dim[/COLOR] i [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]GoTo[/COLOR] ErrHandler:
[COLOR="SeaGreen"]'//Create objects[/COLOR]
[COLOR="Navy"]Set[/COLOR] FSO = CreateObject("Scripting.FileSystemObject")
[COLOR="Navy"]Set[/COLOR] AC = CreateObject("Access.Application")
[COLOR="Navy"]Set[/COLOR] col = [COLOR="Navy"]New[/COLOR] Collection
[COLOR="SeaGreen"]'//A location of a single permanent backup folder[/COLOR]
sBackupPath = "C:\Documents and Settings\All Users\Documents\DB_Backups"
[COLOR="SeaGreen"]'---------------------------------------[/COLOR]
[COLOR="SeaGreen"]'//DBs to compact and repair (1 or more)[/COLOR]
col.Add "C:\myTemp\TestDB5.accdb"
col.Add "C:\myTemp\TestDB6.accdb"
[COLOR="SeaGreen"]'---------------------------------------[/COLOR]
[COLOR="SeaGreen"]'//Iterate DB's to compact and repair each in turn[/COLOR]
[COLOR="Navy"]For[/COLOR] i = 1 [COLOR="Navy"]To[/COLOR] col.Count
sDBPath = col.Item(i)
[COLOR="SeaGreen"]'//Get temp filename[/COLOR]
[COLOR="Navy"]With[/COLOR] FSO
sTempPath = .GetTempName
sTempPath = Replace(sTempPath, "tmp", .GetExtensionName(sDBPath))
sTempPath = .GetParentFolderName(sDBPath) & "\" & sTempPath
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="SeaGreen"]'//Compact Database (compacts back into its original file location)[/COLOR]
FSO.MoveFile sDBPath, sTempPath
AC.DBEngine.CompactDatabase sTempPath, sDBPath
[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(sDBPath) [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]If[/COLOR] FSO.FileExists(sTempPath) [COLOR="Navy"]Then[/COLOR]
FSO.MoveFile sTempPath, sDBPath
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="SeaGreen"]'//Move Temp File to Backup Folder[/COLOR]
sTemp = sBackupPath & "\" & FSO.GetBaseName(sDBPath) & "_" & Format(Now, "yyyymmdd.hhnnss") & "." & FSO.GetExtensionName(sDBPath)
FSO.MoveFile sTempPath, sTemp
[COLOR="SeaGreen"]'//Clean Up Backups older than 30 days[/COLOR]
[COLOR="Navy"]Call[/COLOR] Backups_Delete(FSO, sBackupPath, sDBPath)
[COLOR="SeaGreen"]'//Log activity (this log is in the backup folder)[/COLOR]
[COLOR="Navy"]Call[/COLOR] Write_Log(FSO, sBackupPath, (Now & " [" & sDBPath & "] --> compacted."))
[COLOR="Navy"]Next[/COLOR] i
My_Exit:
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
[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="Navy"]If[/COLOR] [COLOR="Navy"]Not[/COLOR] errMsg = "" [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]If[/COLOR] [COLOR="Navy"]Not[/COLOR] FSO [COLOR="Navy"]Is[/COLOR] [COLOR="Navy"]Nothing[/COLOR] [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]Set[/COLOR] FSO = CreateObject("Scripting.FileSystemObject")
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]Call[/COLOR] Write_Log(FSO, sBackupPath, errMsg)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]Set[/COLOR] FSO = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]Set[/COLOR] col = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
ErrHandler:
[COLOR="SeaGreen"]'//An error will result in an abort of all further compact and repair operations[/COLOR]
errMsg = "Error: " & Err.Number & ": " & Err.Description
[COLOR="Navy"]Resume[/COLOR] My_Exit
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Private[/COLOR] [COLOR="Navy"]Sub[/COLOR] Check_Backup_Folder(ByRef FSO [COLOR="Navy"]As[/COLOR] Object, [COLOR="Navy"]ByRef[/COLOR] sDBPath [COLOR="Navy"]As[/COLOR] String)
[COLOR="SeaGreen"]'//To make sure a subfolder called "DB_Backups" exists in the same folder as the database to be compacted[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sTemp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]With[/COLOR] FSO
sTemp = .GetParentFolderName(sDBPath) & "\DB_Backups"
[COLOR="Navy"]If[/COLOR] [COLOR="Navy"]Not[/COLOR] .FolderExists(sTemp) [COLOR="Navy"]Then[/COLOR]
.CreateFolder (sTemp)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]Private[/COLOR] [COLOR="Navy"]Sub[/COLOR] Backups_Delete(ByRef FSO [COLOR="Navy"]As[/COLOR] Object, _
[COLOR="Navy"]ByRef[/COLOR] sBackupPath [COLOR="Navy"]As[/COLOR] String, [COLOR="Navy"]ByRef[/COLOR] sDBPath [COLOR="Navy"]As[/COLOR] String)
[COLOR="SeaGreen"]'//To delete backups older than 30 days[/COLOR]
[COLOR="SeaGreen"]'//Assumes that DB backups are saved in the form DBName_yyyymmdd.mdb or DBName_yyyymmdd.hhnnss.mdb[/COLOR]
[COLOR="Navy"]Dim[/COLOR] re [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Object[/COLOR]
[COLOR="Navy"]Dim[/COLOR] fldr [COLOR="Navy"]As[/COLOR] Folder
[COLOR="Navy"]Dim[/COLOR] f [COLOR="Navy"]As[/COLOR] File
[COLOR="Navy"]Dim[/COLOR] s [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] d [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Date[/COLOR]
[COLOR="Navy"]Set[/COLOR] re = CreateObject("VBScript.RegExp")
[COLOR="Navy"]With[/COLOR] re
.Pattern = FSO.GetBaseName(sDBPath)
.Pattern = .Pattern & "_\d{8}"
.Global = False
.MultiLine = False
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]With[/COLOR] FSO
[COLOR="Navy"]Set[/COLOR] fldr = .GetFolder(sBackupPath)
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] f [COLOR="Navy"]In[/COLOR] fldr.Files
[COLOR="Navy"]If[/COLOR] re.Test(f.Name) [COLOR="Navy"]Then[/COLOR]
s = re.Execute(f.Name)(0)
d = DateSerial(Left(Right(s, 8), 4), Left(Right(s, 4), 2), Right(s, 2))
[COLOR="Navy"]If[/COLOR] d < (Date - 30) [COLOR="Navy"]Then[/COLOR]
f.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]Next[/COLOR] f
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]Set[/COLOR] re = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="SeaGreen"]'_______________________[/COLOR]
[COLOR="Navy"]Sub[/COLOR] Write_Log(ByRef FSO [COLOR="Navy"]As[/COLOR] Object, [COLOR="Navy"]ByRef[/COLOR] sBackupPath [COLOR="Navy"]As[/COLOR] String, [COLOR="Navy"]ByRef[/COLOR] arg [COLOR="Navy"]As[/COLOR] String)
[COLOR="SeaGreen"]'//Records compact/repair activity and any errors[/COLOR]
[COLOR="SeaGreen"]'//Log file is assumed to be in the permanent backup folder with the backups and will be created if missing[/COLOR]
[COLOR="SeaGreen"]'//Self deletes if it grows to 8Mb in size[/COLOR]
[COLOR="Navy"]Dim[/COLOR] sTemp [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]String[/COLOR]
[COLOR="Navy"]Dim[/COLOR] f [COLOR="Navy"]As[/COLOR] File
[COLOR="Navy"]Dim[/COLOR] ts [COLOR="Navy"]As[/COLOR] TextStream
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]Resume[/COLOR] [COLOR="Navy"]Next[/COLOR]
sTemp = sBackupPath & "\" & "log.txt"
[COLOR="Navy"]Set[/COLOR] ts = FSO.OpenTextFile(sTemp, 8, True, -2)
ts.WriteLine arg
ts.Close
[COLOR="Navy"]Set[/COLOR] ts = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]Set[/COLOR] f = FSO.GetFile(sTemp)
[COLOR="Navy"]If[/COLOR] f.Size > 8000000 [COLOR="Navy"]Then[/COLOR] [COLOR="SeaGreen"]'//Log file has been ignored for ages. Delete it.[/COLOR]
f.Delete
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="Navy"]Set[/COLOR] f = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]