Auto Compacting

PaulSpell

Registered User.
Local time
Today, 13:26
Joined
Apr 19, 2002
Messages
201
Does anyone have the code for, or know how to, auto compact a database?

I have a database that creates a number of large temporary tables. Obviously the database gets very big very quickly and I want to include some code that compacts the database automatically either at a fixed time each day or alternatively each time the application is closed.

Can anyone help?
 
Paul,

I had a similar problem. I found an access addin called compacter which I can send you(email me). I put together some code to detect when the database reached a certain size. The user then got prompted with a tes no box to compact . see code below

The addin needs to be installed before it can work.

Function DataBaseSize()
'*******************************************
'Name: DataBaseSize (Function)
'Purpose: This warns the user to compact if the DB size gets to large
'Author: Mcgrco
'Date: August 19, 2002, 11:06:59 AM
'Called by:
'Calls: Calls an addin called compactor
'Inputs:
'Output:
'*******************************************

Dim dbsize As Long


Dim dbname As String
Dim msg As String
Dim CurUser As String


dbname = DBEngine(0)(0).Name
dbsize = FileLen(dbname)

On Error GoTo CompactError:

CurUser = UCase(Mid(CurrentUser, 1, 1)) & Mid(CurrentUser, 2, Len(CurrentUser))

msg = "WARNING! " & CurUser & "@" & "The database is currently at " & _
Format((dbsize) / 1049592, "##,##0") & "mb. It is" & Chr(10) & _
"recommended that you compact before " & Chr(10) & _
"running to help speed up the process. @Compact Now???"

If dbsize > 200000000 Then '200mb
If MsgBox(msg, vbYesNo, "Compact Database") = vbYes Then
Call Application.Run("Compacter.DoCompact", True)

End If
Else
End If

CompactError:
If err = 2517 Then 'if the addinin isnt installed warn the user
MsgBox "Cannot run compact function as the compact addin hasnt been installed" & Chr(10) & Chr(10) & _
"Select tools,addins,Addin Manager. Then add new" & Chr(10) & Chr(10) & _
"Browse for the file called compactor.mda in C:\Program Files\Liquidity" & Chr(10) & Chr(10) & _
"Ignore error, cant add to the registry"

End If
Exit Function


End Function
 
Excellent, thank you.
 

Users who are viewing this thread

Back
Top Bottom