Use case
Compact and repair an Access database (and automate it)
An Access database grows even when you delete data: the space freed is only given back when you compact. Too big, the file slows down, and at 2 GB Access can no longer write to it. Here is how to compact and repair safely, then three ways to stop thinking about it.
Why the file grows
- Access creates hidden temporary objects for its work, and does not always remove them;
- a deleted record or object does not give its space back to the disk;
- an application that empties and refills work tables each time it runs (append or make-table queries) swells the file day after day.
The result: objects open more slowly, queries take longer, and there is a hard limit — an Access file cannot exceed 2 GB, system objects included.
Compacting by hand
- Get everyone out: compacting needs exclusive access to the file.
- Make a copy of the database. Microsoft recommends it: the repair may remove data from damaged tables.
- Database open: File › Info › Compact & Repair Database.
- Database closed: open Access with no database, then Database Tools › Compact and Repair Database, and pick the file.
Automate 1: compact on close
File › Options › Current Database › Compact on Close. Useful for the front-end file installed on a PC, or for a personal database. Avoid it for a shared database opened across the network: compacting rewrites the whole file over the network, just as the user closes up and shuts down.
Automate 2: schedule it overnight
The /compact command-line switch compacts and repairs the named database, then closes Access. A small script, run every night by Windows Task Scheduler on a PC with Access, is enough: it gives up if someone left the database open, keeps yesterday's copy, then compacts.
@echo off
set DB=\\server\sales\Data_be.accdb
set LOCK=\\server\sales\Data_be.laccdb
set PREVIOUS=\\server\backups\Data_be_previous.accdb
rem someone has the database open: leave it alone
if exist "%LOCK%" exit /b 1
copy /y "%DB%" "%PREVIOUS%" || exit /b 1
"C:\Program Files\Microsoft Office\root\Office16\MSACCESS.EXE" "%DB%" /compactAutomate 3: in VBA, from another database
Application.CompactRepair compacts a file into a new file and returns True if all went well. The source file must be neither the current database nor open by anyone else, and the destination file must not exist. The DAO method DBEngine.CompactDatabase does the same without a log.
Public Function CompactData() As Boolean
Const DB As String = "\\server\sales\Data_be.accdb"
Const TEMP As String = "\\server\sales\Data_be_compact.accdb"
Const PREVIOUS As String = "\\server\backups\Data_be_previous.accdb"
If Dir(Replace(DB, ".accdb", ".laccdb")) <> "" Then Exit Function ' database open
If Dir(TEMP) <> "" Then Kill TEMP
If Not Application.CompactRepair(DB, TEMP, True) Then Exit Function
FileCopy DB, PREVIOUS ' yesterday's copy
Kill DB
Name TEMP As DB
CompactData = True
End FunctionWhen repair is not enough
If the database will not open ("Unrecognized database format"), or records show "#Deleted", compacting is only one step: the full procedure is in Access database corrupt or unrecognized: what to do. And if corruption keeps coming back, the network is often the cause: see Access on a network share.
After compacting
The Auscult diagnostic neither compacts nor repairs. On the compacted database, it tries every form, every report and every query, and tells you what works and what fails, with the cause. The report also flags among the risks a database that is not split, or server paths written into the code. See the Access database audit.
Sources
- Compact and repair a database — Microsoft Support
- Access specifications — Microsoft Support
- Command-line switches for Microsoft Office products — Microsoft Support
- How to: Compact and Repair a Database (Access VBA) — Microsoft Learn
- DBEngine.CompactDatabase method (DAO) — Microsoft Learn