It is possible to program a command button that will compact "another" database, however you can't program a command button to compact the current database because it is still open and will fail.
You can always create a shortcut to compact the database.
There are a couple of parts to creating the shortcut.
Shortcut Properties for Target.
"Location of MSACCESS.EXE" "Location of Database" /repair /compact
Example
"C:\Program Files\Microsoft Office\Office\MSACCESS.EXE" "C:\Program Files\Database\My DB.mdb" /repair /compact
You can also set the database to compact and repair on close using the options within Access