以下为CompactRepair方法的VBA帮助里面的示例
Function RepairDatabase(strSource As String, _
strDestination As String) As Boolean
' Input values: the paths and file names of
' the source and destination files.
' Trap for errors.
On Error GoTo error_handler
' Compact and repair the database. Use the return value of
' the CompactRepair method to determine if the file was
' successfully compacted.
RepairDatabase = _
Application.CompactRepair( _
LogFile:=True, _
SourceFile:=strSource, _
DestinationFile:=strDestination)
' Reset the error trap and exit the function.
On Error GoTo 0
Exit Function
' Return False if an error occurs.
error_handler:
RepairDatabase = False
End Function
很容易就能明白,CompactRepair方法本来是用来修复压缩指定的MDB文件,但原始文件被压缩后会生成一个新的MDB文件,那就可以利用这个功能,进行备份了,而且备份文件和filecopy方法或者API文件复制的方法相比,是经过压缩修复的.缺点就是源文件必须是不被打开状态.
以下为调用方法,如果只需要备份的话,就可以将删除文件,重命名的代码给注释掉
Public Function AutoCompactCurrentProject()
Dim fs, f, s, filespec
Dim strProjectPath As String, strProjectName As String
Call RepairDatabase("c:\上标与下标.mdb", "c:\1.mdb")
Set fs = CreateObject("Scripting.FileSystemObject")
f = fs.deletefile("c:\上标与下标.mdb", False) '删除原始文件
Name "c:\1.mdb" As "c:\上标与下标.mdb" '将压缩修复后的文件改名
End Function