|
|
Сжатие внешней базы данных
Private Sub CompactExternalDB_Demo()
Dim lRet&, sVal$
sVal = "d:\Temp\modArchivTestDB MSA2003\MoneyDB_SM.mdb"
lRet = CompactExternalDB(sVal)
If Not lRet = 0 Then
End If
End Sub
Private Function CompactExternalDB(sDBFilePath As String, Optional blnNoReportMSG As Boolean) As Long
Dim sTempFilePath$, cVal@, iVal%, sVal$
Dim sDBFileName$, sDBFileExt$, sDBFolderPath$, lSizeBefore As Long, iSizeAfter As Long
Dim objFSO As Object
Dim objFSOFile As Object
Const csMsgBoxTitle$ = "Сжатие БД"
Const csTempFilePrefix$ = "Temp_"
On Error GoTo CompactExternalDB_Err
If Dir(sDBFilePath) = "" Then
MsgBox "Файл данных:" & vbCrLf & """" & sDBFilePath & """" & vbCrLf & _
"Не существует." & vbCrLf & "Прдолжение не возможно", vbExclamation, csMsgBoxTitle
GoTo CompactExternalDB_End
End If
iVal = FreeFile
Open sDBFilePath For Random Access Read Write Lock Read Write As #iVal
Close #iVal
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFSOFile = objFSO.GetFile(sDBFilePath)
lSizeBefore = objFSOFile.Size
sDBFileExt = "." & objFSO.GetExtensionName(sDBFilePath)
sDBFolderPath = objFSOFile.ParentFolder.Path
sTempFilePath = sDBFolderPath & csTempFilePrefix & Format(Now, "yyyymmdd_hhnnss") & sDBFileExt
sVal = sDBFolderPath & "\Temp_" & Format(Now, "yyyy") & "*" & sDBFileExt
If objFSO.FileExists(sVal) Then objFSO.DeleteFile sVal
DBEngine.CompactDatabase sDBFilePath, sTempFilePath
DoEvents
objFSO.CopyFile sTempFilePath, sDBFilePath, True
DoEvents
objFSO.DeleteFile sTempFilePath
If blnNoReportMSG = False Then
iSizeAfter = objFSOFile.Size
cVal = (iSizeAfter - lSizeBefore) / 1024
If cVal > 1024 Then
sVal = Format(cVal / 1024, "# ##0.00") & " Mb"
Else
sVal = Format(cVal, "# ##0.00") & " Kb"
End If
sVal = "База данных:" & vbCrLf & """" & sDBFilePath & """" & vbCrLf & _
"Успешно сжата." & vbCrLf & "Изменение размера: " & sVal
MsgBox sVal, vbInformation, csMsgBoxTitle
End If
CompactExternalDB_End:
On Error Resume Next
Close #iVal
Set objFSO = Nothing: Set objFSOFile = Nothing
Err.Clear
Exit Function
CompactExternalDB_Err:
Select Case Err.Number
Case 70
MsgBox "База данных:" & vbCrLf & """" & sDBFilePath & """" & vbCrLf & "Открыта другим процессом." & vbCrLf & _
"Прдолжение не возможно", vbExclamation, csMsgBoxTitle
Case Else
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Function :" & _
"CompactExternalDB.", vbCritical, "Error! " & csMsgBoxTitle
End Select
Err.Clear
Resume CompactExternalDB_End
End Function
|
|