?
Solved

Packing Access Database

Posted on 2001-08-03
5
Medium Priority
?
176 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 200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month9 days, 4 hours left to enroll

764 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