The compact option is already turned on in the BE. It doesn't compact when you close the FE which was exclusively connected to it. No events where fired when connecting to the BE.Are you wanting to compact the front end or the back end? Why not simply tick compact on close in the settings. So when your overnight job has finished and the front end closes, this will close the back as well, if you are the only user on the system. Thereby firing the compact on close on the back end. Having said that I am not exectly sure that it will anyway you can code a compact routine on completion of the overnight job.
David
'Compact the database to a temp name
DBEngine.CompactDatabase "C:\Databases\SSPSdata.mdb", "C:\Databases\SSPSdataOld.mdb"
DoEvents
'Check to see if the file has been created
If Dir("C:\Databases\SSPSdataOld.mdb") <> "" Then
'Delete the original mdb
Kill "C:\Databases\SSPSdata.mdb"
'Run the compact again back to its original name
DBEngine.CompactDatabase "C:\Databases\SSPSdataOld.mdb", "C:\Databases\SSPSdata.mdb"
DoEvents
'Delete the old copy
Kill "C:\Databases\SSPSdataOld.mdb"
DoEvents
Else
'Did the compact work?
If MsgBox("A problem occured creating the initial compacted file." & vbCrLf & vbCrLf & "click OK to continue without compacting the database or Cancel to exit the backup facility.", vbExclamation + vbOKCancel, "Problem occured") = vbCancel Then
Exit Function
End If
End If
Public Function CompactDb(strDbName As String) As Boolean
Dim dbs As Database
Dim strCurdir As String
Dim strDbNw As String
Dim strNewName As String
On Error GoTo CD_Error
'Assume success
CompactDb = True
Set dbs = CurrentDb
strDbNw = "CompactNw.mdb"
strCurdir = Mid(strDbName, 1, Len(strDbName) - Len(Dir(strDbName)))
strNewName = strCurdir & strDbNw
If FileExists(strNewName) Then
Kill strNewName
End If
DBEngine.CompactDatabase strDbName, strNewName
DoEvents
'Check to see if the file has been created
If Len(Dir(strNewName)) > 0 Then 'Delete the original mdb
Kill strDbName
'Run the compact again back to its original name
DBEngine.CompactDatabase strNewName, strDbName
DoEvents 'Delete the old copy
Kill strNewName
DoEvents
Else 'Did the compact work?
CompactDb = False
End If
Exit_CD:
Exit Function
CD_Error:
CompactDb = False
ErrorProc Err, Err.Description, "CompactDb", "modUtilities"
Resume Exit_CD
Resume
End Function
I would use a different front end that doe snot open any field int he database to be compacted.But since the FE is connected to the BE how can i compact it?
Dim db
Dim strDbName
dim strPassword
dim strMacro
strMacro = "YourMacro"
strdbName = "c:\yourpath\yourDB.mdb"
strPassword = "YourPassword"
Set acc = createobject("Access.Application")
' this temporarily changes the macros security to low to avoid messages.
'onlt works with access 2000 onwards
if acc.syscmd(7) >= 10 Then
acc.AutomationSecurity = 1
End if
'if you do not need to see Access delete this line
acc.Visible = True
Set db = acc.DBEngine.OpenDatabase(strDbName, False, False, ";PWD=" & strPassword)
acc.OpenCurrentDatabase strDbName
acc.docmd.runmacro strMacro
db.Close
Dim db
Dim acc
Dim strDbName
WScript.Sleep 300000 '5*60 sec
strdbName = "C:\Temp\ContData.mdb"
Set acc = createobject("Access.Application")
'if you do not need to see Access delete this line
acc.Visible = True
Set db = acc.DBEngine.OpenDatabase(strDbName)
acc.OpenCurrentDatabase strDbName
acc.docmd.runmacro "CompactMe"
db.close