Codice:
Function CompactAndRepairDatabase(DatabasePath As String, _
Optional Password As String, _
Optional TempFile As String = "c:\temp.mdb")
If DBEngine.Version < "3.6" Then DBEngine.RepairDatabase DatabasePath
'if no temp file specified, use "c:\temp.mdb"
If TempFile = "" Then TempFile = "c:\temp.mdb"
'delete temp file if it exists
If Dir(TempFile) <> "" Then Kill TempFile
'format password in form of ";pwd=PASSWORD" if password exists
If Password <> "" Then Password = ";pwd=" & Password
'compact db: a new db will be created
DBEngine.CompactDatabase DatabasePath, TempFile, , , Password
'kill first db
Kill DatabasePath
'move compacted db to original db's path
FileCopy TempFile, DatabasePath
'delete temp file
Kill TempFile
End Function