Compact and repair

  • Thread starter Thread starter Chad
  • Start date Start date
C

Chad

Is there a way to compact and repair a database through
VBA? If so can someone please let me know?

Thanks,
Chad
 
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
 
Thanks,
That helped a lot. Mentioned in your post that you are
able to compact and repair other databases through code
assuming they are closed. Can you give me an idea of how
to do this?

Thanks,
Chad
 
Chad,

In the same newsgroup there is a posting by Kcomer, titled "Compacting DBs",
dated 12/01/04 12:25. See my reply to that one.

Regards,
Nikos
 
Back
Top