novicedelphidev
asked on
Deleteing, Compacting, & Repairing
I would like to empty certain tables, and perform a compact & repair on them.
Reason being is I need empty the tables and reset the auto index values.
I am using Delphi 7.0 + ADO + MS ACCESS
What is the best method of doing this via runtime (code)?
Sample code please!
I will start this off at 100 points, but will increase it significantly for a good solution!
Reason being is I need empty the tables and reset the auto index values.
I am using Delphi 7.0 + ADO + MS ACCESS
What is the best method of doing this via runtime (code)?
Sample code please!
I will start this off at 100 points, but will increase it significantly for a good solution!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Easier said then done. The database has 6 tables with Referential integrity
Master Detail/detail/detail
I need a solution to empty the tables and then pack/repair
I am increasing the points to 500
Master Detail/detail/detail
I need a solution to empty the tables and then pack/repair
I am increasing the points to 500
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
novicedelphidev,
you could create a completely new database via runtime instead of > empty the tables and reset the auto index values
Are you agree ?
you could create a completely new database via runtime instead of > empty the tables and reset the auto index values
Are you agree ?
I have a working example about creating a new Access database at runtime. If you need I will provide it to you ....
Hi
OldDB:= ChangeFileExt(Application.
NewDB:= ChangeFileExt(Application.
Shouldn't these be different names.........
Also, compacting and repairing a database will not reset any auto increments or remove any data, all they do is free up space allocated to deleted records.
Also does referential integrity work outside of the Access IDE?
>> If your tables have referential integrity then you should empty it using bottom up approach.
This is correct.......
Voodooman
Hi
>> AtulParmar
Is incorrect in suggesting you use DAO as you clearly say that you are using the ADO.
Voodooman
This code will do the trick in my app.
Code:
function TfrmMain.CompactAndRepair( sOldMDB, sNewMDB: String): Boolean;
const
sProvider = 'Provider=Microsoft.Jet.OL EDB.4.0;';
var
oJetEng : JetEngine;
begin
sOldMDB := sProvider + 'Data Source=' + sOldMDB;
sNewMDB := sProvider + 'Data Source=' + sNewMDB;
try
oJetEng := CoJetEngine.Create;
oJetEng.CompactDatabase(sO ldMDB,sNew MDB);
oJetEng := Nil;
Result := True;
except
oJetEng := Nil;
Result := False;
end;
end;
Code:
function TfrmMain.CompactAndRepair(
const
sProvider = 'Provider=Microsoft.Jet.OL
var
oJetEng : JetEngine;
begin
sOldMDB := sProvider + 'Data Source=' + sOldMDB;
sNewMDB := sProvider + 'Data Source=' + sNewMDB;
try
oJetEng := CoJetEngine.Create;
oJetEng.CompactDatabase(sO
oJetEng := Nil;
Result := True;
except
oJetEng := Nil;
Result := False;
end;
end;
To empty your table(s) with Parent/Child/etc. structure...
Assign a referential delete constraint to them so you are allowed to delete the parent and all related children will be removed automatically.
Assign a referential delete constraint to them so you are allowed to delete the parent and all related children will be removed automatically.
Hi,
I created an application to run queries in a Access database and then compact and repair when it is finished.
I have Delphi 7 Enterprise and thus have the servers component tab. I use a TAccessAplication component in my app.
Here is the code with the Database name as Input.
procedure TForm1.CompactRepairExecut e(dbname : string);
var curs : TCursor;
begin
curs := forms.Screen.Cursor;
forms.Screen.Cursor := crHourGlass;
busy := true;
try
ForceDirectories('c:\temp' );
if AccessApplication1.Compact Repair(dbn ame,'c:\te mp\CandR.m db',true)
then
begin
DeleteFile(dbname);
MoveFile(pchar('c:\temp\Ca ndR.mdb'), pchar(dbna me));
DeleteFile('c:\temp');
end
else
ShowMessage('Error Compacting File');
finally
forms.Screen.Cursor := curs;
end;
end;
Hope It Helps
I created an application to run queries in a Access database and then compact and repair when it is finished.
I have Delphi 7 Enterprise and thus have the servers component tab. I use a TAccessAplication component in my app.
Here is the code with the Database name as Input.
procedure TForm1.CompactRepairExecut
var curs : TCursor;
begin
curs := forms.Screen.Cursor;
forms.Screen.Cursor := crHourGlass;
busy := true;
try
ForceDirectories('c:\temp'
if AccessApplication1.Compact
then
begin
DeleteFile(dbname);
MoveFile(pchar('c:\temp\Ca
DeleteFile('c:\temp');
end
else
ShowMessage('Error Compacting File');
finally
forms.Screen.Cursor := curs;
end;
end;
Hope It Helps
ASKER
(Although it compiles and I can call it, I just keep getting the 'Repair not successful.' & 'Compression not successful' messages)
function SQLConfigDataSource(hwndPa
lpszAttributes: LPCSTR): BOOL; stdcall; external'ODBCCP32.DLL';
procedure TfrmData.DatabasePack;
var OldDB,NewDB: string;
const ODBC_ADD_DSN=1;
begin
{compress and repair the database}
with dbMain do
begin
dbMain.Close;
OldDB:= ChangeFileExt(Application.
NewDB:= ChangeFileExt(Application.
if not SQLConfigDataSource(0,ODBC
PChar('REPAIR_DB='+OldDB+#
MessageDlg('Repair not successful.',mtInformation
else MessageDlg('Repair successful.',mtInformation
if not SQLConfigDataSource(0,ODBC
PChar('COMPACT_DB='+OldDB+
MessageDlg('Compression not successful.',mtInformation
else MessageDlg('Compression successful.',mtInformation
dbMain.Open;
end;
end;