Compacting Databases

With use, the internal structure of a database can become fragmented, leading to wasted disk space — and to poor performance if the fragmentation is excessive.

Compacting a database:

Before you can compact a database, the following conditions must be met:

Note that you cannot compact the database that is currently open in Microsoft Access. You must close the database and compact it either from the Microsoft Access user interface or from another Visual Basic project.

Determining whether the current database is open exclusively is not simply a matter of trying to open the database. If you have the database open exclusively and try to open it again, you won’t receive a run-time error as you may expect. Actually, you can open a database as many times as you want to, exclusively or not, with the same DBEngine object.

To determine whether the current database is open exclusively, you can try to open the database using the PrivDBEngine object. For more information, see “Determining Whether the Current Database Is Open Exclusively” in Chapter 2, “Introducing Data Access Objects.”

You can compact a Microsoft Jet database using the CompactDatabase method of the DBEngine object. Although the primary use of this method is to compact a database, it supports a variety of other operations with the options argument. The syntax of the CompactDatabase method is:

DBEngine.CompactDatabase olddb, newdb [,locale [,options[,password]]]

In this syntax:

You can’t use the newdb argument to specify the same database file as the olddb argument.