Public Function AutoCompactCurrentProject()
Dim fs, f, s, filespec
Dim strProjectPath As String, strProjectName As String
strProjectPath = Application.CurrentProject.Path
strProjectName = Application.CurrentProject.Name
filespec = strProjectPath & "\" & strProjectName
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.GetFile(filespec)
s = CLng(f.Size / 1000000) ‘轉換文件大小為MB
If s > 20 Then ‘修改此處的20M為你自己需要的值
Application.SetOption ("Auto Compact"), 1 ‘壓縮程序
Else
Application.SetOption ("Auto Compact"), 0 ‘不壓縮程序
End If
End Function
在任何退出的程序前調用這個函數,例如在Docmd.Quit前調用:
AutoCompactCurrentProject