Hello,
I understand that you want to make this an automated action, but Access has a feature that will allow you to compact and repair the database everytime it is closed.
You can do this by opening the database, selecting the tools menu, then options. Select the general tab, and make sure the Compact on Close option is selected.
The only time this will not occur is if this is a multiuser application, and there are still users using the application at the time of close, but from what I understand, even if you automate this, and it is a multiuser application, it will not compact and repair if there are users still using the application.
Mike