Connect password protected access file

时间:2017-06-15 09:56:48

标签: excel vba excel-vba ms-access

I want to connect with password protected access file. with below coding, I am getting an error as "Incorrect password" but when I open it manually with the same password, its open. Do I get to make any changes in database setting.

Dim Acon As New ADODB.Connection
Dim Rs As New ADODB.Recordset

With Acon
    .Provider = "Microsoft.ACE.OLEDB.12.0"
   .ConnectionString = "Data Source=" + "U:\scratch\OK.accdb"
    .Properties("Jet OLEDB:Database Password") = "123"
    .Open
 End With

1 个答案:

答案 0 :(得分:4)

Yes you will get that problem. To sort it, follow the steps exactly as I tell you

  1. Close your database
  2. From Access, click on File|Open and then open your database as Exclusive enter image description here
  3. Decrypt the database from the File menu

    enter image description here

  4. Click on File|Options|Client Settings and navigate to the end and select the options shown below enter image description here

  5. Encrypt your database after you click OK in the above screen.
  6. Close the database and then try the code

Code I used for testing

Sub Sample()
    Dim Acon As New ADODB.Connection
    Dim Rs As New ADODB.Recordset

    With Acon
        .Provider = "Microsoft.ACE.OLEDB.12.0"
        .ConnectionString = "C:\Users\Siddharth\Desktop\Database3.accdb"
        .Properties("Jet OLEDB:Database Password") = "test"
        .Open
     End With
End Sub