auto-compact bypassed by docmd.quit (1 Viewer)

aidan

Registered User.
Local time
Today, 13:41
Joined
May 23, 2005
Messages
34
I'm trying to use a button to close a database which has compact-on-exit set. However docmd.RunCommand acCmdCompactDatabase won't run from a form (there are lots of threads about this here), and both Access.RunCommand acCmdExit and DoCmd.Quit both ignore the flag.

Is there a way around this? and why does it get bypassed anyway?
 

ghudson

Registered User.
Local time
Today, 08:41
Joined
Jun 8, 2002
Messages
6,195
That command will not compact a db in the manner that you want. Use DoCmd.Quit to close the db. Then also add my CompactOnClose() function to only compact the db if it is really needed when you are closing the db.

I run this function to only compact the db on close if it exceeds my preset amount when the db is being closed.

Code:
'Used with setting the status bar message
Public vStatusBar As Variant

Public Function CompactOnClose()
On Error GoTo Err_CompactOnClose

    If FileLen(CurrentDb.Name) > 5000000 And CurrentUser <> "programmer" Then '5 megabytes
        Application.SetOption ("Auto Compact"), 1
        Application.SetOption "Show Status Bar", True
        vStatusBar = SysCmd(acSysCmdSetStatus, "The application must be compacted, please do not interfere with the Compacting process!")
    Else
        Application.SetOption ("Auto Compact"), 0
    End If

Exit_CompactOnClose:
    Exit Function

Err_CompactOnClose:
    MsgBox Err.Number & " - " & Err.Description
    Resume Exit_CompactOnClose

End Function
 

aidan

Registered User.
Local time
Today, 13:41
Joined
May 23, 2005
Messages
34
Wow, just what I was looking for. Thanks!
 

Users who are viewing this thread

Top Bottom