wilsonm2
wilsonm2

Reputation: 591

Can't connect to Access Database from VBA query in Excel

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?

Upvotes: 0

Views: 1480

Answers (1)

ashleedawg
ashleedawg

Reputation: 21619

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)

Upvotes: 1

Related Questions