|Compacting an Access database with ADO and Delphi|
|While working in a database application you change data in a database, the database becomes fragmented and uses more disk space than is necessary. Periodically, you can compact your database to defragment the database file. This article shows how to use JRO from Delphi in order to compact an Access database from code.|
As with ADOX, the JRO library must be imported in Delphi, since it is not a part of the ADOExpress (or dbGo in D6). The description of the ADOX library is "Microsoft Jet and Replication Objects 2.x Library (Version 2.x)". The JRO library file name is MSJRO.dll. We've already seen the steps needed to import a type library in Delphi (ADOX). The same process should be repeated in this case. To import JRO in Delphi you need to open a new project and Select Project | Import Type Library. In the dialog box choose "Microsoft Jet and Replication Objects 2.x Library (Version 2.x)". Note that it will add two new classes, the TReplica and TJetEngine. Press Install button to add JRO to a package or press Create unit to just create a single interface unit. If you click Install, two new icons will appear on the ActiveX tab (if you have left the default Palette page on the Dialog).
Note: Delphi 6 users will not succeed in importing JRO type library. If you have Delphi 6, while trying to install the library in a package, an error will pop up indicating that ActiveConnection in the JRO_TLB file doesn't exist (along with some other errors). The problem lies in Delphi 6 TLB importer. There are two options to overcome the problem: 1. Use Delphi 5 to import JRO an then install it in Delphi 6. 2. Manually declare the missing ActiveConnection property and change property declarations to make them writeable.
Compact Delphi Project
The TJetEngine class has a CompactDatabase method. The method takes two parameters: the ADO connection string for the source as well for the destination database. CompactDatabase method compacts a database and gives you the option of changing its version, password, collating order and encryption.
In our form, the edSource is used to specify the database we want to compact. The edDest specifies the destination database. Within the connection strings, you specify various connection properties to determine how the source database is opened and how the destination database is compacted. At a minimum, you must use the Data Source property in each connection string to specify the path and name of the database.
The next code (btnCompact OnClick event handler) is an example of the CompactDatabase method:
Note that the above code presumes an Access 2000 database. Microsoft Jet OLEDB 4.0 is the default data engine for Access 2000.
In many cases you'll want to have the same database name after the compact operation. Since edSource and edDest can't be the same your code should replace the original file with the compacted version. The next function takes only one parameter - the name of the database you want to compact:
The DatabaseCompact receives a sdbName string parameter with the full name of the database you want to compact. The function returns True if compact is successful False otherwise. The sdbName is compacted in sdbTemp, the sdbName is then deleted and sdbTemp renamed to sdbName. The DatabaseCompact could be called as:
The DatabaseCompact function is ideal to be called from within your Delphi ADO application as an external application. It could also be written as a console mode application that takes one command line parameter (or more) since it requires no GUI.