You can't compact the back-end if anyone else is using it, and it's possible
for you to be using it if you've got a bound form open, for instance.
In general, you can use DAO to compact the back-end if it's not open. You
can tell if it's open by whether or not there's the locking file (.ldb) in
the same folder. I typically rename the existing back-end and then compact
it to "the correct name", just in case:
Function CompactDatabase(DatabaseName As String) As Boolean
' Renames the existing backend database from .MDB to .BAK
' Compacts the backup copy to the "proper" database
'
' Returns True if successful, False otherwise
On Error GoTo Err_CompactDatabase
Dim booStatus As Boolean
Dim strBackupFile As String
booStatus = True
' Make sure that DatabaseName exists
If Len(Dir$(DatabaseName)) > 0 Then
' Figure out what the backup file should be named
If StrComp(Right$(DatabaseName, 4), ".mdb", vbTextCompare) = 0 Then
strBackupFile = Left$(DatabaseName, Len(DatabaseName) - 4) &
".bak"
' Determine whether the backup file already exists,
' and delete it if it does.
If Len(Dir$(strBackupFile)) > 0 Then
Kill strBackupFile
End If
Name DatabaseName As strBackupFile
' Do the actual compact
DBEngine.CompactDatabase strBackupFile, DatabaseName
End If
End If
End_CompactDatabase:
CompactDatabase = booStatus
Exit Function
Err_CompactDatabase:
booStatus = False
MsgBox Err.Description & " (" & Err.Number & ")", _
vbOkOnly + vbCritical
Resume End_CompactDatabase
End Function
(watch for word-wrap in that code)
You can also use JRO (Jet Replication Objects) to do the compact.
See
http://support.microsoft.com/?id=230501 for details