0

I have a VBA query built in excel that runs a SQL query against an Access Database. The connection works when the database is unprotected without a password using the following code:

Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct

But when I password protect the database and try the same above but with added password condition, it won't connect and I get a "Run-time error -2147217843" message. The code I'm using for this is as follows:

Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Cnct = Cnct & "user ID=" & Environ("Username") & ";"
Cnct = Cnct & "password=XXXXXXXXXX;"
Connection.Open ConnectionString:=Cnct

Does anybody know what I am doing wrong here?

5
  • Some one will probably spot this straight away but what is in Environ("Username") at run time? and then can you manually log into the db using the value in Environ("Username") and the password? Commented Jun 16, 2015 at 11:07
  • When i open the database it only asks for a password. Do I even need the username piece? I got Environ("Username") from internet, it supposedly grabs the windows login username. Commented Jun 16, 2015 at 11:10
  • 3
    connectionstrings.com/access - See the comments under Microsoft ACE OLEDB 12.0 RE encryption with Access 2010 Commented Jun 16, 2015 at 11:10
  • Thanks Alex, do you know how to set the encryption settings to the 2007 encryption? Commented Jun 16, 2015 at 13:24
  • The database is looking for the database userid and password, not the windows userid (which is returned by Environ("Username") ) . If it only asks for a password, the UserID is likely admin Commented Feb 18, 2018 at 19:09

1 Answer 1

1

This is the connection string to use when you have an Access 2007 - 2013 database protected with a password using the "Set Database Password" function in Access.

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\myFolder\myAccessFile.accdb;Jet OLEDB:Database Password=MyDbPassword;

Some reports of problems with password longer than 14 characters. Also that some characters might cause trouble. If you are having problems, try change password to a short one with normal characters.

Note! Reports say that a database encrypted using Access 2010 - 2013 default encryption scheme does not work with this connection string. In Access; try options and choose 2007 encryption method instead. That should make it work. We do not know of any other solution. Please get in touch if other solutions is available!

(Source)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.