Compacting db with pw through vba

thesaurusrex

Registered User.
Local time
Yesterday, 19:36
Joined
Aug 2, 2005
Messages
14
I used the backup/compact sample from Roger's Access Library as the base of a backup system for my database. The problem is, the database I'm working with is passworded. The following code works perfectly when the password is removed:

Code:
Public Function cmdCompact()
On Error GoTo Err_cmdCompact
Dim DateUnderscore As String
Dim ExecutionMacro As String
Dim BackupPath As String
    Set db = CurrentDb

    DateUnderscore = Month(Date) & "_" & Day(Date) & "_" & Year(Date)
    BackupPath = rgetpath(db.Name)
    FilePath = Left(BackupPath, Len(BackupPath) - 7)
    OriginalFile = "MedDirector2k.mdb"
    FileWithoutExtension = Left(OriginalFile, InStr(OriginalFile, ".") - 1)
    DoCmd.Hourglass True

TryAgain:
DBEngine.CompactDatabase FilePath & OriginalFile, BackupPath & FileWithoutExtension & DateUnderscore & ".mdb"

    DoCmd.Hourglass False

    
Exit_cmdCompact:
    Exit Function

Err_cmdCompact:
    If Err.Number = 3356 Then
        Resume TryAgain
    ElseIf Err.Number = 3045 Then
        Resume TryAgain
    Else
        MsgBox Err.Number & ": " & Err.Description
        Resume Exit_cmdCompact
    End If
    
End Function

It is run from a temporary mdb which is created that contains just the module to compact/backup the main database. I've tried appending ";pwd=mypassword" in a few different places, but I still am unable to perform the compact. I've looked at this thread, but I guess I'm not using the right syntax to apply it to mine. I likewise have a problem reopening the original database after the compact which is also due to not having a password.

Code:
Function ReRunApp()
Dim htask As Variant
Dim Response As Integer
Dim AppPath As String
Dim ExecutionMacro As String

   Set db = CurrentDb

    ExecutionMacro = " /xmcrReOpenForm"
    AppPath = SysCmd(acSysCmdAccessDir) & "MSACCESS.EXE"
    htask = Shell(AppPath & " """ & FilePath & OriginalFile & """" & ExecutionMacro, 1)

End Function

Any help with defining the password would be appreciated.
 
After reading a little more, I decided to change from a simple password on the database to user level security (albeit with only one user). Now the compact/backup is working fine. To backup the user is prompted for a login/pw twice, though. Is there any way to supply this automatically through code?
 

Users who are viewing this thread

Back
Top Bottom