CompactRepair 方法

       

压缩和修复指定的数据库 (.mdb) 或 Microsoft Access 项目 (.adp) 文件。返回 Boolean 型值;如果处理成功,返回 True

expression.CompactRepair(SourceFile, DestinationFile, LogFile)

expression   必需。返回“Applies To”列表中的一个对象的表达式。

SourceFile  String 型,必需。代表要压缩和修复的数据库或项目文件的完整路径和文件名。

DestinationFile  String 型,必需。完整的路径和文件名,代表所返回文件的保存位置。

LogFile  Boolean 型,可选。如果在目标目录中创建一个日志文件用于记录在源文件中检测到的损坏,则为 True。只有在源文件中检测到损坏时,才创建日志文件。如果 LogFileFalse 或省略,就不创建日志文件,即使在源文件中检测到损坏也是如此。

说明

源文件不可以是当前数据库,也不能被其他用户打开,因为调用该方法将会以独占方式打开源文件。

示例

下面的示例将压缩和修复一个数据库,如果在源文件中检测到损坏,则会创建一个日志文件,并根据恢复是否成功而返回一个 Boolean 值。为使该示例运行,必须将源文件和目标文件的路径和文件名传给它。

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