Compact Database
acCmdCompactDatabase
This example can be used to compact a database. It can not be used to compact the database that is calling it.
This is not the best way to compact a database in code but is used for only to demonstrate the use of the acCmdCompactDatabase. It is better to use the CompactDatabase method of the DBEngine object.
'***************** Code Start *******************
' This code was originally written by Terry Wickenden.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
Sub CompactDB(strOldDb As String, Optional strNewDb As String)
'strOldDb = name of database to be compacted including full path
'strNewDb = name of database once compacted including full path
Dim strCompact As String
On Error GoTo errCompactDB
'Turn off screen refresh
Application.Echo False
'Check if new database has been specified
If IsMissing(strNewDb) Then
strCompact = strOldDb
Else
strCompact = strNewDb
End If
'Which data base to be compacted
SendKeys strOldDb, False
SendKeys "{enter}", False
'Name of database to compact to
SendKeys strCompact, False
SendKeys "{enter}", False
RunCommand acCmdCompactDatabase
exitCompactDB:
'Turn the screen refresh back on
Application.Echo True
Sub
errCompactDB:
Select Case Err
Case 2501
'Cancel Button selected in the compact database dialog
Case Else
MsgBox Err.Number & ":- " & vbCrLf & Err.Description
End Select
Resume exitCompactDB
End Sub
'***************** Code End *******************