我当前正在尝试连接到2个单独的.mdb
文件并执行一个INNER JOIN
。
因此,我有2个.mdb(Sample1.mdb
和Sample2.mdb
)。两者均受密码保护。
当我尝试连接时,它显示一条错误,指出"it's already opened exclusively by another user, or you need permission..."
。
是否由于未在此语句中插入password
属性而发生错误?如果是,如何在此语句中插入password
属性?
[Excel 8.0;HDR=YES;IMEX=2;DATABASE=" & Application.ActiveWorkbook.Path & "\Sample1.mdb]
?
这是我的完整示例代码:
Dim Conn As ADODB.Connection
Dim resultSet As ADODB.Recordset
Set Conn = New ADODB.Connection
sqlStatement = "SELECT * FROM [Excel 8.0;HDR=YES;IMEX=2;DATABASE=" &
Application.ActiveWorkbook.Path & "\Sample1.mdb].[SampleData$] a INNER JOIN
[Excel 8.0;HDR=YES;IMEX=2;DATABASE=" & Application.ActiveWorkbook.Path &
"\Sample2.mdb].[SampleInfo$] b ON a.Index = b.Index WHERE a.idCode = 2"
With Conn
.Provider = "Microsoft.ACE.OLEDB.12.0"
.ConnectionString = Application.ActiveWorkbook.Path + "\Sample2.mdb"
.Properties("Jet OLEDB:Database Password") = "password"
.Open
Set resultSet = .Execute(sqlStatement)
End With
答案 0 :(得分:1)
这有一些奇怪的事情。
[Excel 8.0;HDR=YES;IMEX=2;
是用于连接到Excel文件而不是MDB数据库的连接字符串。由于将MDB数据库作为参数传递给它,因此肯定会出现奇怪的错误。由于连接到Access是Access的本机,因此只需要指定路径即可。Dim Conn As ADODB.Connection
Dim resultSet As ADODB.Recordset
Set Conn = New ADODB.Connection
sqlStatement = "SELECT * FROM [" &
Application.ActiveWorkbook.Path & "\Sample1.mdb].[SampleData$] a INNER JOIN [SampleInfo$] b ON a.Index = b.Index WHERE a.idCode = 2"
With Conn
.Provider = ""
.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;User ID=Admin;Data Source="Application.ActiveWorkbook.Path + "\Sample2.mdb;Jet OLEDB:Database Password=""password"""
.Open
Set resultSet = .Execute(sqlStatement)
End With
要使用密码查询Access数据库,请在ISAM连接字符串中指定密码:
"SELECT * FROM [MS Access;PWD=password;DATABASE=" &
Application.ActiveWorkbook.Path & "\Sample1.mdb].[SampleData$] a INNER JOIN
[MS Access;PWD=password;DATABASE=" & Application.ActiveWorkbook.Path &
"\Sample2.mdb].[SampleInfo$] b ON a.Index = b.Index WHERE a.idCode = 2"
当然,您需要确保对两个MDB数据库使用正确的连接字符串:使用当前代码,Sample2受保护,但Sample1受保护,Sample2受加密保护(仅提示输入密码),而不是用户级别安全性(提示输入密码和用户名)。