Discussion Locked

auto compact and renaming .mdb

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I know you can create a compact.mdb to compact mdb's at a specific time automatically. I would like to have the compacting run automaically and then rename the database to it's original name (also automatically). Like to run this at night when no one is around.

5 replies

by dklippert

Here's a BAT file from the forums:

How about writing a simple DOS batch file?

rem MYBACKUP.BAT by Don Leverton/Trevor Best
cls
echo off
rem ********************************************
rem This is a batch file that "backs up"
rem yesterday's MYFILE.MDB
rem by repairing, then compacting, (and renaming
rem in a different directory)
rem and then opens it again today.
rem You actually get 2 days worth of backup...
rem One copy in c:\backup, another in c:\recycled
rem Note that youwill have to create an icon
rem (shortcut) that runs
rem this batch file, and ALWAYS open MYFILE.MDB
rem using this Batch File.
rem **************************************
If Exist C:\BACKUP\BACKUP.MDB MOVE C:\BACKUP\BACKUP.MDB C:\RECYCLED
rem (used MOVE above because it does not prompt
rem you about overwriting)
c:\msaccess.exe c:\access\myfile.mdb /repair/compact c:\backup\backup.mdb
C:\ACCESS\MSACCESS.EXE MYFILE.MDB
exit
rem **********************************************

by c1980

You could create an EXE using VB and use the following code to a sub proceedure (a very simple process):

'Delete the previous back-up, if one exits (Could use API function to send it to the bin instead)
On Error Resume Next
Kill sPath & sBakName

On Error GoTo ErrHandler

'Make a new back-up of the MDB with the name in the sBakName variable (in case something goes wrong)
FileCopy sPath & sDBName,sPath & sBakName

'Compact the MDB into the name in the sCmptName variable
DBEngine.CompactDatabase sPath & sDBName, sCmptName, , ,sPass

'Delete the orig. MDB '(Could use API function to copy over instead)
Kill sPath & sDBName

'Rename the compacted MDB back to the orig. name
Name sCmptName As sPath & sDBName

ExitProceedure:
Exit Function

ErrHandler:
'Do something with the errors
resume ExitProceedure

This discussion is closed to new replies.