Compact the database via VBS (1 Viewer)

Number11

Member
Local time
Today, 22:21
Joined
Jan 29, 2020
Messages
607
Does any one have a working method to compact access 365 database from VBS so i can automate it to compact after importing data pls?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 14:21
Joined
Oct 29, 2018
Messages
21,494
You could try using the /compact command line switch when you execute the MSACCESS.EXE app in your VBS. I don't have a sample handy.
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 17:21
Joined
Apr 27, 2015
Messages
6,367
I cant remember where I found this, but this works great...

Code:
' ***************** BEGIN CODE HERE ' *****************

 

    Dim objScript

    Dim objAccess

    Dim strPathToMDB

    Dim strTempDB

    Dim strMsg

 

' ///////////// NOTE: User must edit variables in this section /////

'

' The following line of code is the only variable that need be edited

' You must provide a path to the Access MDB which will be compacted

'

    strPathToMDB = "C:\Your Full Path\YourDB.accdb"

'

' ////////////////////////////////////////////////////////////////

' Set a name and path for a temporary mdb file

 

    strTempDB = "C:\YourFullPath\YourDB_Comp.accdb"

 

    Set objAccess = CreateObject("Access.Application")

 

' Perform the DB Compact into the temp mdb file

' (If there is a problem, then the original mdb is preserved)

 

    objAccess.DBEngine.CompactDatabase strPathToMDB, strTempDB

 

    If Err.Number > 0 Then ' There was an error. Inform the user and halt execution

        strMsg = "The following error was encountered while compacting database:"

        strMsg = strMsg & vbCrLf & vbCrLf & Err.Description

    Else ' Create File System Object to handle file manipulations

        Set objScript = CreateObject("Scripting.FileSystemObject")

    ' Back up the original file as Filename.mdbz. In case of undetermined

    ' error, it can be recovered by simply removing the terminating "z".

        objScript.CopyFile strPathToMDB, strPathToMDB & "z", True

    ' Copy the compacted mdb by into the original file name

        objScript.CopyFile strTempDB, strPathToMDB, True

    ' We are finished with TempDB. Kill it.

        objScript.DeleteFile strTempDB

    End If

 

' Always remember to clean up after yourself

Set objAccess = Nothing

Set objScript = Nothing

'

MSGbox "Done!"

' ****************** END CODE HERE ' ******************
 

bastanu

AWF VIP
Local time
Today, 14:21
Joined
Apr 13, 2010
Messages
1,402
While the script does include backing up the original file I think it is missing a (very) important step; copying the original file first to the temp location (which should always be on a local drive) and actually compacting that not the original. Errors during compacting a large back-end located on a network drive can render it corrupted beyond repair.

Cheers,
 

NauticalGent

Ignore List Poster Boy
Local time
Today, 17:21
Joined
Apr 27, 2015
Messages
6,367
Good point. I might make that change if I ever decide to use this...
 

Users who are viewing this thread

Top Bottom