Although you don’t need to do too much to maintain an Access database, you must know about a few important techniques that you should use to ensure that you maintain your databases as effectively as possible. The first technique is compacting. Compacting a database means removing unused space from a database (.accdb
or .mdb
file). The second technique involves backing up your databases. Without a proper backup procedure in place, you are like a circus performer without a safety net. Another useful technique to have at your disposal is the capability to convert a database created in an earlier version of Access to the .accdb
file format. Finally, it is important that you are able to detect broken references within your database. This chapter covers the compacting process and the ways you can compact. It also covers all the other maintenance techniques available to you within Microsoft Access.
As you and the users of your application work with a database, the database grows in size. To maintain a high state of performance, Access defers the removal of discarded pages from the database until you explicitly compact the database file. This means that as you add data and other objects to the database and remove data and objects from the database, Access does not reclaim the disk space that the deleted objects occupied. This not only results in a very large database file, but it also ultimately degrades performance, as the physical file becomes fragmented on disk. Compacting a database accomplishes the following tasks:
1
.Defragmenting the hard drive that a database is stored on before performing the compacting process is a good idea. The defragmentation process ensures that as much contiguous disk space as possible is available for the compacted database.
In earlier versions of Access, the repair process was a separate utility from the compacting process. With Access 2000, Access 2002, Access 2003, and Access 2007, there is no longer a separate repair process. The compacting and repair processes both occur when you compact a database. When you open a database in need of repair, Access prompts you to compact it.
To compact a database, you can use one of five techniques:
CompactDatabase
method of the JetEngine
object.CompactRepair
method of the Application
object.Regardless of which method you select for the compacting procedure, the following conditions must be true:
Modify Design
permission for all tables in the database.Backing up the database before you attempt to compact it is a good idea. It is possible for the compact process to damage the database. Also, do not use the compacting process as a substitute for carefully following backup procedures. The compacting process is not always successful. Nothing is as foolproof as a fastidiously executed routine backup process.
If, at any time, Access detects that something has damaged a database, it will prompt you to repair the database. This situation occurs when you attempt to open, compact, encrypt, or decrypt the damaged database. At other times, Access might not detect the damage. Instead, you might suspect that damage has occurred because the database behaves unpredictably. This is the time you should first back up and then perform the compacting process, using one of the methods covered in this chapter.
Access provides a fairly straightforward user interface to the compacting operation. To compact a currently open database, click the Microsoft Office Access button and select Manager, Compact and Repair Database. Access closes the database, compacts it, and then reopens it.
To compact a database other than the currently open database, follow these steps:
Figure 30.1. The Database to Compact From dialog box allows you to select the database you want to compact.
Figure 30.2. The Compact Database Into dialog box allows you to select the name for the compacted database.
To give users a simple way to compact a database, you can create an icon that performs the compacting process. You accomplish this by using the /Compact
command-line option, which compacts the database without ever opening it. The shortcut looks like this:
c:Progam FilesMicrosoft Office 2007Office12
Msaccess.exe c:DatabasesTimeAndBilling.ACCDB /Compact
You can follow this syntax with a space and the name of a destination database if you do not want Access to overwrite the current database with the compacted version. If you do not include a path for the destination database, Access places it in the My Documents folder by default.
To create a shortcut, follow these steps:
Using the environmental setting Compact on Close, you can designate that Access will compact specific databases whenever the user closes them. Access compacts a database upon close only if it determines that the compact process will reduce the size by at least 256KB. To set the Compact on Close environmental setting, follow these steps:
Figure 30.3. Select the Current Database tab of the Access Options dialog box.
Although set in the Access Options dialog box, because the Compact on Close setting is on the Current Database tab, it applies only to the database that is open when you select the option. This feature allows you to selectively designate which databases Access compacts when the user closes them.
Remember that when you use the Compact on Close option, the database must meet all the conditions ordinarily required for Access to compact a database. For example, if other users are in the database when someone tries to close it, the user trying to close the database receives an error.
CompactDatabase
Method of the JetEngine
ObjectUsing the CompactDatabase
method, you can compact a database using code. The CompactDatabase
method is performed on a member of the Microsoft Jet and Replication Objects (JRO), the JetEngine
object. It receives a source connection string and a destination connection string as parameters. The Access Database Engine uses these connection strings to designate the source and destination databases, respectively. The Access Database Engine also uses the Source Connection
and Destination Connection
parameters for the following purposes:
The Locale Identifier property of the Destination Connection
parameter determines the collating order in which the Access Database Engine sorts the data in the compacted database. You use this option when you are working with a database in which the data is stored in another language, and you want the data to be collated in a particular language.
The Jet OLEDB:Encrypt Database property of the Destination Connection
parameter specifies whether you want the compacted database to be encrypted. If you do not specify this property, the compacted database will have the same encryption status as the original source database.
The Jet OLEDB:Engine Type property of the Source Connection
parameter designates the version of the source database to open. The Jet OLEDB:Engine Type property of the Destination Connection
parameter indicates the version of the new database. If this property is omitted, the version of the source and destination databases is the same.
Finally, the User ID and Password properties of the Source Connection
parameter enable you to supply the name of the user and the user’s password for a database that is password protected.
The following code, contained in the basCompactDB
module of Chap30Ex.ACCDB
, compacts and encrypts a database called Chap30Big.ACCDB
:
The code names the compacted database Chap30Small.MDB
. The code also encrypts the database during the compacting process.
For this code to execute successfully, remember that you must close the Chap30Big
database, and the user running the code must have the right to open the database exclusively. Furthermore, the user must have Modify Design
permissions for all tables in the database. Finally, because the JRO JetEngine
object performs the CompactDatabase
method, you must include a reference to the Microsoft JRO 2.1 Library. Access does not reference this library by default when you create a new Access database. You must use Tools, References to reference it.
The CompactDatabase
method of the JetEngine
object does not work with an Access 2007 database. You need to use the CompactRepair
method of the Application
object when working with Access 2007 files, and you can use the CompactDB method with databases stored in an earlier file format.
CompactRepair
Method of the Application
ObjectAn alternative to the JetEngine
object is a method introduced with the Access 2007 Application
object. The CompactRepair
method simplifies the process shown in the preceding section:
The code, located in basCompactDB
, declares a string variable. The Left
and InstrRev
functions extract the current path from the Name
property of the CurrentDB
object. If the designation file is located in the current folder, the code deletes it. The CompactRepair
method of the Application
object compacts and repairs the database into the designated destination database. The CompactRepair
method receives three parameters. The first is the name and location of the source database, the second is the name and location of the destination database, and the third is whether you want Jet to log the operation.
Introduced with Access 2003 is the capability to back up your database from within Microsoft Access. Here’s the process:
Figure 30.4. The Save As dialog box allows you to provide the name and location for the backup database.
Because the backup process simply creates a copy of the open database in a name and location that you specify, restoring the database involves moving and/or renaming the backup database file to the production location and name. You can then simply open the backup database and continue working as usual.
After you have moved or renamed the backup database, you can access it from the Microsoft Office Access button just like any other Access database.
Access 2007 makes it easy to interact with other versions of Access. Access 2007 allows you to open, read, and update Access databases stored in the Access 2000 file format and the Access 2002–2003 file format, without converting the files to the Access 2007 file format. It is important to note that as long as the database is stored in the MDB file format, you will not be able to take full advantage of Microsoft Office Access 2007’s rich set of new features.
As mentioned earlier, you can use Access 2000 files and Access 2002–2003 files with Access 2007. If you want to convert an open database stored in the Access 2000 or Access 2002–2003 file format to the Access 2007 file format, take the following steps:
Figure 30.5. The Convert Database option appears after you click the Microsoft Office Access button.
Figure 30.6. Access warns you that you cannot use the ACCDB file with previous versions of Access.
A method introduced with Access 2002 makes it easy to programmatically convert an Access database from one version to another. The code, found in basMaintenance
, looks like this:
To begin, the code declares a string variable. It uses the built-in Left
and InStrRev
functions to extract the path associated with the current database and place it in the strFilePath
variable. If the destination database exists in the current folder, the code deletes it. The code uses the ConvertAccessProject
method of the Application
object to convert the Chap30Small.mdb
database, located in the current folder and stored in the Access 2002–2003 file format, to the Chap30V2007.ACCDB
database, located in the current folder and stored in the Access 2007 file format.
Constants exist for the ConvertAccessProject
method that allow you to convert to the Access 2007, Access 2002, Access 2000, Access 97, Access 95, and Access 2.0 file formats.
Prior to Access 2002, it was difficult to locate and diagnose broken references. Access 2007 offers BrokenReference
, a property of the Application
object that rectifies this problem. If broken references exist, the property evaluates to True
. If no broken references exist, it evaluates to False
. Querying the BrokenReferences
property is much more efficient than looping through each reference to determine whether it is intact. The code, found in basMaintenance
, looks like this:
Begin by using the techniques you learned to back up a database. Then practice compacting the database using each of the five methods covered in the chapter. Finally, determine whether your database has any broken references.
You should perform the compacting process regularly—especially on databases containing your application data. The compacting process provides major benefits in terms of both performance and conservation of disk space. The more activity that occurs on a database, the more frequently you should compact it. Although you should consider the compacting process an important part of the database maintenance process, remember that there is absolutely no substitute for proper backup techniques. This chapter also showed you a feature included in Access 2007 that allows you to back up an open database.
In addition to compacting your database, you need to understand the database conversion options available to you. You can convert databases from one version of Access to another using either the user interface or code. Finally, whereas it was an arduous, time-consuming process to detect broken references prior to Access 2002, the BrokenReference
property makes this process much easier, by reporting whether all database references are intact. Using all the techniques covered in this chapter should save you a lot of time and effort in maintaining and working with your databases.
3.17.148.66