Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Compact database Access97

Posted on 1998-10-01
9
Medium Priority
?
361 Views
Last Modified: 2006-11-17
Is it possible to do a "Compact databse" whith
the VBA code?
So my project comapct itself ex. every month?

0
Comment
Question by:babban
9 Comments
 
LVL 3

Accepted Solution

by:
dapperry earned 30 total points
ID: 1964109
Just use this statement:

DBEngine.CompactDatabase "Source.mdb", "Dest.mdb"

Let me know if you have any ?s

:) D Perry
0
 
LVL 3

Expert Comment

by:dapperry
ID: 1964110
Let me know what methodology you would want for scheduling the compact.  I might use a scheduler and have it done sometime in the middle of the night.  But there are certainly other ways of going about it.

:) D Perry
0
 

Expert Comment

by:load_o_crap
ID: 1964111
u should know that u can't compact the database that u'r currently using.

Unless of course u use the sendkeys command to call up the toolbar Database Utilites and compact database. Then there's that piece of coding missing to rename the compacted database  back to the orginal...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:babban
ID: 1964112
Hm?
dapperry says it could go and load_o_crap says it DON'T??????

0
 
LVL 3

Expert Comment

by:dapperry
ID: 1964113
No, I actually didn't say you could do that with :

DBEngine.CompactDatabase "Source.mdb", "Dest.mdb"

That is only from an outside database.  As far as renaming, I figured you probably knew that much, but for yucks..

DBEngine.CompactDatabase "Source.mdb", "Dest.mdb"
Kill "Source.mdb"
Name "Dest.mdb" As "Source.mdb"

But if you need to do it from within the database, by all means use SendKeys as load_o_crap suggested.

:) D Perry
0
 
LVL 1

Expert Comment

by:mogray
ID: 1964114
Below is a kb artical to use the method dapperry is talking about.

Article ID: Q158937


**********************************************************************************************
The following example uses a Visual Basic procedure to compact one or more
databases, and then to close Microsoft Access when it is finished. You
cannot compact the database that is running the procedure, nor can you

compact any database that you cannot open exclusively. You must have read
and write permissions for the folder where the database you are compacting
resides, and you need enough disk space in that folder to store the
original and the compacted copies of the database.

 1. Create a new blank database called Compact.mdb.

 2. Create the following new table in Design view:

       Table: DBNames
       ----------------------------------------------------
       Field Name: DBID

          Data Type: AutoNumber (or Counter in version 2.0)
       Field Name: DBFolder
          Data Type: Text
          Field Size: 255
       Field Name: DBName
          Data Type: Text
          Field Size 255

       Table Properties: DBNames
       -------------------------
       PrimaryKey: DBID

 3. Save the table as DBNames and close it.

 4. Create a new blank form and set the following properties:

       Caption: Compact Databases
       Default View: Single Form

       Scrollbars: Neither
       RecordSelectors: No
       NavigationButtons: No
       OnTimer: [Event Procedure]
       TimerInterval: 60000

 5. Click the Build button next to the OnTimer property of the form and
    type the following procedure:

    In Microsoft Access 7.0 and 97
    ------------------------------

      Private Sub Form_Timer()
      '==================================================================
      'The Timer event runs this code every minute. It compares your

      'system time with the StartTime variable. When they match, it
      'begins compacting all databases in the DBNames table.
      '==================================================================
      Dim StartTime As String
      ' Set this variable for the time you want compacting to begin.
      StartTime = "12:00 AM"
      ' If StartTime is now, open the DBNames table and start compacting
      If Format(Now(), "medium time") = Format(StartTime, _
              "medium time") Then

         Dim RS As Recordset, DB As DATABASE
         Dim NewDBName As String, DBName As String
         Set DB = CurrentDb()
         Set RS = DB.OpenRecordset("DBNames")
         On Error Resume Next
         RS.MoveFirst
         Do Until RS.EOF
            DBName = RS("DBFolder") & "\" & RS("DBName")
            ' Create a new name for the compacted database.
            ' This example uses the old name plus the current date.
            NewDbName = Left(DbName, Len(DbName) - 4)

            NewDbName = NewDbName & " " & Format(Date, "MMDDYY") & ".mdb"
            DBEngine.CompactDatabase DBName, NewDBName
            RS.MoveNext
         Loop
      ' Close the form, and then close Microsoft Access
      DoCmd.Close acForm, "CompactDB", acSaveYes
      DoCmd.Quit acSaveYes
      End If
      End Sub

    In Microsoft Access 2.0
    -----------------------

    NOTE: In the following sample code, an underscore (_) at the end of a
    line is used as a line-continuation character. Remove the underscore

    from the end of the line when re-creating this code in Access Basic.

      Private Sub Form_Timer()
      '==================================================================
      'The Timer event runs this code every minute. It compares your
      'system time with the StartTime variable. When they match, it
      'begins compacting all databases in the DBNames table.
      '==================================================================
      Dim StartTime As String

      ' Set this variable for the time you want compacting to begin.
      StartTime = "12:00 AM"
      ' If StartTime is now, open the DBNames table and start compacting.
      If Format(Now(), "medium time") = Format(StartTime, _
              "medium time") Then
         Dim RS As Recordset, DB As DATABASE
         Dim NewDBName As String, DBName As String
         Set DB = CurrentDb()
         Set RS = DB.OpenRecordset("DBNames")
         On Error Resume Next

         RS.MoveFirst
         Do Until RS.EOF
            DBName = RS("DBFolder") & "\" & RS("DBName")
            ' Create a new name for the compacted database.
            ' This example uses the DBID plus the current date, which
            ' falls within DOS 8.3 file name limits for DBID = 1 to 99.
            NewDBName = RS("DBFolder") & "\" & RS("DBID") & Format(Date, _
                "MMDDYY") & ".mdb"
            DBEngine.CompactDatabase DBName, NewDBName

            RS.MoveNext
         Loop
      ' Close the form, and then close Microsoft Access.
      DoCmd Close a_Form, "CompactDB"
      DoCmd Quit a_Save
      End If
      End Sub

 6. Save the form as CompactDB and close it.

 7. Create a new macro with the following action:

      Action
      --------
      OpenForm

      Action Arguments
      --------------------
      Form Name: CompactDB
      View: Form
      Data Mode: Read Only
      Window Mode: Normal

 8. Save the macro as AutoExec and close it.

 9. Open the DBNames table and add a record for each database you want to
    compact. Type the full path to the database in the DBFolder field, and
    the name of the database itself in the DBName field. For example:

      DBID   DBFolder                       DBName
      ---------------------------------------------------
         1   C:\MSOffice\Access\Samples     Northwind.mdb
         2   \\Servername\Access\Sampapps   Nwind.mdb

10. Close the database, and then reopen it any time before compacting is
    scheduled to start. The AutoExec macro will automatically open the
    CompactDB form. Leave Microsoft Access running with this form open.
    At the specified time, compacting begins and when the last database is
    done, Microsoft Access closes.



Copyright (c) Microsoft Corporation. All rights reserved.

*****************************************************************************************************

Wayne

0
 

Author Comment

by:babban
ID: 1964115
OK,thanks.
0
 

Author Comment

by:babban
ID: 1964116
Adjusted points to 8
0
 

Author Comment

by:babban
ID: 1964117
Adjusted points to 10
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Implementing simple internal controls in the Microsoft Access application.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question