【发布时间】:2015-03-06 20:47:00
【问题描述】:
在 Access 2010 中针对 Access DB 工作时,我在 QBE 中创建了一个查询。作为过于复杂的季度报告流程的一部分,我现在需要能够通过 VBA 代码使用不同的参数执行该查询。查询的 SQL 现在嵌入在我的 VBA 模块中,并在运行中进行修改,然后执行。在 QBE 中运行时,此查询的特定实例返回 400 多行数据,但在 VBA 中通过以下代码执行时没有返回任何数据:
Dim Data As New ADODB.Recordset
Dim SQLString As String
SQLString = "SELECT PatientStatSchedDataDump.PtCompID, AppType.ProviderW, " & _
"PatientStatSchedDataDump.Date, PatientStatSchedDataDump.Status " & _
"FROM (AppType INNER JOIN PatientStatSchedDataDump ON AppType.AType = " & _
"PatientStatSchedDataDump.Type) LEFT JOIN GroupType ON AppType.Group = " & _
"GroupType.Group " & _
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName*' ) " & _
"AND ((PatientStatSchedDataDump.Date) BETWEEN #1/1/2014# AND #3/31/2014#) " & _
"AND ((GroupType.[Sort Order]) IN ('A', 'B', 'C', 'D', 'E')))"
Data.Open Source:=SQLString, ActiveConnection:=CurrentProject.Connection
If Not Data.EOF And Not Data.BOF Then
'the IF is never true - EOF & BOF are always True
NewSheet.Cells(InstCountRow + InstCount + 2, 1).CopyFromRecordset Data
End If
Data.Close
Set Data = Nothing
同样,如果我在 Access 中创建一个新查询,将 SQL 代码粘贴到 SQL 窗口并运行它,我会通过这个精确查询获得 400 多行结果
【问题讨论】:
标签: ms-access vba ms-access-2010