Tuesday, July 21, 2009

Compact and Repair Access database

Compact and Repair Access database

step 1 : Add reference of dll

"\Microsoft Visual Studio 9.0\Visual Studio Tools for Office\PIA\Office12\dao.dll"

Step 2 : In Button Click event write following code

Try
Dim File_Path, compact_file As String
'Original file path that u want to compact
Dim str As String = ConfigurationSettings.AppSettings("dbname")
Dim _connection_string As String = System.Windows.Forms.Application.StartupPath() & "" & str

File_Path = _connection_string
'compact file path, a temp file
compact_file = AppDomain.CurrentDomain.BaseDirectory & "db1.mdb"
'First check the file u want to compact exists or not
If File.Exists(File_Path) Then
Dim db As New dao.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
Catch ex As Exception
MsgBox(ex.Message)
End Try



File_path = " Main Database path"

Compact_file="Temp Database path for compact"

No comments: