Compress the ACCESS database in VB. If you delete data or objects in the Access database or Access project, fragments may occur and the disk space usage efficiency may be reduced. At the same time, the size of the database file is not reduced. if you delete data or objects in the Access database or Access project, fragments may occur and the disk space usage efficiency may be reduced. At the same time, the size of the database file is not reduced, but is constantly increasing until your hard disk has no space. Is there a good solution? In fact, the database can be compressed and optimized in Access to improve the performance of Access databases and Access projects. the essence of such compression processing is to copy the file, and re-organize the storage of files on the disk. However, such compression in the Access Project does not affect database objects (such as tables or views), because they are stored in the Microsoft SQL Server database rather than in the Access project itself. Similarly, this compression will not affect the automatic number in the Access Project. In the Access database, if records have been deleted from the end of the table, compressing the database will reset the automatic number value. The automatic number of the next record to be added will be greater than the automatic number of the last record not deleted in the table.
The following describes how to use a CompactJetDatabase process in VB to compress Access database files. in this process, an optional parameter is provided, whether you need to back up the original database files to a temporary directory (True or False) before compression ). I used this method to compress a MB database to only KB.
'These codes can be placed in the module and used in other forms
Public Declare Function GetTempPath Lib "kernel32" Alias _
"GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Public Const max_path= 260
Public Sub CompactJetDatabase (Location As String, Optional BackupOriginal As Boolean = True)
On Error GoTo CompactErr
Dim strBackupFile As String
Dim strTempFile As String
'Check whether the database file exists
If Len (Dir (Location) Then
'Back up if you need to back up
If BackupOriginal = True Then
StrBackupFile = GetTemporaryPath & "backup. mdb"
If Len (Dir (strBackupFile) Then Kill strBackupFile
FileCopy Location, strBackupFile
End If
'Create a temporary file name
StrTempFile = GetTemporaryPath & "temp. mdb"
If Len (Dir (strTempFile) Then Kill strTempFile
'Use DBEngine to compress database files
DBEngine. CompactDatabase Location, strTempFile
'Delete the original database file
Kill Location
'Copy the temporary database file that has just been compressed to the original location
FileCopy strTempFile, Location
Bytes. At the same time, the size of the database file is not reduced ,...