m:
We have back end data on a server that only has admin access that
needs to be compacted/repaired about once per month. Is there a
way to set this up automatically - say at 2am when the server is
quiet?
Right now we have to find someone with admin access who can do it
and until we do all the wkstns are down.
I use a VBScript that I schedule in the Windows Scheduler. After my
signature is one recent one. The comments at the top explain how to
set it up. The database names and paths are hardwired into the
script, instead of it being generic and called with parameters.
This particular version is fairly elaborate, with logging, and a
check for compact errors.
--
David W. Fenton
http://www.dfenton.com/
contact via website only
http://www.dfenton.com/DFA/
Dim strDBName
Dim strDBPath
Dim strSourceDB
Dim strBackupDB
Dim objFSO
Dim objEngine
Dim objDB
Dim tdf
' change this to the name of the main data file
' without the extension
strDBName = "Data 2003"
' change this to the path to where the data file
' is with trailing backslash
strDBPath = "D:\PathToDataFile\"
' create a CompactBackup folder in that folder
' copy the CompactLog.txt file into this folder
strLogFile = strDBPath & "CompactBackup\CompactLog.txt"
strSourceDB = strDBPath & strDBName
strBackupDB = strDBPath & "CompactBackup\" & Year(Date()) & "-" _
& Right("00" & FormatNumber(Month(Date()), 0), 2) & "-" _
& Right("00" & FormatNumber(Day(Date()), 0), 2) _
& strDBName
Set objEngine = CreateObject("DAO.DBEngine.36")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If (objFSO.FileExists(strSourceDB & ".ldb")) Then
Call WriteLog(Err, "Skipping compact because file is open", _
objFSO, strLogFile)
Else
If (objFSO.FileExists(strBackupDB & ".mdb")) Then
On Error Resume Next
objFSO.DeleteFile strBackupDB & ".mdb"
Call WriteLog(Err, "Deleting file", objFSO, strLogFile)
On Error GoTo 0
End If
On Error Resume Next
objFSO.MoveFile strSourceDB & ".mdb", strBackupDB & ".mdb"
Call WriteLog(Err, "Moving file", objFSO, strLogFile)
On Error GoTo 0
On Error Resume Next
objEngine.CompactDatabase strBackupDB & ".mdb", _
strSourceDB & ".mdb"
Call WriteLog(Err, "Compacting file", objFSO, strLogFile)
On Error GoTo 0
' check for Compact Errors table
Set objDB = objEngine.OpenDatabase(strSourceDB & ".mdb")
For Each tdf in objDB.TableDefs
If tdf.Name = "MSysCompactErrors" Then
On Error Resume Next
Err.Raise 9999, , "Errors occured in compact"
Call WriteLog(Err, "MSysCompactErrors table found", _
objFSO, strLogFile)
Err.Clear
End If
Next
Set tdf = Nothing
Set objDB = Nothing
End If 'LckFile
Set objEngine = Nothing
Private Sub WriteLog(ByRef objErr, ByVal strMessage, _
ByRef objFSO, ByVal strLogFile)
Dim strLogEntry
Dim objLogFile
Dim ts
If Err.Number = 0 Then
strLogEntry = Now() & " -- Success (" & strMessage & ")"
Else
strLogEntry = Now () & " -- " & Err.Number & ": " _
& Err.Description & " (" & strMessage & ")"
End If
'MsgBox strLogEntry
Set objLogFile = objFSO.GetFile(strLogFile)
Set ts = objLogFile.OpenAsTextStream(8)
ts.WriteLine strLogEntry
ts.Close
set ts = Nothing
Set objLogFile = Nothing
End Sub