Frequently, the database often has problems with operations when working in an Access database. And the database is infinitely larger. In order not to affect the use we need to compress/repair the database. The following code is used to compress and repair the Access database de code. You can use it directly. 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 if the database file exists
If Len (Dir) Then
' Perform backup if backup is required
If backuporiginal = True Then
Strbackupfile = Gettemporarypath & "Backup.mdb"
If Len (Dir (strbackupfile)) then Kill Strbackupfile
FileCopy location, Strbackupfile
End If
' Create temporary file name
Strtempfile = Gettemporarypath & "Temp.mdb"
If Len (Dir (strtempfile)) then Kill Strtempfile
' Compress database files via DBEngine
Dbengine.compactdatabase location, Strtempfile
' Delete the original database file
Kill Location
' Copy has just compressed the temporary database file to its original location
FileCopy Strtempfile, location
' Delete temporary files
Kill Strtempfile
End If
Compacterr:
Exit Sub
End Sub
Public Function Gettemporarypath ()
Dim Strfolder as String
Dim Lngresult as Long
Strfolder = String (MAX_PATH, 0)
Lngresult = GetTempPath (MAX_PATH, Strfolder)
If Lngresult <> 0 <