SQL 查询忽略它引用的控件

Posted

技术标签:

【中文标题】SQL 查询忽略它引用的控件【英文标题】:SQL Query ignores controls that it refrences 【发布时间】:2015-10-09 07:44:15 【问题描述】:

我有一个访问表单,其中包含一个从以下查询中获取数据的 ListBox:

SELECT tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR AS [Retro Frei], 
tblMorningstar_Data.[DEU Tax Transparence], tblMorningstar_Data.[Distribution Status], 
tblISIN_Country_Table.Country

FROM 
(tblFUNDS INNER JOIN tblISIN_Country_Table ON tblFUNDS.ISIN = tblISIN_Country_Table.ISIN) 
INNER JOIN tblMorningstar_Data ON 
(tblFUNDS.Fund_Selection = tblMorningstar_Data.Fund_Selection) 
AND (tblFUNDS.ISIN = tblMorningstar_Data.ISIN)

GROUP BY tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR, 
tblMorningstar_Data.[DEU Tax Transparence], tblMorningstar_Data.[Distribution Status], 
tblISIN_Country_Table.Country, tblFUNDS.Fund_Selection

HAVING (((tblFUNDS.RDR) Like Nz([Forms]![frmMain]![ddnRDR],'*')) AND
((tblMorningstar_Data.[DEU Tax Transparence]) Like Nz([Forms]![frmMain]![ddnTax],'*')) AND
((tblMorningstar_Data.[Distribution Status]) Like Nz([Forms]![frmMain]![ddnDistribution],'*')) 
AND ((tblISIN_Country_Table.Country) Like Nz([Forms]![frmMain]![ddnCountry].[Text],'*')) 
AND ((tblFUNDS.Fund_Selection)=0));

我已经设置了查询引用的各种控件,以便在单击各种下拉字段的_AfterUpdate 事件上运行上述相同的 SQL 语句。它们都执行了,我可以通过 a) 更新列表框和 b) 设置断点来判断。

问题是这样的: 例如,当我更改国家/地区下拉字段的值时,它会按国家/地区过滤。如果我随后为税设置下拉字段,它会过滤税,但忽略国家下拉控件中设置的值(以及其他下拉列表中的所有值)。

我的问题: 为什么会发生这种情况,我怎样才能让它一次根据所有下拉字段的值进行过滤?

【问题讨论】:

这是所有_AfterUpdate 事件运行的查询吗?因为我没有看到你提到的“税收”过滤器? @Wouter 是的,这是所有 _AfterUpdate 事件运行的查询。我拿出了税务报表进行实验……对此感到抱歉。我会把它放回去更新。 SQL在我看来基本没有错。但是您不应该将 HAVING 用于过滤器(也许这是问题所在)。在 WHERE 语句中使用它。 HAVING 用于聚合函数,请参阅此处w3schools.com/sql/sql_having.asp 或许还有其他方式,涉及到一点vba编码。您可以将列表框的记录源设置为您在 VBA 中创建的查询。我以前在子表单中成功地做到了这一点。您必须创建一个模块来检查每个文本框是否为空。如果没有,您可以在变量中创建过滤器,之后您可以将所有变量组合在一起。 问题是 ddnCountry 表单的绑定列引用了错误的列。它与我使用 ListBox 查询的列不匹配!感谢您的帮助! 【参考方案1】:

喜欢评论,试试:

SELECT tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR AS [Retro Frei], tblMorningstar_Data.[DEU Tax Transparence], tblMorningstar_Data.[Distribution Status], tblISIN_Country_Table.Country

FROM (tblFUNDS INNER JOIN tblISIN_Country_Table ON tblFUNDS.ISIN = tblISIN_Country_Table.ISIN) INNER JOIN tblMorningstar_Data ON (tblFUNDS.Fund_Selection = tblMorningstar_Data.Fund_Selection) AND (tblFUNDS.ISIN = tblMorningstar_Data.ISIN)

WHERE (((tblFUNDS.RDR) Like Nz([Forms]![frmMain]![ddnRDR],'*')) AND ((tblMorningstar_Data.[DEU Tax Transparence]) Like Nz([Forms]![frmMain]![ddnTax],'*')) AND ((tblMorningstar_Data.[Distribution Status]) Like Nz([Forms]![frmMain]![ddnDistribution],'*')) AND ((tblISIN_Country_Table.Country) Like Nz([Forms]![frmMain]![ddnCountry].[Text],'*')) AND ((tblFUNDS.Fund_Selection)=0))

GROUP BY tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR, tblMorningstar_Data.[DEU Tax Transparence], tblMorningstar_Data.[Distribution Status], tblISIN_Country_Table.Country, tblFUNDS.Fund_Selection;

【讨论】:

我刚试了一下,效果还是一样。 好吧,我很遗憾它没有改善任何东西。 @Wouter 与 vba 的评论答案是合理的。每次需要时,我也会使用此方法。 SQL 更短,SQL 中不需要 NZ 函数 我保留了您的 SQL...非常感谢您!事实上,我刚刚设法解决了这个问题。你、Sheils Barra 和 @Wouter 无疑帮助我走上了正轨! 在让 ListBox 正确排序后,它引起了后续问题,因为我在一段代码中使用了相同的 SQL 查询,现在不再工作了......当我更改 SQL回到我原来的样子,ListBox 不再正确排序了。这是一个很长的说法:您提供了使其正常工作的主要信息,这就是为什么我将其标记为答案!【参考方案2】:

很抱歉,在您更新国家/地区组合框后,其他文本框的值是否仍然存在?尝试将它们传递到消息框或将它们存储在变量中,您可以观察以查看传递给查询的确切参数

【讨论】:

值在那里。我使用debug.print 进行检查。不过建议很好。【参考方案3】:

很高兴听到您解决了问题。为了详细说明我的评论,无论如何我都会在此处放置更改列表框控制源的代码。也许有一天其他人会发现它很有用。也欢迎任何 cmets。

Public Function Rapport_query()
Dim sqlTax As String
Dim sqlRDR As String
Dim sql As String
Dim selectQuery As String
Dim whereStatement As String
Dim i As Integer
Dim i1 As Integer
Dim i2 As Integer

'set counter (because if the filter is not the first there should be an "AND" operator before the filter.
i = 0

'check if the combobox is empty, if it's not use the input as input for you where statement
    If Not (IsNull(Forms!frmMain!ddnRDR)) Then
        i1 = i + 1
        sqlRDR = " tblFUNDS.RDR LIKE " & Chr(34) & Forms!frmMain!ddnRDR & Chr(34)
        i = i + 1
    End If

    If Not (IsNull(Forms!frmMain!ddnTax)) Then
        i2 = i + 1
        If i2 > i1 And i > 0 Then
            sqlTax = " AND tblMorningstar_Data.[DEU Tax Transparence] LIKE " & Chr(34) & Forms!frmMain!ddnTax & Chr(34)
        Else
            sqlTax = "tblMorningstar_Data.[DEU Tax Transparence] LIKE " & Chr(34) & Forms!frmMain!ddnTax & Chr(34)
        End If
        i = i + 1
    End If

'if the lenght is 0, there are no filters. Else fill the where statement string
    If Len(sqlRDR & sqlTax) = 0 Then
        whereStatement = ""
    Else
        whereStatement = "WHERE " & sqlRDR & sqlTax
    End If

'set the select query
    selectQuery = "SELECT tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR AS [Retro Frei]," & _
"tblMorningstar_Data.[DEU Tax Transparence], tblMorningstar_Data.[Distribution Status]," & _
"tblISIN_Country_Table.Country " & _
"FROM (tblFUNDS INNER JOIN tblISIN_Country_Table ON tblFUNDS.ISIN = tblISIN_Country_Table.ISIN) " & _
"INNER JOIN tblMorningstar_Data ON (tblFUNDS.Fund_Selection = tblMorningstar_Data.Fund_Selection) " & _
"AND (tblFUNDS.ISIN = tblMorningstar_Data.ISIN) " & _
"GROUP BY tblFUNDS.MorningsStar_Fund_Name, tblFUNDS.ISIN, tblFUNDS.RDR," & _
"tblMorningstar_Data.[DEU Tax Transparence], tblMorningstar_Data.[Distribution Status]," & _
"tblISIN_Country_Table.Country , tblFUNDS.Fund_Selection"
'combine the select query with the variable where statement
    sql = selectQuery & whereStatement

'set the listbox controlsource
    Forms!frmMain.ListBox.ControlSource = sql
End Function

【讨论】:

请把函数改成Sub 我把它变成了一个函数,因为它是通过宏引用的,我无法引用子。 @Wouter 感谢您展示这一点。我可能会在未来的案例中再次使用它。 这很有趣。我不知道你可以使用没有返回类型的函数。总是在学习新的东西。不错【参考方案4】:

为了完整起见,我的答案是为了将来更容易找到其他人:

问题在于 ddnCountry 表单的绑定列引用了错误的列。它与我使用 ListBox 查询的列不匹配!

【讨论】:

以上是关于SQL 查询忽略它引用的控件的主要内容,如果未能解决你的问题,请参考以下文章

SQL 查询数据连接具有它忽略的空值

相关查询和引用子表单控件

如何在sql查询中忽略大小写

可以 Go 的 Rows.Scan 忽略 SQL 查询中的字段

为啥 SQL Server 查询优化器有时会忽略明显的聚集主键?

Ora SQL 查询:无引用加入