programing automatic compact and

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Access VBA Training and help » Programing an automatic compact and repair | Access forum

Programing an automatic compact and repair | Access forum

resolvedResolved · High Priority · Version 2003

Sarah has attended:
Access Introduction course

Programing an automatic compact and repair

I have a Database (Db1) which is the main database I need to compact and repair regularly. Since a number of people regularly work away from the office it has proved impractical to set the database to compact on close since if their connection is slow then it tends to crash rather than compact.

I looked into how to set up an automatic Compact and Repair that would be timed through the Windows Scheduled Tasks function, and executed though another Database (CompactDB) specifically set up with only enough information to open Db1 and run a compact and repair, and the close itself and Db1.

For this I found some VBA code (originally for an older Access verison though) on the web that said it would work - it does seem to work, however it actually creates a NEW compacted Db1 file (named by date), and leaves the original Db1 in an uncompacted state.

I have tried to understand the code to remove the part telling it to create a new file - but I really am at a loss. Here is the code:

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 to compact all databases in the DBNames table.
'==================================================================
Dim StartTime As String
' Set this variable for the time you want compacting to begin.
StartTime = "04:51 PM"
' 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 in 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 acForm, "CompactDB"
DoCmd.CloseDatabase
End If
End Sub


Please let me know how to resolve this - or any other suggestions for an automatic compact and repair welcome!

Thanks,
Sarah

RE: Programing an automatic compact and repair

Hi Sarah

Thank you for your question regarding Compact and Repair.

One solution I have implented before now is to use windows scheduler to execute the following command on the database in question. It opens the database and compacts

"C:\Program Files\Microsoft Office\Office12\msaccess.exe" "C:\My Folder\My Database.accdb" /compact

If the code method is your preference then I suggest you rename the database before you compact. Compact and repair does very occasionally corrupt a database. It is very rare but to be extra safe i would run rename the database using scheduler then run the code to compact.


Please let me know how you get on.


Laura GB

RE: Programing an automatic compact and repair

Thank you for your reply - I have tried to put your suggested command into task scheduler but it comes back to say that it doesn't understand the command? (I have amended it to suit my paths, not just copied and pasted)

Also - how do I make the code that I pasted previously, compact the DB without creating a new re-named file? Our system and DB is backed up every hour and hence (I am told) that I do not need to worry about creating a back up before a compact and repair process.

Thanks!

RE: Programing an automatic compact and repair

Sarah,

You may need to adjust the location of Access in the command too.

ie. C:\Program Files\Microsoft Office\Office12\msaccess.exe might not be where Access is stored on your computer. That might be why the command isn't working...

Regards, Rich

Wed 20 May 2009: Automatically marked as resolved.

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Access tip:

Prefixed Autonumbering

When using Date Type = Autonumber as a primary key, clients may require autonumbering to be prefixed with a letter.

For example, P001, where P is the prefixed letter

Instructions
Step 1. Create Field Name
Step 2. Set Data Type to Autonumber
Step 3. Within Field Property General tabsheet,
enter Format property as [backslash]P000



View all Access hints and tips


Server loaded in 0.08 secs.