CompactDatabase Method

See Also      Applies To

Copies and compacts a closed database, and gives you the option of changing its version, collating order, and encryption. (Microsoft Jet workspaces only).

Syntax

VOIDCompactDatabase(LPCTSTR pstrOldDatabase,

LPCTSTR pstrNewDatabase,

LPCTSTR pstrDstConnect = NULL,

LONG lOptions = -1,

LPCTSTR pstrSrcConnect = NULL);

Parameters

Type Argument Description
LPCTSTR pstrOldDatabase A pointer to a string that identifies an existing, closed database. It can be a full path and file name, such as "C:\db1.mdb". If the file name has an extension, you must specify it. If your network supports it, you can also specify a network path, such as "\\server1\share1\dir1\db1.mdb".
LPCTSTR pstrNewDatabase A pointer to a string that is the file name (and path) of the compacted database that you are creating. You can also specify a network path. You can't use the pstrNewDatabase argument to specify the same database file as pstrOldDatabase.
LPCTSTR pstrDstConnect Optional. A pointer to a string composed of zero or more parameters separated by semicolons. This parameter is used to pass additional information to the destination database as needed. For example, collating order is specified here.
LONG Ioptions Optional. A constant or combination of constants that indicates one or more options. The options specify the version of its data format and whether the new compacted database should be encrypted.
LPCTSTR pstrSrcConnect Optional. A pointer to a string composed of zero or more parameters separated by semicolons. This parameter is used to pass additional information from the source as needed.

Settings

You can concatenate one of the following constants in the pstrDstConnect argument to specify the CollatingOrder for string comparisons of text in the compacted database.

Constant Collating order
dbLangGeneral English, German, French, Portuguese, Italian, and Modern Spanish
dbLangArabic Arabic
dbLangChineseSimplified Simplified Chinese
dbLangChineseTraditional Traditional Chinese
dbLangCyrillic Russian
dbLangCzech Czech
dbLangDutch Dutch
dbLangGreek Greek
dbLangHebrew Hebrew
dbLangHungarian Hungarian
dbLangIcelandic Icelandic
dbLangJapanese Japanese
dbLangKorean Korean
dbLangNordic Nordic languages (Microsoft Jet database engine version 1.0 only)
dbLangNorwDan Norwegian and Danish
dbLangPolish Polish
dbLangSlovenian Slovenian
dbLangSpanish Traditional Spanish
dbLangSwedFin Swedish and Finnish
dbLangThai Thai
dbLangTurkish Turkish

You can use one of the following constants in the lOptions argument to specify whether to encrypt or to decrypt the database while it's compacted.

Constant Description
dbEncrypt Encrypt the database while compacting.
DbDecrypt Decrypt the database while compacting.

If you omit an encryption constant or if you include both dbDecrypt and dbEncrypt, pstrNewDatabase will have the same encryption as pstrOldDatabase.

You can use one of the following constants in the lOptions argument to specify the version of the data format for the compacted database. This constant affects only the version of the data format of pstrNewDatabase and doesn't affect the version of any Microsoft Access-defined objects, such as forms and reports.

Constant Description
dbVersion10 Creates a database that uses the Microsoft Jet database engine version 1.0 file format while compacting.
dbVersion11 Creates a database that uses the Microsoft Jet database engine version 1.1 file format while compacting.
dbVersion20 Creates a database that uses the Microsoft Jet database engine version 2.0 file format while compacting.
dbVersion30 Creates a database that uses the Microsoft Jet database engine version 3.0 file format (compatible with version 3.5) while compacting.

You can specify only one version constant. If you omit a version constant, pstrNewDatabase will have the same version as pstrOldDatabase. You can compact pstrNewDatabase only to a version that is the same or later than that of pstrOldDatabase.

Remarks

As you change data in a database, the database file can become fragmented and use more disk space than is necessary. Periodically, you can use the CompactDatabase method to compact your database to defragment the database file. The compacted database is usually smaller and often runs faster. You can also change the collating order, the encryption, or the version of the data format while you copy and compact the database.

You must close pstrOldDatabase before you compact it. In a multiuser environment, other users can't have pstrOldDatabase open while you are compacting it. If pstrOldDatabase isn't closed or isn't available for exclusive use, an error occurs.

Because CompactDatabase creates a copy of the database, you must have enough disk space for both the original and the duplicate databases. The compact operation fails if there isn't enough disk space available. The pstrNewDatabase duplicate database doesn't have to be on the same disk as pstrOldDatabase. After successfully compacting a database, you can delete the pstrOldDatabase file and rename the compacted pstrNewDatabase file to the original file name.

The CompactDatabase method copies all the data and the security permission settings from the database specified by pstrOldDatabase to the database specified by pstrNewDatabase.

If you use CompactDatabase to convert a version 1.x database to version 2.5 or 3.x, only applications using version Microsoft Jet 2.5 or 3.x can open the converted database.

Note In an ODBCDirect workspace, using the CompactDatabase method doesn't return an error, but instead loads the Microsoft Jet database engine into memory.

Caution Because the CompactDatabase method doesn't convert Microsoft Access objects, you shouldn't use CompactDatabase to convert a database containing such objects.

Usage

#include <afxole.h>
#include <dbdao.h>
...
CdbDBEngine   dben;
...
dben.CompactDatabase(   _T("Northwind.mdb"), _T("Nwind2.mdb"),
                     dbLangGeneral, // This is really a string.
                     dbVersion30);