Solved

Packing Access Database

Posted on 2001-08-03
5
155 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
  • 3
5 Comments
 
LVL 22

Expert Comment

by:mnasman
Comment Utility
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:
mnasman earned 50 total points
Comment Utility
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
Comment Utility


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:mnasman
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now