我使用Excel根据存储在Access中的数据生成报告。我ping一个特定的查询,以根据单元格的值返回一个完整的数据集。我有3个相同类型的其他模块从另一个查询中获取完美的数据。下面给我一个头疼,因为它没有返回任何记录。当我进入访问并使用相同的SQL字符串创建虚拟查询时,Access将按预期返回记录。有人可以帮忙吗?谢谢,
Sub fc_GetPLP()
Dim Z_Connection As Object
Dim Z_Recordset As Object
Dim Z_OnlineOD As String
Z_OnlineOD = Sheet2.Range("F6").Value
Dim Z_Access_Path As String
Dim Z_Access_File As String
Z_Access_Path = Sheet2.Range("C2").Value
Z_Access_File = Sheet2.Range("C3").Value
Set Z_Connection = CreateObject("ADODB.Connection")
Z_Connection.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Z_Access_Path & Z_Access_File
Set Z_Recordset = CreateObject("ADODB.RECORDSET")
Z_Recordset.activeconnection = Z_Connection
Z_SQL = "SELECT Q_SPA_Pricing.* FROM Q_SPA_Pricing WHERE (Q_SPA_Pricing.Qf_FAR_OD='" & Z_OnlineOD & "')"
Debug.Print Z_SQL
Z_Recordset.Open Z_SQL
Debug.Print Z_Recordset.EOF
Sheet4.Columns("B:H").Clear
Sheet4.Range("B2").CopyFromRecordset Z_Recordset
Z_Recordset.Close
Z_Connection.Close
Set Z_Connection = Nothing
End Sub
例如,我刚刚返回的SQL是:
SELECT Q_SPA_Pricing.* FROM Q_SPA_Pricing WHERE (Q_SPA_Pricing.Qf_FAR_OD='PAR-SIN')
这是预期的,但仅限于Access。 Excel返回一个空的Recordset。
谢谢,
佛瑞德
答案 0 :(得分:0)
尝试以下代码
Sub fc_GetPLP()
Dim Z_Connection As Object
Dim Z_Recordset As Object
Dim Z_OnlineOD As String
Z_OnlineOD = Sheet2.Range("F6").Value
Dim Z_Access_Path As String
Dim Z_Access_File As String
Z_Access_Path = Sheet2.Range("C2").Value
Z_Access_File = Sheet2.Range("C3").Value
Set Z_Connection = CreateObject("ADODB.Connection")
Z_Connection.Open "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & Z_Access_Path & Z_Access_File
Set Z_Recordset = CreateObject("ADODB.RECORDSET")
Z_Recordset.activeconnection = Z_Connection
Z_SQL = "SELECT Q_SPA_Pricing.* FROM Q_SPA_Pricing WHERE (Q_SPA_Pricing.Qf_FAR_OD='" & Z_OnlineOD & "')"
Debug.Print Z_SQL
Z_Recordset.Open Z_SQL
'Debug.Print Z_Recordset.EOF
If Z_Recordset.EOF = False Then
Sheet4.Columns("B:H").Clear
Sheet4.Range("B2").CopyFromRecordset Z_Recordset
End If
Z_Recordset.Close
Z_Connection.Close
Set Z_Connection = Nothing
End Sub