我怀疑根本没有办法使用 OLEDB 来做你描述的事情。
我测试了以下 VBA 代码,它工作正常:
Sub odbcTest()
Dim cdb As DAO.Database, sql As String
Set cdb = CurrentDb
sql = _
"SELECT ID, LastName INTO myContacts " & _
"FROM [ODBC;Driver={SQL Server Native Client 10.0};Server=.\SQLEXPRESS;Database=myDb;Trusted_Connection=yes;].dbo.myContacts"
cdb.Execute sql, dbFailOnError
End Sub
然后,我测试了(我希望是)OLEDB 等效项,但它失败了,出现了与您遇到的相同的错误(“找不到可安装的 ISAM。”)
Sub oledbTest()
Dim cdb As DAO.Database, sql As String
Set cdb = CurrentDb
sql = _
"SELECT ID, LastName INTO myContacts " & _
"FROM [OLEDB;Provider=SQLNCLI10;Server=.\SQLEXPRESS;Database=myDb;Trusted_Connection=yes;].dbo.myContacts"
cdb.Execute sql, dbFailOnError
End Sub
但请注意方括号内的ODBC;
andOLEDB;
前缀:在建立“正常”连接时不需要它们。以下代码有效...
Sub adoTest()
Dim con As Object
Const sqlInstance = ".\SQLEXPRESS"
Set con = CreateObject("ADODB.Connection")
con.Open "Driver={SQL Server Native Client 10.0};Server=" & sqlInstance & ";Database=myDb;Trusted_Connection=yes;"
MsgBox "ODBC connection to " & sqlInstance & " established."
con.Close
Set con = Nothing
End Sub
...但是当我ODBC;
在连接字符串的开头添加一个子句时...
Sub adoTest()
Dim con As Object
Const sqlInstance = ".\SQLEXPRESS"
Set con = CreateObject("ADODB.Connection")
con.Open "ODBC;Driver={SQL Server Native Client 10.0};Server=" & sqlInstance & ";Database=myDb;Trusted_Connection=yes;"
MsgBox "ODBC connection to " & sqlInstance & " established."
con.Close
Set con = Nothing
End Sub
...失败并显示“[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified”。
因此,上面第一个代码示例ODBC;
中的前缀表明该方法只是一个 Access 快捷方式,用于从 ODBC 数据源中提取数据,而无需显式创建链接表。(方括号内的内容将是链接表对象的属性。)Sub odbcTest()
FROM [ODBC;...]
.Connect
TableDef
因此,据我所知,Access 仅支持 ODBC 链接表(而不支持 OLEDB 链接表),因此对 OLDEB 使用此方法将不起作用。