I have a large access database that has to be compacted frequently. I have tried to use the compact database functionality available in access however it takes too long to do it this way. What i would like to have is another access database that can compact the larger database. I have tried using visual basic code to do this, it didn't seem to work. I kept getting a pop-up that asked me for a macro. My knowledge of
VB is quite limited, can anyone help me out with this.
This is what i tried to do:
1. Created a new database
2. Created a new module
3. Pasted in following code:
Sub Main()
Try
Dim File_Path, compact_file As String
'Original file path that u want to compact
File_Path = AppDomain.CurrentDomain.BaseDirectory & "E:\new finance backup(hold shift).mdb"
'compact file path, a temp file
compact_file = AppDomain.CurrentDomain.BaseDirectory & "E:\test_1.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
End Sub
4. I seem to be obtaining compile errors.
Any help will be appreciated !