access 数据库长期使用后,经过多次的插入和删除操作,数据库会变得很大,以至于经常被毁坏而打不开。请问如何压缩和修复access数据库,是通过CompactDatabase么,

解决方案 »

  1.   

    '安装Microsoft ActiveX Data Objects 2.X library  (X为1以上)
    '引用Microsoft Jet and Replication Objects 2.X library
    Dim jro As jro.JetEngine
    Set jro = New jro.JetEngine
    jro.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test\test.mdb", _
    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\test\new.mdb;"
      

  2.   

    Sub CompactDatabaseX()   Dim dbsNorthwind As Database   Set dbsNorthwind = OpenDatabase("Northwind.mdb")   ' Show the properties of the original database.
       With dbsNorthwind
          Debug.Print .Name & ", version " & .Version
          Debug.Print "  CollatingOrder = " & .CollatingOrder
          .Close
       End With   ' Make sure there isn't already a file with the 
       ' name of the compacted database.
       If Dir("NwindKorean.mdb") <> "" Then _
          Kill "NwindKorean.mdb"   ' This statement creates a compact version of the
       ' Northwind database that uses a Korean language
       ' collating order.
       DBEngine.CompactDatabase "Northwind.mdb", _
          "NwindKorean.mdb", dbLangKorean   Set dbsNorthwind = OpenDatabase("NwindKorean.mdb")   ' Show the properties of the compacted database.
       With dbsNorthwind
          Debug.Print .Name & ", version " & .Version
          Debug.Print "  CollatingOrder = " & .CollatingOrder
          .Close
       End WithEnd Sub