call the following code from the on close event procedure of your main form (or an invisible form what loads when the client starts). I use it on NT and 2000.
'**********************************************************************************
Sub sbCompactCurrentDatabase()
Dim FS, a
Dim strWrite As String, strVBFilePath As String
strVBFilePath = CurrentProject.Path & "\testfile.vbs"
strWrite = "Set objApp = WScript.CreateObject(" & Chr(34) & "Access.Application" & Chr(34) & "" & vbCrLf & _
"Set FS = WScript.CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & "" & vbCrLf & _
"k = Now() + TimeSerial(0, 0, 5)" & vbCrLf & _
"While k > Now()" & vbCrLf & _
"Wend" & vbCrLf & _
"On Error Resume Next" & vbCrLf & _
"strFilePath = " & Chr(34) & CurrentProject.Path & "\" & CurrentProject.Name & Chr(34) & vbCrLf & _
"strTempPath = " & Chr(34) & CurrentProject.Path & "\Tmp_" & CurrentProject.Name & Chr(34) & vbCrLf & _
"If FS.FileExists(strTempPath) <> " & Chr(34) & Chr(34) & " Then FS.DeleteFile strTempPath" & vbCrLf & _
"objApp.DBEngine.CompactDatabase strFilePath, strTempPath" & vbCrLf & _
"If FS.FileExists(strTempPath) <> " & Chr(34) & Chr(34) & " Then" & vbCrLf & _
" FS.DeleteFile strFilePath" & vbCrLf & _
" FS.CopyFile strTempPath, strFilePath, True" & vbCrLf & _
" FS.DeleteFile strTempPath" & vbCrLf & _
"End If" & vbCrLf & _
"FS.DeleteFile " & Chr(34) & strVBFilePath & Chr(34)
On Error Resume Next
Call Kill(strVBFilePath)
On Error GoTo 0
Set FS = CreateObject("Scripting.FileSystemObject"
Set a = FS.CreateTextFile(strVBFilePath, True)
a.WriteLine (strWrite)
a.Close
strVBFilePath = "WScript.exe " & Chr(34) & strVBFilePath & Chr(34)
Shell strVBFilePath
Application.Quit acQuitSaveAll
End Sub ide
'**********************************************************************************
Sub sbCompactCurrentDatabase()
Dim FS, a
Dim strWrite As String, strVBFilePath As String
strVBFilePath = CurrentProject.Path & "\testfile.vbs"
strWrite = "Set objApp = WScript.CreateObject(" & Chr(34) & "Access.Application" & Chr(34) & "" & vbCrLf & _
"Set FS = WScript.CreateObject(" & Chr(34) & "Scripting.FileSystemObject" & Chr(34) & "" & vbCrLf & _
"k = Now() + TimeSerial(0, 0, 5)" & vbCrLf & _
"While k > Now()" & vbCrLf & _
"Wend" & vbCrLf & _
"On Error Resume Next" & vbCrLf & _
"strFilePath = " & Chr(34) & CurrentProject.Path & "\" & CurrentProject.Name & Chr(34) & vbCrLf & _
"strTempPath = " & Chr(34) & CurrentProject.Path & "\Tmp_" & CurrentProject.Name & Chr(34) & vbCrLf & _
"If FS.FileExists(strTempPath) <> " & Chr(34) & Chr(34) & " Then FS.DeleteFile strTempPath" & vbCrLf & _
"objApp.DBEngine.CompactDatabase strFilePath, strTempPath" & vbCrLf & _
"If FS.FileExists(strTempPath) <> " & Chr(34) & Chr(34) & " Then" & vbCrLf & _
" FS.DeleteFile strFilePath" & vbCrLf & _
" FS.CopyFile strTempPath, strFilePath, True" & vbCrLf & _
" FS.DeleteFile strTempPath" & vbCrLf & _
"End If" & vbCrLf & _
"FS.DeleteFile " & Chr(34) & strVBFilePath & Chr(34)
On Error Resume Next
Call Kill(strVBFilePath)
On Error GoTo 0
Set FS = CreateObject("Scripting.FileSystemObject"
Set a = FS.CreateTextFile(strVBFilePath, True)
a.WriteLine (strWrite)
a.Close
strVBFilePath = "WScript.exe " & Chr(34) & strVBFilePath & Chr(34)
Shell strVBFilePath
Application.Quit acQuitSaveAll
End Sub ide