Sobat Campus VB Pernahkah gak sih.. mengalami kejadian seperti ini, contohnya ketika menghapus isi tabel pada database access yang ukuran filenya besar tetapi setelah dihapus ukuran filenya tetap besar?
Kalo pernah pasti tahu donk bagaimana mengembalikan ukuran filenya agar lebih kecil? yap.. jawabannya pasti Compact and Repair kan. hehehe..
Menu Compact and Repair memang sudah ada di Microsoft Office Access. Tampilannya seperti ini :
![]() |
| Menu Compact And Repair di Microsoft Office Access 2013 |
Ketika kita hapus isi tabel pada database Access ukurannya file databasenya tidak akan kembali seperti semula karena ada history yang masih tersimpan pada database tersebut. Oleh sebab itulah kenapa ukuran databasenya masih sama ketika isi tabelnya dihapus. Kita harus melakukan Compact and Repair database tersebut agar ukuran filenya berubah menjadi kecil tetapi bagaimana caranya melakukan Compact and Repair menggunan Visual Studio .Net.
Langkah yang pertama buatlah Project baru di Visula Studio 2017 dan buatlah tampilan seperti berikut ini :
Setelah selesai membuat tampilan programmnya, copy dan paste code berikut ini :
Imports System.IO
Imports Microsoft.Office.Interop.Access.Dao
Public Class Form1
Private Sub CompactAndRepair()
Try
Dim File_Path, compact_file As String
'Original file path that u want to compact
File_Path = AppDomain.CurrentDomain.BaseDirectory & "db.accdb"
'compact file path, a temp file
compact_file = AppDomain.CurrentDomain.BaseDirectory & "db1.accdb"
'First check the file u want to compact exists or not
If File.Exists(File_Path) Then
Dim db As New DBEngine()
'CompactDatabase has two parameters, creates a copy of
'compact DB at the Destination path
db.CompactDatabase(File_Path, compact_file)
End If
'restore the original file from the compacted file
If File.Exists(compact_file) Then
File.Delete(File_Path)
File.Move(compact_file, File_Path)
End If
MsgBox("Compact and Repair Database success!")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CompactAndRepair()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
TextBox1.Text = AppDomain.CurrentDomain.BaseDirectory & "db.accdb"
End Sub
End Class
Tambahkan Referensi : Microsoft.office.interop.access.dao versi 12.00 ke atas
Selesai, selamat mencoba.. Semoga bermanfaat!!!
Selesai, selamat mencoba.. Semoga bermanfaat!!!


EmoticonEmoticon