SQL 查询忽略它引用的控件

SQL Query ignores controls that it refrences

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

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) 通过设置断点来判断。

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

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

评论点赞,试试:

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;

很抱歉陈述显而易见的问题,但是在您更新国家/地区组合框之后,其他文本框的值是否仍然存在?尝试将它们传递到消息框或将它们存储在变量中,您可以观察这些变量以准确了解传递给查询的参数

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

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

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

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