As your application is used, the database file can grow in size. If you are responsible for the maintenance of your database, you’ll need to perform a number of system-oriented tasks from time to time, such as compacting the database and repairing it if it becomes corrupted. This section discusses these maintenance tasks.
If you are performing compact and repair operations, you will often use the menu commands provided in the user interface. The Compact Database and Repair Database commands are both available on the Database Utilities submenu (Tools menu) in the Microsoft Access startup window.
However, if you want to provide a way for your users to compact and repair their copies of your application, you can provide them with either an icon that performs the compact and repair operations or code that compacts and repairs the database automatically.
You can create a shortcut that uses the /compact or /repair command-line options. You can specify either option, or both. If you specify both the /compact and /repair options, the repair always happens before the compact. These command-line options compact and repair your application without opening it. When you create the shortcut, type a command line that consists of:
Important To compact or repair a database, the database and any connections to it must be closed. Also, if you have established user-level security for your application, the user running the repair or compact operation must have Open Exclusive permission for the database, and have Modify Design or Administer permission for all tables in the database. For more information on permissions, search the Help index for “setting permissions.” Additionally, when compacting, there must be sufficient disk space for both the original and compacted versions of the database, even if the database is being compacted to the same file name. This is because the compacted database is renamed as the original database only after compacting to a temporary file is successful.
To maintain a high state of performance, Microsoft Access defers the removal of discarded pages until you shut down the database and compact the discarded pages. This design keeps the interactive performance of your database high at the expense of recoverable disk space.
Compacting a database:
To compact your database, point to Database Utilities on the Tools menu and click Compact Database. If the database you want to compact is currently open, it will be closed, compacted into a temporary file, and then reopened. If no database is open, you must specify the database to compact and the database to compact into. If you specify the same file name to compact into, Microsoft Access compacts into a temporary file. When Microsoft Access compacts to a temporary file, it renames the temporary file back to the original file name once the compacting process is completed.
You can also use the CompactDatabase method of the DBEngine object to compact a database. When you use the CompactDatabase method, you must compact to a file with a different name. The following code example uses the CompactDatabase method to compact a database to a temporary file and then renames that temporary file to the original name if the compacting process is successful.
Sub CompactDB()
On Error GoTo CompactDB_Err
Const conFilePath = "C:\Program Files\Microsoft Office\Office\Samples\"
' Compact the database to a temp file.
DBEngine.CompactDatabase conFilePath & "Northwind.mdb", _ conFilePath & "NorthTemp.mdb"
' Delete the previous backup file if it exists.
If Dir (conFilePath & "Northwind.bak") <> "" Then
Kill conFilePath & "Northwind.bak"
End If
' Rename the current database as backup and rename the temp file to ' the original file name.
Name conFilePath & "Northwind.mdb" As conFilePath & "Northwind.bak"
Name conFilePath & "NorthTemp.mdb" As conFilePath & "Northwind.mdb"
MsgBox "Compacting is complete"
Exit_CompactDB:
Exit Sub
CompactDB_Err:
MsgBox Err.Description
Resume Exit_CompactDB
End Sub
The CompactDatabase method also gives you the option of changing the encryption state, the version, and the disk or folder of a database while it’s being compacted. In other words, you can choose to convert an encrypted database to a non-encrypted database, or vice versa.
For example, the following code compacts a database named Old and creates a new, encrypted database named New.
DBEngine.CompactDatabase "C:\Old.mdb", "C:\New.mdb", _
dbLangGeneral, dbEncrypt
See Also For more information on database encryption, see “Encrypting a Database” in Chapter 14, “Securing Your Application.”
Note Compacting cannot be done inside a transaction, or on a database that is currently open by another user.
Replicated databases make extensive use of temporary space, especially when many design changes are made to an application. Just as with non-replicated databases, compacting regularly reduces file size and improves performance.
For optimal results, compact a replicated database twice. Compacting a replica the first time reclaims some space and flags other space as available for reclaiming later. Compacting a second time reclaims all available space. Compacting more than twice doesn’t provide additional benefits.
Improperly compacting the Design Master of a replica set causes the replica set to lose its Design Master. When Microsoft Access opens a replica, it stores the name and location of the file in the database. The next time Microsoft Access opens the replica, it checks to see if the file has the same name and location. If so, then the file opens normally. If not, Microsoft Access gives the replica a new ReplicaID property value and, if the file was the Design Master, removes the Design Master flag. This prevents two replicas from having the same ReplicaID property value or a replica set from having two Design Masters.
To prevent a Design Master from losing its Design Master flag when you compact a replicated database, do one of the following:
If your database is damaged, close the database, then point to Database Utilities on the Tools menu and click Repair Database. You can also use the RepairDatabase method of the DBEngine object to repair your database. The RepairDatabase method checks all pages in the database for correct linkage, validates all system tables, and validates all indexes. Because the RepairDatabase method can’t fix all possible forms of database corruption, you should back up your database files regularly to avoid unrecoverable data loss. This kind of corruption can occur when the system isn’t shut down normally (such as during a power failure).
The RepairDatabase method takes only one argument, the name of the database file you want to repair. For example, to repair the Northwind database, use the following code:
DBEngine.RepairDatabase "C:\Program Files\Microsoft Office\Office" _
& "\Samples\Northwind.mdb"
When a database is repaired, it may increase in size, because the process of creating indexes may leave some deleted pages in the database. It’s always a good idea to compact the database after any repair to eliminate unnecessary pages.