To be compacted, a database has to be closed. Therefore,
when you run compact and repair from the menu, Access
actually closes the database, makes a copy of it named
dbX.mdb (X is 1,2,3...), compacts the copy, and if no
error is encountered, it deletes the original .mdb,
renames dbX.mdb to the original name and reopens it.
This explains why a database cannot compact itself through
a macro or VB code; if it's closed the code won't run
anymore! VB code can compact ohter databases (provided
they're closed at the time), not the current.
A useful workaround is to set the database to compact on
close (Tools > Options > General > Compact On Close). It
will secure regular compaction with no code required.
HTH,
Nikos