programing automatic compact and
Microsoft Office TrainingThe UK's Number 1 for Microsoft Office Training Sitemap add this page to your favourites/bookmarksBookmark page
 
view a printable version of this pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

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

Programing an automatic compact and repair

resolvedResolved · High Priority · Version 2003

replyReplyWed 14 Jan 2009, 17:28Delegate Sarah said...

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

For upcoming training course dates see: Pricing & availability

replyReplyWed 21 Jan 2009, 14:08Trainer Laura said...

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

replyReplyFri 23 Jan 2009, 10:35Delegate Sarah said...

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!

replyReplyThu 14 May 2009, 10:22Trainer Rich said...

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.

 

 

Please browse our web site to find out more about
courses in microsoft access and other Microsoft training courses.

Access tip:

Convert A Form Into A Report

If there is a form that you want to to save as a report:

1. Open that form in Design View
2. Select File and Save As
3. In the Save As Dialog box Select Report

The system creates a report based on the form.

View all Access hints and tips

forum postJust to confirm my training today, no actual question.

» Forum post: Excel


Rate this page:
2.2/5 (213 votes cast)
Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses
Microsoft Certified Partner
Security Seal verified by visa, mastercard securecard

Mini sitemap. These are the main areas of our web site. Full sitemap.

Management training

Professional Skills courses
Project Management Course London
Project Management Courses London
Project Management Training London
Project Management Training
Project Seminar
Project Seminars
Time Management Course London
Time Management London
Time Management Courses London
Time Management Training London
Introduction to Finance course
Assertiveness Skills course
Effective Communications Skills training
Presentation Skills London

Training Formats

Public scheduled courses
On-site training
Closed company courses

Consultancy
Application Development

Blogs

Excel Training
MS Project Training
Microsoft Training Blog

Version differences

Office 2010 vs 2007
MS Project version differences

Training Information

London Computer Training
Computer Training London
Docklands Training Courses
Docklands Training London

Training venues London
Client list
FAQ
Pricing and availability
Course details / Syllabus

Training Articles
Training Information

Microsoft training

Microsoft Office training
& IT Applications

Microsoft Project training
Microsoft Outlook training
Microsoft Powerpoint training
Microsoft Word training
MS Project courses
MS Project training
Outlook courses
PowerPoint courses
PowerPoint training
VBA courses
Word courses
Microsoft.training
(more...)

Excel Training

Excel courses
Excel Training Courses Medway
Autonumber in Excel
Microsoft Excel training
Basic Excel Courses
Basic Excel Course
Basic Excel Training

Interested in MS Access training?

Access courses
Microsoft Access training
Microsoft access courses
Microsoft training access course
Microsoft+access+training
Access courses in london

Training provider

Training providers
IT training companies
IT training providers
Management Training providers
Management Training provider

Event history, feedback results
Events in 2012 · 2011 · 2010 · More

See also

Crystal Reports training