To compact an Access database via VBA you need to make a copy of the
original, and compact from one to the other. When complete you can
delete the original. Here is code from within Access, you will need to
reference MS-Access within Excel to accomplish this. Though I will admit
I never tried this via Excel.
Dim dbOriginal As String
Dim dbCompact As String
Dim dbPath As String
Dim dbTempPath As String
dbOriginal = "Inventory and Cost.mdb" 'Original Database
dbCompact = "Inventory and Cost1.mdb" 'Database to copy too
dbPath = "M:\Databases\" & dbOriginal 'Path of original database
dbTempPath = "M:\Databases\" & dbCompact 'Path of new database
Call DBEngine.CompactDatabase(dbPath, dbTempPath) 'Compact data
DoEvents
Kill dbPath 'Delete original
DoEvents
Name dbTempPath As dbPath 'Rename new database to original name