在 QBE 中查询 returns 结果,但不是通过 VBA 代码
Query returns results in QBE, but not via VBA code
在 Access 2010 中针对 Access 数据库工作时,我在 QBE 中创建了一个查询。作为过于复杂的季度报告流程的一部分,我现在需要能够通过 VBA 代码使用不同的参数执行该查询。查询的 SQL 现在嵌入到我的 VBA 模块中,并即时修改,然后执行。当在 QBE 中 运行 时,查询的这个特定实例 returns 400+ 行数据,但是 none 在 VBA 中通过以下代码执行时返回 [=13] =]
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 window 和 运行 中,我得到 400 多行这个确切查询的结果
来自 ADO 的查询 运行 需要 ANSI 通配符:%
而不是 *
; _
而不是 ?
。
所以改变这个...
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName*' ) "
到这个...
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName%' ) "
如果您想要一个在 ADO 中使用 运行 时与在 QBE 中使用 运行 时相同的查询,您可以使用 ALike
而不是 Like
.使用 ALike
,数据库引擎始终需要 ANSI 通配符。
"WHERE (((PatientStatSchedDataDump.PtCompID) ALike 'ClientName%' ) "
在 Access 2010 中针对 Access 数据库工作时,我在 QBE 中创建了一个查询。作为过于复杂的季度报告流程的一部分,我现在需要能够通过 VBA 代码使用不同的参数执行该查询。查询的 SQL 现在嵌入到我的 VBA 模块中,并即时修改,然后执行。当在 QBE 中 运行 时,查询的这个特定实例 returns 400+ 行数据,但是 none 在 VBA 中通过以下代码执行时返回 [=13] =]
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 window 和 运行 中,我得到 400 多行这个确切查询的结果
来自 ADO 的查询 运行 需要 ANSI 通配符:%
而不是 *
; _
而不是 ?
。
所以改变这个...
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName*' ) "
到这个...
"WHERE (((PatientStatSchedDataDump.PtCompID) Like 'ClientName%' ) "
如果您想要一个在 ADO 中使用 运行 时与在 QBE 中使用 运行 时相同的查询,您可以使用 ALike
而不是 Like
.使用 ALike
,数据库引擎始终需要 ANSI 通配符。
"WHERE (((PatientStatSchedDataDump.PtCompID) ALike 'ClientName%' ) "