✔ 最佳答案
Method 1: Probably it is what you are looking for
Open Access database, Tools -> Options -> General tab
Check the Compact on Close option
Method 2: VBA
A database cannot compact itself while it is open. You need another access database or program to compact your live database.
1. Create a new access database.
2. Create a new module, put the follow codes in the module
Function CompactDatabase()
Dim ac As New Access.Application
' use either one of the two methods below, do not use both
ac.CompactRepair "c:\temp\Live.mdb", "c:\temp\Temp.mdb" 'Compact the life database to a temp database
' or this
ac.DBEngine.CompactDatabase "c:\temp\Live.mdb", "c:\temp\Temp.mdb"
FileCopy "c:\temp\Temp.mdb", "c:\temp\Live.mdb" 'Make a copy of the compacted database
Kill "c:\temp\Temp.mdb" 'Delete the temp database
MsgBox "Live.mdb has been compacted successfully"
End Function
3. Create a new Macro
Macro Name: CompactDatabase
Action: RunCode
Function Name: CompactDatabase()
Save the Macro
Running this Macro will compact your live database. You can also use the CompactDatabase macro in any Event Procedure.