Solved

Packing Access Database

Posted on 2001-08-03
5
163 Views
Last Modified: 2010-04-06
I know I've seen this before, but the Search function in EE has been going haywire and I'm getting timeouts. So I'm asking again, how do I pack an Access database?
0
Comment
Question by:DragonSlayer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6349774
Hello

 Here's what you asked for :)

uses ComObj;
procedure TForm1.Button1Click(Sender: TObject);
var
  dao: OLEVariant;
begin
  dao := CreateOleObject('DAO.DBEngine.35');
  dao.CompactDatabase('C:\My Documents\DB1.mdb',
'C:\My Documents\CompactedDB1.mdb');
end;

Best regards
Mohammed Nasman
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 50 total points
ID: 6349786
also here's how to compact access 2000 database, I didn't test it cuz i'm home and i dont' have access 2000 in home

//=======

Function CompactAndRepair(sOldMDB : String; sNewMDB : String) : Boolean;
const
         sProvider = 'Provider=Microsoft.Jet.OLEDB.4.0;';
var
         oJetEng   : JetEngine;
begin
         sOldMDB := sProvider + 'Data Source=' + sOldMDB;
         sNewMDB := sProvider + 'Data Source=' + sNewMDB;

         try
            oJetEng := CoJetEngine.Create;
            oJetEng.CompactDatabase(sOldMDB, sNewMDB);
            oJetEng := Nil;
            Result  := True;
         except
            oJetEng := Nil;
            Result  := False;
         end;
end;


Example :

if CompactAndRepair('e:\Old.mdb', 'e:\New.mdb') then
   ShowMessage('Successfully')
else
   ShowMessage('Error?');

Important Notes:
1- Include the JRO_TLB unit in your uses clause.
2- Nobody should use or open the database during compacting.
3- If the compiler gives you an error on the JRO_TLB unit follow these steps:
a) Using the Delphi IDE go to Project ? Import Type Library.
b) Scroll down until you reach ?Microsoft Jet and Replication Objects 2.1 Library?.
c) Click on Install button.
d) Recompile a gain.


Mohammed
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6349880


implementation

{$R *.DFM}

   const
  ODBC_ADD_DSN = 1;

  function SQLConfigDataSource(hwndParent: HWND; fRequest: WORD; lpszDriver: LPCSTR;
  lpszAttributes: LPCSTR): BOOL; stdcall; external 'ODBCCP32.DLL';


procedure CompactMDB;
var
  sCompactDB, sCompactedDB: string;
 
begin
  sCompactDB := 'C:\DataBaseDir\TheDataBase.mdb';
  sCompactedDB := 'C:\DataBaseDir\CompactedTheDataBase.mdb';
  if not SQLConfigDataSource(0, ODBC_ADD_DSN,
    'Microsoft Access Driver (*.mdb)', PChar(
    'COMPACT_DB=' + sCompactDB + ' ' + sCompactedDB + ' General'#0)) then
     ShowMessage('Problems with compacting ' + sCompactDB + ' could not compact');
end;

end.


The Crazy One
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6351055
Hello DragonSlayer

  I test the second code I gave to you, it's work fine with Acess2000 and AccessXP (2002), so now you can Compact anydatabase with it :)

Mohammed
0
 
LVL 14

Author Comment

by:DragonSlayer
ID: 6351145
Thanks mnasman!

Sorry crazyone, but mnasman's a tad bit faster... and it doesn't require linking to external DLL.

mnasman, how do I know whether or not the database is currently opened by other programmes, because I open it as ShareReadWrite.
Thanks again.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question