Compact and repair from command button Access 2010

I have a form which conatins a command button to compact and repair the database when the button is clicked.  It doesn't seem to work any longer with Access 2010.  Here is my code:

Private Sub Command111_Click()

    On Error GoTo Err_Command111_Click

    CommandBars("Menu Bar"). _
    Controls("Tools"). _
    Controls("Database utilities"). _
    Controls("Compact and repair database..."). _

    Exit Sub

    MsgBox Err.Description
    Resume Exit_Command111_Click

End Sub

I need to be able to compact and repair using 2003 and 2010 (and 2007) with a command button.  How can I fix this?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Your code is failing because it can no longer find the correct "menu" items (2010 and 2007 don't have the same menu structures).

You have a few options:

1) Use Sendkeys, which is something like this:

SendKeys "%(YC)", False

This may or may not work. Sendkeys is notoroious for sporadically working, so use this at your own risk.

2) Use the DAO method:

DAO.CompactDatabase "SourceDatabase", "Destination"

See this help topic:

The trouble with this is you cannot compact an in-use database, so you must do this with your database closed (if you're working with an unsplit database) or you must do this on the Backend ONLY (if your database is split).

Roger Carlson has a good example of this:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Systems AnalystCommented:
I also suggest you review the information in the link below:

And be aware of this utility:

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.