Skip to content
Auscultby NBIT
EN
Free diagnostic

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

  1. Get everyone out: compacting needs exclusive access to the file.
  2. Make a copy of the database. Microsoft recommends it: the repair may remove data from damaged tables.
  3. Database open: File › Info › Compact & Repair Database.
  4. 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.

compact_data.cmd — adjust the paths; the path to MSACCESS.EXE depends on how Office is installed.
@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%" /compact

Automate 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.

Compact the data file from an admin database: keep yesterday's copy, then replace it with the compacted file.
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 Function

When 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

All articles

Frequently asked questions

How often should I compact?

There is no fixed rule. Watch the file size: if it grows quickly, schedule regular compacting of the data file, and look for the work tables that make it swell.

Can compacting lose data?

On a healthy database, no. On a damaged one, the repair may remove unreadable records: that is why you always compact after making a copy.

My database is close to 2 GB: what now?

Compact first, then purge or archive old data, split the database if that is not done yet, and if growth continues, move the data to SQL Server: see migrate an Access database to SQL Server with SSMA.

Where are the other errors in your application hiding?

The diagnostic runs on your computer, on a copy of the application, without sending the data in your tables. The health grade and the summary are free.