HOWTO: Compacting Microsoft Access Database Through OLE DB<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
OLE DB specification doesn't provide interfaces to compact or repair databases. However, the OLE DB Provider for Microsoft Jet version 4.0 exposes this functionality through a custom interface: IJetCompact (TDataSource cotype). IJetCompact is defined in the header file Jetoledb.h that can be obtained separately from Microsoft (Please see knowledge base article Q228525).
To repair and compact a Microsoft Access database using OLE DB, MDAC 2.1 or higher version must be properly installed on the computer. The following steps are required:
1. Create and initialize the data source objects by using interfaces of IDBInitialize and IDBCreateSession.
2. Use GetJetEngineType function to get the version of Jet database.
3. Fill a DBPROPSET structure with information about the destination database to compact to.
4. Query the IDBCreateSession object for the Jet Compact object: IJetCompact.
5. Call IJetCompact's Compact method passing in the DBPROPSET created in step 2.
The following code demonstrates the earlier steps using Visual C++ ATL OLE DB consumer templates:
1. If you have a compiler error such as:
DBPROP_JETOLEDB_ENGINE undefined specifier
For additional information about updated Jet header files, please click the article number below to view the article in the Microsoft Knowledge Base:
228525 PATCH: JetVC.exe VC++ Support Files for the Jet OLE DB Provider
2. Inline the GetJetEngineType function in your code if you don't want to open another connection.
3. Compacting a database also repairs the database, unlike in DAO where it was a separate functionality. In OLE DB there is no way to only repair a database.
For additional information, please click the article number below to view the article in the Microsoft Knowledge Base:
230501 HOWTO: Compacting Microsoft Access Database via ADO
The information in this article applies to:
Microsoft OLE DB Provider for Jet 4.0
Microsoft Data Access Components 2.1
Microsoft Data Access Components 2.5
Microsoft Data Access Components 2.6