How do I compact and repair a database with a password

Posted on 2014-08-18
Last Modified: 2014-08-19
I'm trying to compact and repair a password protected database with the following code:

Public Sub Compact()
   Dim CurrentFile As String, BackupFile As String
   On Error GoTo 0
   CurrentFile = "C:\CAV3\Database1.accdb"
   BackupFile = "F:\Database1.accdb"
   If Dir(BackupFile) > "" Then
      Kill BackupFile
   End If
   Application.CompactRepair CurrentFile, BackupFile, True
   Exit Sub
End Sub

How do I change it to supply the password?

Thanks in advance.
Question by:CRB1609
    LVL 119

    Accepted Solution

    look at DBEngine.CompactDatabase Method

    DBEngine.CompactDatabase CurrentFile, BackupFile, ";pwd=password", , ";pwd=password"

    Author Closing Comment

    Perfect.  Thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    The first two articles in this short series — Using a Criteria Form to Filter Records ( and Building a Custom Filter ( — discuss in some detail how a form can be…
    It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
    In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
    With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now