Compact and Repair a Database

Office 2013 and later

Contribute to this content

Use GitHub to suggest and submit changes. See our guidelines for contributing to VBA documentation.

Access database files (.accdb) can grow quickly as you use them, sometimes impeding performance. They can also occasionally become corrupted or damaged. You can use the CompactRepair method on the Access.Application object to prevent or fix these problems.

The CompactRepair method compacts a bloated database file, and if the database file is corrupted, repairs the database file and creates a log file. This method returns a Boolean value based on whether the recovery was successful (True for success).

' Example driver application.
Sub Test()

Dim repaired As Boolean

' You can simply make up a name for the destination file.
repaired = RepairDatabase("C:\Users\Public\Documents\Database.accdb", _

MsgBox repaired

End Sub

' Input values: The fully-qualified path names of the source and destination
' database files.
Function RepairDatabase(sourceDb As String, destDb As String) As Boolean

' Compact and repair the database. You can use the return value of the
' CompactRepair method to determine if the file was successfully compacted.
RepairDatabase = Access.Application.CompactRepair(sourceDb, destDb, True)

End Function