"Compact and Repair Database" Command in a Form

Discussion in 'Microsoft Access Form Coding' started by Michael, Feb 19, 2004.

  1. Michael

    Michael Guest

    I would like to run the "Compact and Repair Database"
    command by clicking a command button on a form. I am using
    MS Access 2000.

    The "DoCmd.RunCommand acCmdCompactDatabase" cannot work
    while running a VB code or macro. Also, I can't find the
    right parameters to use with "DoCmd.DoMenuItem" for that
    command.

    I already created a custom toolbar with this command. But
    this is the top menu. I would like to have it also in the
    main menu, which is a form.

    Can it be done?

    Thanks,

    Michael
     
    Michael, Feb 19, 2004
    #1
    1. Advertisements

  2. You can't compact the current database through code.

    --
    Doug Steele, Microsoft Access MVP
    http://I.Am/DougSteele
    (No private e-mails, please)



    "Michael" <> wrote in message
    news:131c801c3f730$92bc4a10$...
    > I would like to run the "Compact and Repair Database"
    > command by clicking a command button on a form. I am using
    > MS Access 2000.
    >
    > The "DoCmd.RunCommand acCmdCompactDatabase" cannot work
    > while running a VB code or macro. Also, I can't find the
    > right parameters to use with "DoCmd.DoMenuItem" for that
    > command.
    >
    > I already created a custom toolbar with this command. But
    > this is the top menu. I would like to have it also in the
    > main menu, which is a form.
    >
    > Can it be done?
    >
    > Thanks,
    >
    > Michael
    >
     
    Douglas J. Steele, Feb 19, 2004
    #2
    1. Advertisements

  3. Hi Michael,

    The following worked for me. Try it in a copy and see if it also works for
    you. The code is behind the OnClick event of a commandbutton, in my case the
    button name is CompactDB.

    Private Sub CompactDB_Click()
    On Error GoTo Err_CompactDB_Click

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

    Exit_CompactDB_Click:
    Exit Sub

    Err_CompactDB_Click:
    MsgBox Err.Description
    Resume Exit_CompactDB_Click

    End Sub

    Hope this helps,

    Alp
    "Michael" <> wrote in message
    news:131c801c3f730$92bc4a10$...
    > I would like to run the "Compact and Repair Database"
    > command by clicking a command button on a form. I am using
    > MS Access 2000.
    >
    > The "DoCmd.RunCommand acCmdCompactDatabase" cannot work
    > while running a VB code or macro. Also, I can't find the
    > right parameters to use with "DoCmd.DoMenuItem" for that
    > command.
    >
    > I already created a custom toolbar with this command. But
    > this is the top menu. I would like to have it also in the
    > main menu, which is a form.
    >
    > Can it be done?
    >
    > Thanks,
    >
    > Michael
    >
     
    Alp Bekisoglu, Feb 20, 2004
    #3
  4. Michael

    Michael Guest

    It works!

    Thank you very much!

    Michael



    >-----Original Message-----
    >Hi Michael,
    >
    >The following worked for me. Try it in a copy and see if

    it also works for
    >you. The code is behind the OnClick event of a

    commandbutton, in my case the
    >button name is CompactDB.
    >
    >Private Sub CompactDB_Click()
    >On Error GoTo Err_CompactDB_Click
    >
    > CommandBars("Menu Bar"). _
    > Controls("Tools"). _
    > Controls("Database utilities"). _
    > Controls("Compact and repair database..."). _
    > accDoDefaultAction
    >
    >Exit_CompactDB_Click:
    > Exit Sub
    >
    >Err_CompactDB_Click:
    > MsgBox Err.Description
    > Resume Exit_CompactDB_Click
    >
    >End Sub
    >
    >Hope this helps,
    >
    >Alp
    >"Michael" <> wrote in

    message
    >news:131c801c3f730$92bc4a10$...
    >> I would like to run the "Compact and Repair Database"
    >> command by clicking a command button on a form. I am

    using
    >> MS Access 2000.
    >>
    >> The "DoCmd.RunCommand acCmdCompactDatabase" cannot work
    >> while running a VB code or macro. Also, I can't find the
    >> right parameters to use with "DoCmd.DoMenuItem" for that
    >> command.
    >>
    >> I already created a custom toolbar with this command.

    But
    >> this is the top menu. I would like to have it also in

    the
    >> main menu, which is a form.
    >>
    >> Can it be done?
    >>
    >> Thanks,
    >>
    >> Michael
    >>

    >
    >
    >.
    >
     
    Michael, Feb 20, 2004
    #4
  5. Michael

    Guest Guest

    This code works fine on condition that it is the only line in the OnClick-event of your pushbutton

    If you try to put more commands (like a complete VBA procedure), it won't work
    You get a message "You can't compact the open database while running a macro or Visual Basic code. Instead of using a macro or code, on the Tools menu, point to Database Utilities, and then click Compact/Repair Database.

    I have no explanation for this strange behaviour, I just found out after a lot of testing, so don't ask me why ..

    JMathys
     
    Guest, Mar 10, 2004
    #5
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim/Chris

    Compact and Repair

    Jim/Chris, Aug 27, 2003, in forum: Microsoft Access Form Coding
    Replies:
    0
    Views:
    169
    Jim/Chris
    Aug 27, 2003
  2. Mike C

    Compact and Repair

    Mike C, Oct 10, 2003, in forum: Microsoft Access Form Coding
    Replies:
    3
    Views:
    135
    NCole
    Oct 10, 2003
  3. Chris B

    Error on compact and repair database

    Chris B, Apr 27, 2004, in forum: Microsoft Access Form Coding
    Replies:
    1
    Views:
    435
    Guest
    Apr 27, 2004
  4. Guest

    Compact and Repair Database

    Guest, Nov 8, 2004, in forum: Microsoft Access Form Coding
    Replies:
    3
    Views:
    151
    Dirk Goldgar
    Nov 8, 2004
  5. Guest

    Error 7846 Can't compact and repair current database

    Guest, Oct 16, 2006, in forum: Microsoft Access Form Coding
    Replies:
    2
    Views:
    1,027
    Guest
    Oct 17, 2006
Loading...

Share This Page