The following functions are used to compress the Access database needs to increase the COMOBJ unit.
Function CompactDatabase (AfileName, Apassword: String): Boolean;
// Compressed and repair the database, overwrite the source file
Const
Sconnectionstring = 'provider = microsoft.jet.Oledb.4.0; data source =% s;'
'Jet OLEDB: Database Password =% S;';
VAR
Spath, sfile: array [0..254] of char;
STEMPFILENAME: STRING;
JEVARIANT;
Begin
GetTemppath (40, spath); // get the TEMP path of Windows
GetTempFileName (Spath, '~ cp', 0, sfile); // get the TEMP file name, Windows will automatically establish 0 byte files
StempFileName: = sfile; // pchar-> string
Deletefile (STEMPFILENAME); / / Delete 0 byte files established by Windows
Try
JE: = CREATEOLOBJECT ('jro.jetengine'); // Establish an OLE object, the function ends OLE object exceeds the scope automatic release
Olecheck (Je.CompactDatabase (Format (Sconnectionstring, [AfileName, Apassword]),
Format (Sconnectionstring, [STEMPFILENAME, APASSWORD])))); // Compressed database
// Copy and override the source database file. If the copy fails, the function returns a false, compressed, but does not meet the function.
Result: = CopyFile (Pchar (StempFileName), Pchar (AfileName), FALSE;
Deletefile (STEMPFILENAME); / / Delete temporary files
Except
Result: = false; // Compression failed
END;
END;