无法从 Excel 中的 VBA 查询连接到 Access 数据库
Can't connect to Access Database from VBA query in Excel
我在 excel 中内置了一个 VBA 查询,它针对 Access 数据库运行 SQL 查询。使用以下代码在没有密码的情况下数据库不受保护时连接有效:
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct
但是当我用密码保护数据库并尝试上面相同的操作但添加了密码条件时,它不会连接并且我收到 "Run-time error -2147217843" 消息。我为此使用的代码如下:
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
有人知道我做错了什么吗?
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)
我在 excel 中内置了一个 VBA 查询,它针对 Access 数据库运行 SQL 查询。使用以下代码在没有密码的情况下数据库不受保护时连接有效:
Set Connection = New ADODB.Connection
Cnct = "Provider=Microsoft.ACE.OLEDB.12.0;"
Cnct = Cnct & "Data Source=" & DBFullName & ";"
Connection.Open ConnectionString:=Cnct
但是当我用密码保护数据库并尝试上面相同的操作但添加了密码条件时,它不会连接并且我收到 "Run-time error -2147217843" 消息。我为此使用的代码如下:
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
有人知道我做错了什么吗?
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)