Compact An Access Database From VB

This code will show you how to compact an access database (As you can do from Microsoft Access Application) from Visual Basic using code.

Preparations

Add reference to DAO:
from the menu choose Project-> References, mark the Microsoft DAO 3.6 (or 3.51) Object Library check box, and press OK.

Add 1 Command Button to your form.
Press the button to compact the database.

If you got "Unrecognized Database format" error message:

If you using database that made in Access 2000 and you don't have Microsoft DAO 3.6 Object Library reference, click on browse, and select the file C:\Program Files\Common Files\Microsoft Shared\Dao\dao360.dll (If you have Access 2000 installed in your computer you have this file.)
This will add Microsoft DAO 3.6 Object Library reference to your project. Now mark it and press OK.

Form Code

Private Sub Command1_Click()
' this line will compact the "c:\myDir\db1.mdb" database to "c:\myDir\db2.mdb".
' after this line had been called you will have the original
' uncompacted database in "c:\myDir\db1.mdb" 
' and the new compacted database in "c:\myDir\db2.mdb".
   
DBEngine.CompactDatabase "c:\myDir\db1.mdb", "c:\myDir\db2.mdb"
End Sub

Go Back