Granny Spitz via AccessMonster.com said:
That option is only available for the *current* database file through
the Access interface. You would have to kick everybody out of the
front end and then open and close the back end with the compact on
close option set on the back end.
To compact a database, you need exclusive access to the database.
The front end is linked so when you use code in the front end to
compact the back end, the compaction will fail because *you* are in
it (linked from the front end). You can unlink all your tables, run
code to compact the back end then relink the front end, but it's
easier to just set up a windows task to compact it at night when
everybody is gone or compact the back end manually when nobody is in
it.
It's not necessary to unlink all the tables. As long as no one (the
current database included) has any *active* connection to the back
end -- no open bound forms, table/query datasheets, reports, recordsets,
etc. -- you can get exclusive access to the back end database and
compact it. I have some code here somewhere .... ah, here it is:
'------ start of code (Access 97) -----
Sub CompactBackendDatabase()
' Compact the back-end database to which this database is linked.
On Error GoTo Err_Handler
Dim strBackendPathAndName As String
Dim strBackendFileName As String
Dim strBackendFolder As String
Dim strFileNameNoExt As String
Dim strTempFile As String
Dim strOldFile As String
Dim strErrText As String
Dim strErrUser As String
Dim strMessage As String
Dim intPos As Integer
strBackendPathAndName = DataDBName
strBackendFileName = Dir(strBackendPathAndName)
strBackendFolder = Left(strBackendPathAndName,
Len(strBackendPathAndName) - Len(strBackendFileName))
intPos = InStrRev(strBackendFileName, ".")
If intPos > 0 Then
strFileNameNoExt = Left(strBackendFileName, intPos - 1)
Else
strFileNameNoExt = strBackendFileName
End If
strTempFile = strBackendFolder & strFileNameNoExt & "_COMPACTED.mdb"
DoCmd.OpenForm "PleaseWait", , , , , , "Compacting back-end database
...."
DoEvents
Application.DBEngine.Idle dbRefreshCache
DoEvents
' Compact the database into the temp file.
Application.DBEngine.CompactDatabase strBackendPathAndName,
strTempFile
Application.DBEngine.Idle dbRefreshCache
' If that went well, rename the database to a different name.
strOldFile = strBackendFolder & strFileNameNoExt &
"_UNCOMPACTED.mdb"
If FileExists(strOldFile) Then
Kill strOldFile
End If
Name strBackendPathAndName As strOldFile
' If that went well, rename the temp file to the original DB name.
Name strTempFile As strBackendPathAndName
' If that went well, delete the old, uncompacted back-end.
Kill strOldFile
' Refresh the table links.
' RefreshTableLinks
' We're done.
Exit_Point:
Application.DBEngine.Idle dbRefreshCache
DoCmd.Close acForm, "PleaseWait"
DoEvents
Exit Sub
Err_Handler:
DoCmd.Hourglass False
Select Case Err.Number
Case 3356
strMessage = "Unable to get exclusive access to the back-end
database.@"
intPos = InStr(Err.Description, " by user")
If intPos > 0 Then
strErrText = Mid(Err.Description, intPos)
strErrText = Left(strErrText, InStr(strErrText, "."))
strErrUser = Mid(strErrText, 11, InStr(11, strErrText,
"'") - 11)
If strErrUser <> CurrentUser Then
strMessage = strMessage & "The database is currently
in use" & strErrText
Else
strMessage = strMessage & _
"Close any open forms, reports, or datasheets
that are " & _
"displaying data, and try again."
End If
End If
strMessage = strMessage & _
"@All active connections to the database must be closed
in order to compact it."
MsgBox strMessage, vbExclamation, "Unable to Compact
Back-End Database"
Case Else
MsgBox Err.Description, vbExclamation, "Error " & Err.Number
End Select
LogError "CompactBackendDatabase"
Resume Exit_Point
End Sub
'------ end of code -----
Be aware that many of the lines of code above will have been wrapped by
the newsreader. I haven't tried to fix them up.
Note: the function DataDBName() is a user-defined function that returns
the name and path of the back-end database. FileExists() is a function
that returns True if the argument "FileName" is the name of an existing
file, that is not a directory and not a hidden or system file, and False
if it is not. InStrRev() is not the built-in VBA function that was
introduced with Access 2000, but rather a user-written lookalike that I
added for use with Access 97. If you're adapting this function for
Access 2000 or later, you don't need to worry about that.