Wrox Programmer Forums
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old January 30th, 2007, 02:14 PM
Registered User
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Compact Access Database

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()
            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 !

Old February 10th, 2007, 04:10 PM
Authorized User
Join Date: Aug 2006
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to anubisascends

To Compact a database in VB look at this:


Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.
Albert Einstein
US (German-born) physicist (1879 - 1955)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Compact and Repair Access Database raguapk VB How-To 8 May 23rd, 2013 10:00 AM
Compact Access Database tang_moor Access VBA 0 January 30th, 2007 02:38 PM
Compact And Repair Access Database myedu2k4 Pro VB Databases 1 May 25th, 2004 01:43 AM
Compact Access Database lai_ccca Access VBA 0 January 9th, 2004 08:41 PM
compact access database hellobookus Access VBA 2 January 5th, 2004 06:51 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.