Ozgrid Excel Help Forums & Excel Best Practices


XL Templates | XL Add-ins | XL Training | XL Estimating | XL Scheduling | XL Recovery | XL Trading | XL Financial | XL Conversion | XL Charting


<
+ Reply to Thread
Results 1 to 1 of 1

Thread: Access Automation – VBA compact current database

  1. #1
    Join Date
    14th January 2005
    Location
    Cyprus
    Posts
    2,241

    Access Automation – VBA compact current database

    For some time on this forum we have assumed that the only VBA way to compact and repair an Access database is from outside the current db. With Access often you need to run a compact as a daily routine which meant that you needed to have code somewhere, which compacted the first db to a second db, deleted the first, then renamed the second to the first.

    This code (source vangelos) compacts the current db with one line.
    Code:
    Public Sub CompactDB()
    CommandBars("Menu Bar").Controls("Tools").Controls("Database utilities").Controls("Compact and repair database...").accDoDefaultAction
    End Sub
    The below code (and attached) performs the old operation but also takes five rolling backups of the database.

    This db is only used as a VBA holder there is no data in it. If you download the file open it with the shift key held down to stop it running automatically. It contains a form which is set as the start form. The form has an on open event that runs a macro and the macro runs a defined function that runs the code. You don’t need the macro bit but hopefully it demonstrates the possibilities. You can use this with a simple Windows Schedule that opens the db (the run line in Windows just needs the path to the db).

    Code:
    Sub CompactDbase()
         ' Requires a reference to be set to
         ' "Microsoft Jet & Replication Objects Library 2.x"
         ' (Tools > References and check the reference)
         ' Requires a form called "startup" with a label called "ProgressLabel"
        On Error GoTo ErrHandler
        
        ' Inform user of progress
        Form_StartUp.ProgressLabel.Caption = "Starting Process"
        DoEvents
        
        ' dimension variables and set
        Dim DbaseDir As String
        Dim BackupDir As String
        Dim DbaseName As String
        Dim DbaseTempName As String
        Dim DbaseBackupName As String
        Dim Dbasebk1 As String
        Dim Dbasebk2 As String
        Dim Dbasebk3 As String
        Dim Dbasebk4 As String
        Dim Dbasebk5 As String
        
        ' CHANGE SETTINGS IN THIS SECTION TO REUSE THIS PROGRAM
        ' ###########################################################
        DbaseDir = "C:\Database\"
        BackupDir = "C:\DbaseBackup\"
        DbaseName = "XXX.mdb"
        DbaseTempName = "XXXTemp.mdb"
        DbaseBackupName = "XXXBackup.mdb"
        Dbasebk1 = "XXX1.mdb"
        Dbasebk2 = "XXX2.mdb"
        Dbasebk3 = "XXX3.mdb"
        Dbasebk4 = "XXX4.mdb"
        Dbasebk5 = "XXX5.mdb"
        ' ###########################################################
        Form_StartUp.ProgressLabel.Caption = "Initial Backup started. " & DbaseBackupName
        DoEvents
        ' Perform Backup before starting
        FileCopy DbaseDir & DbaseName, _
            DbaseDir & DbaseBackupName
        
        Dim je As New JRO.JetEngine
        
        ' Kill previous Temp file
        If Dir(DbaseDir & DbaseTempName) <> "" Then _
            Kill DbaseDir & DbaseTempName
         
         ' Compact dbase to dbaseTemp
        Form_StartUp.ProgressLabel.Caption = "Compacting started. " & DbaseTempName
        DoEvents
        je.CompactDatabase _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & DbaseDir & DbaseName, _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & DbaseDir & DbaseTempName & ";"
        
        ' Delete dbase
        Form_StartUp.ProgressLabel.Caption = "Replacing compact file " & DbaseName
        DoEvents
        Kill DbaseDir & DbaseName
        
        ' Rename dbaseTemp (now compacted) to dbase
        Name DbaseDir & DbaseTempName _
            As DbaseDir & DbaseName
    
        ' Resuffle backups and backup current
        Form_StartUp.ProgressLabel.Caption = "Resuffling backups " & Dbasebk5
        DoEvents
        If Dir(BackupDir & Dbasebk5) <> "" Then
            Kill BackupDir & Dbasebk5
        End If
        Form_StartUp.ProgressLabel.Caption = "Resuffling backups " & Dbasebk4
        DoEvents
        If Dir(BackupDir & Dbasebk4) <> "" Then
            Name BackupDir & Dbasebk4 _
            As BackupDir & Dbasebk5
        End If
        Form_StartUp.ProgressLabel.Caption = "Resuffling backups " & Dbasebk3
        DoEvents
        If Dir(BackupDir & Dbasebk3) <> "" Then
            Name BackupDir & Dbasebk3 _
            As BackupDir & Dbasebk4
        End If
        Form_StartUp.ProgressLabel.Caption = "Resuffling backups " & Dbasebk2
        DoEvents
        If Dir(BackupDir & Dbasebk2) <> "" Then
            Name BackupDir & Dbasebk2 _
            As BackupDir & Dbasebk3
        End If
        Form_StartUp.ProgressLabel.Caption = "Resuffling backups " & Dbasebk1
        DoEvents
        If Dir(BackupDir & Dbasebk1) <> "" Then
            Name BackupDir & Dbasebk1 _
            As BackupDir & Dbasebk2
        End If
    
            
        FileCopy DbaseDir & DbaseName, _
            BackupDir & Dbasebk1
         Form_StartUp.ProgressLabel.Caption = "Process info"
         DoEvents
        Exit Sub
        
    ErrHandler:
    MsgBox ("Error message here - Your first time ? Did you forget to hold down shift key ?")
    End Sub
    
    Public Function CompactBackupdbase()
    ' Allows sub to be visible from macros
    Call CompactDbase
    End Function

    With the new command (compact the current database) you would just need to create a macro to run the one line of code. To run the macro directly from Windows Scheduler you need a more complex run line in Windows eg.

    Code:
    "C:\Program Files\Microsoft Office\Office10\MSACCESS.EXE" E:\Database\YourDatabaseName.mdb /x Combined YourMacroName
    Carl
    Attached Files. REMINDER! OzGrid accepts no responsibility for ANY adverse effects as a result from downloading attached files. ALWAYS run an up-to-date virus scan and disable macros.
    Last edited by carlmack; June 25th, 2008 at 04:27.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Possible Answers

  1. Link an Access 2007 database to a database that contains data access pages
    By Dave Hawley in forum Access RSS Feed Discussions
    Replies: 0
    Last Post: March 25th, 2008, 09:31
  2. Help prevent and correct database file problems by using Compact and Repair
    By Dave Hawley in forum Access RSS Feed Discussions
    Replies: 0
    Last Post: March 25th, 2008, 09:31
  3. Compact A Closed Database From Another Database
    By SerenityNetwork in forum Excel and/or Access Help
    Replies: 15
    Last Post: October 18th, 2006, 22:50
  4. code to compact and repair access 97 mdb
    By mjschukas in forum Excel and/or Access Help
    Replies: 3
    Last Post: September 11th, 2005, 08:22
  5. Compact Database via VBA
    By summer brew in forum Excel and/or Access Help
    Replies: 5
    Last Post: July 12th, 2005, 22:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts