我在ms-access中有一个链接表,它链接到一个sql server表,当我试图通过VBA从ms-access链接表获取excel中的数据时,显示错误消息“ODBC connection failed”。
注意: - 我手动成功地能够在ms-access中刷新ms-access链接表,“peoplemain”是链接表的名称。
注意: - 当我尝试从非链接表中获取数据时,它已成功运行。
下面的代码工作在Excel-32位版本,但不能在excel-64位工作。
[码]
Sub FetchData()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim conn As String
conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Workflow Tools (Michael Cantor)\Tool For Fixing Bug From Michael Cantor\PI MDT Reconciliation Workflow Tool\SampleforPractice.accdb;"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open conn
rs.Open "Select * from peoplemain", cn 'Error Line
Sheet1.Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
谢谢
卡希夫
我会尝试将strSQL
设置为这样的字符串(我有同样的问题,当我做出改变时,它对我有效):
Sub FetchData()
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim conn As String, strSQL AS String
conn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=G:\Workflow Tools (Michael Cantor)\Tool For Fixing Bug From Michael Cantor\PI MDT Reconciliation Workflow Tool\SampleforPractice.accdb;"
strSQL = "Select * from peoplemain"
Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset
cn.Open conn
rs.Open strSQL , cn 'Error Line
Sheet1.Range("A1").CopyFromRecordset rs
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub