SQL查询忽略它引用的控件

时间:2015-10-09 07:44:16

标签: sql ms-access ms-access-2010

我有一个Access Form,其中包含一个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)设置断点。

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

我的问题: 为什么会发生这种情况?如何根据所有下拉字段的值立即过滤?

4 个答案:

答案 0 :(得分:1)

在评论中,请尝试:

Isolation mode: Sandbox

答案 1 :(得分:1)

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

答案 2 :(得分:1)

很高兴听到你解决了你的问题。为了详细阐述我的评论,我将放置代码来改变列表框的控制源。也许别人有一天会发现它很有用。任何评论也欢迎。

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

答案 3 :(得分:0)

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

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