Search code examples
ms-access

Compacting Access files quietly


I created a script that compacts .mdb files on a schedule.

It works great when I call msaccess.exe with the /compact argument, except when it finds an mdb file that has been corrupted.

Instead of compacting it, it stops processing and shows this message until I click OK, and only after that does it start the compacting: microsoft access has detected that this database is in an inconsistent state

How do I avoid this window? Is there a /quiet or /nogui equivalent argument that shows no GUI and just does the compact commmand?

This script would run at night and I can't be there to click OK every time.

JETCOMPACT is not an option, because it freezes when I try to compact one of our mdb files.


Solution

  • I solved it myself in PowerShell. This compacts and repairs mdb files without confirmation or messageboxes interrupting:

    $ObjectAccess = New-Object -ComObject "Access.Application"
    $ObjectAccess.CompactRepair($MDBFilePathSource, $MDBFilePathDestination)
    $ObjectAccess.Quit()
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($ObjectAccess)