Compacting databases

  • Thread starter Thread starter Mark
  • Start date Start date
M

Mark

Does someone have a code snippet they would share for
compacting a db other than the one I am currently working
in for Access 97?

Thanks in advance.

Have a Happy Holidays.

Mark
 
Mark,

Try this code. I typed some of it without testing so make sure to manually
backup test it before implementing on production systems.

Function Compact(stDataFile as String) as Boolean

'This function will backup and compact the mdb file given by the stDataFile
path.
'Manually backup and test debug it first!

Dim stOutFile as String, stBackUpFile as String

stOutFile = Left$(stDataFile, Len(stDataFile) - 4) & ".CMP"

DoCmd.SetWarnings False
DoCmd.Hourglass True

'Delete Temporary OutputFile if exists
On Error Resume Next
Kill stOutFile
On Error Goto Err_Function

'Backup
stBackUpFile = Left(stDataFile, Len(stDataFile) - 4) & ".BAK"
On Error Resume Next
Kill stBackUpFile
On Error Goto Err_Function
FileCopy stDataFile, stBackUpFile

'Compact
DBEngine.CompactDatabase stDataFile, stOutFile, DB_LANG_GENERAL

'Delete Uncompacted Version
Kill stDataFile

'Rename Compacted Version
Name stOutFile As stDataFile

Compact = True

Exit_Function:
DoCmd.SetWarnings False
DoCmd.Hourglass True
Exit Function

Err_Function:
Compact = False
msgbox err.description
Resume Exit_Function
End Function

HTH,
Josh
 
Correction...

There is a problem with the code I posted. Change the Exit_Function block
to:

Exit_Function:
DoCmd.SetWarnings True
DoCmd.Hourglass False
Exit Function

Sorry,
Josh
 
Back
Top