Search code examples
vbams-accesspasswords

Open a MS Access db protected


I have an issue coding on MS Access.

Here the code I have, easy code, to control a button to import data (from a form in MS Access to another MS Access DB). The macro is located in a DB protected by a pw. After few tests I am not able to find the good script to open and fill the pw. The pw must be included in the script to have an automatic process. (The username thing is properly sorted with a Public function).

MS Access 365 MSO 16.0 64bits Version 2108

Private Sub Command35_Click()
    Dim AppAccess As Access.Application
    Set AppAccess = GetObject("C:\Users\" & GetUserName & "\Databases\DB Friends.accdb")
    AppAccess.Visible = False
    AppAccess.DoCmd.RunMacro "Mcr_M_NewFriends"
    AppAccess.Quit
    Me.Command35.BackColor = RGB(255, 124, 128)
    MsgBox "New friends data imported"
End Sub

Can you please help me on that? Thanks guys.


Solution

  • Try the following

            Private Sub Command35_Click()
            Dim acc As Access.Application
            Dim db As DAO.Database
            Dim strDbName As String
            strDbName = "C:\database Folder\abc.accdb"
            Set acc = New Access.Application
            acc.Visible = False
            acc.OpenCurrentDatabase strDbName, False, "password"
            Set db = acc.CurrentDb() '
            acc.UserControl = True
            acc.DoCmd.RunMacro "Mcr_M_NewFriends"
            acc.Quit
            Me.Command35.BackColor = RGB(255, 124, 128)
            MsgBox "New friends data imported"
       End Sub