Di bawah ini merupakan fungsi untuk mengcompact dan merepair database Microsoft Access. Kegunaan Compact And Repair database untuk menghilangkan secara permanen data-data yang terhapus. Bagaimana Fungsi mengenai Compact And Repair Database MS Access.
Option Explicit
Private Function CompactDB(Filename As String) As Boolean
On Error GoTo ErrHandler
Dim DC As New DBEngine
Screen.MousePointer = vbHourglass
DC.CompactDatabase Filename, App.Path & "\~database.tmp"
Kill Filename
Name App.Path & "\~database.tmp" As Filename
Screen.MousePointer = vbDefault
CompactDB = True
Exit Function
ErrHandler:
CompactDB = False
End Function
Contoh penggunaan Compact and Repair Database:
Private Sub Command1_Click()
MsgBox CompactDB(App.Path & "\database.mdb") 'true jika database sukses di compact dan repair
End Sub