Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

MS Access 2003/2007/2010/2016 Compact and Repair

VB.NET Compact & Repair Database – MS Access
VB.NET Compact & Repair Database – MS Access
Neither ADO nor ADO .NET give you a built-in Compact/Repair—use the JRO JetEngine COM library (MDAC 2.x) or the DAO interop for ACCDB files. ADO .NET can consume these via the Interop layer.

Requirements

  • VB .NET (any IDE: VS 2010+)
  • For *.mdb: Microsoft Jet and Replication Objects 2.x (COM reference)
  • For *.accdb: Microsoft.Office.Interop.Access.Dao (NET reference)

Setup Steps

  1. Create a Console (or WinForms) VB .NET project.
  2. Right-click References → Add Reference:
    • MDB: COM → “Microsoft Jet and Replication Objects 2.x Library”
    • ACCDB: .NET → “Microsoft.Office.Interop.Access.Dao”
  3. Accept the wrapper generation if prompted.
  4. Open your Module1.vb, remove default code, and paste one of these examples:
'--- Compact 2000–2003 MDB via JRO
Module Module1
  Sub Main()
    Dim jro As New JRO.JetEngine()
    jro.CompactDatabase(
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Old.mdb",
      "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\New.mdb;Jet OLEDB:Engine Type=5")
    MsgBox("Database compacted successfully!")
  End Sub
End Module
'--- Compact 2007+ ACCDB via DAO
Module Module1
  Sub Main()
    Dim engine = New Microsoft.Office.Interop.Access.Dao.DBEngine()
    Dim tmp = "C:\Temp\New.accdb"
    engine.CompactDatabase(
      "C:\MyData.accdb",
      tmp, , , ";pwd=YourDbPassword")
    My.Computer.FileSystem.DeleteFile("C:\MyData.accdb")
    Rename(tmp, "C:\MyData.accdb")
    MsgBox("ACCDB compacted successfully!")
  End Sub
End Module

Pitfalls & Best Practices

  • The engine needs exclusive access. Wrap in Try…Catch to handle “file in use” errors.
  • Always compact to a new file, then swap—never run against the live database.
  • Schedule off-hour runs (Task Scheduler) to avoid downtime.

Update April 2020

After upgrading to Office 2016, ensure you reference the v15.0 DAO interop DLL (not the older v12.0). If you still see v12.0, download the Office 2016 Database Engine redistributable from Microsoft and re-add the reference.

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]