I wish I knew how to code the compact and the backup on a button too. I am
still trying to search through help when I have time.
If you are interested in compacting code, you can try something that I
wrote/adapted. The company I work for has about a hundred special
purpose .MDB/.MDE files. They are listed in directories such as: \
\fileserver\db$\[something] where different users permissions are set
on each [something] directory. The code recurses through the directory
tree and compacts any MDB/MDE file.
Also, this won't work as is - I have a table where I audit the
performance of this code (size before, size after, time start, time
stop, etc.) and you will need to remove the calls to that function.
'Recurse through the path provided and compact all .MDE or .MDB files
'Must include final backslash in path
Public Function AutoCompactDatabase(Optional strPath As String = "\
\fileserver\db$\", Optional intDirectory As Integer = 1)
On Error GoTo AutoCompactDatabase_Err
Dim strDir As String
strDir = Dir(strPath & "*.mdb")
While strDir <> "" And intDirectory = 1
compactDB strPath & strDir
strDir = Dir
Wend
strDir = Dir(strPath & "*.mde")
While strDir <> "" And intDirectory = 1
compactDB strPath & strDir
strDir = Dir
Wend
strDir = Dir$(strPath, vbDirectory)
Dim intLocalDir As Integer
intLocalDir = 0
While strDir <> ""
'If a directory other than this and previous then recurse
If strDir <> "." And strDir <> ".." And (GetAttr(strPath & strDir)
And vbDirectory) = vbDirectory Then
intLocalDir = intLocalDir + 1
If intLocalDir = intDirectory Then
AutoCompactDatabase strPath & strDir & "\"
End If
End If
strDir = Dir$
Wend
Exit Function
AutoCompactDatabase_Err:
If Err.Number = 5 Then
intLocalDir = 0
intDirectory = intDirectory + 1
strDir = Dir(strPath, vbDirectory)
Resume Next
Else
MsgBox Error$ & Err.Number
End If
End Function
Private Function compactDB(aName As String) As Boolean
On Error GoTo compactDB_Err
Dim ws As Workspace
Dim db As Database
Dim nName As String
Dim dtStart As Date, dtEnd As Date
Dim lngSizeStart As Long, lngSizeEnd As Long
'Initialize audit data
dtStart = Now()
lngSizeStart = FileLen(aName)
'Compact and replace the database
nName = Left(aName, Len(aName) - 3) & "NEW"
DBEngine.CompactDatabase aName, nName
Kill aName
Name nName As aName
'Successful Compacting of Database
dtEnd = Now()
lngSizeEnd = FileLen(aName)
auditLog dtStart, dtEnd, lngSizeStart, lngSizeEnd, aName
compactDB = True
DoEvents
Exit Function
compactDB_Err:
Select Case Err.Number
Case 3356 'MDB or MDE in use
auditLog dtStart, dtEnd, lngSizeStart, lngSizeEnd, aName,
"Error 3356: Database in use"
compactDB = False
Debug.Print "Unable to compact " & aName & " in use."
Case 70 'Permission denied (probably kill command)
auditLog dtStart, dtEnd, lngSizeStart, lngSizeEnd, aName,
"Error 70: " & Err.Description
compactDB = False
Debug.Print "Unable to compact " & aName & " permission
denied."
Case Else
auditLog dtStart, dtEnd, lngSizeStart, lngSizeEnd, aName,
"Error " & Err.Number & ": " & Err.Description
compactDB = False
Debug.Print "Unhandled Error for DB " & aName & "."
End Select
DoEvents
End Function