2015-10-09 105 views
0

我有一个包含一个列表框,但未通过下面的查询数据的访问窗体控件​​:SQL查询忽略它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)); 

我已经设置了查询所引用的各种控件运行相同的SQL以上陈述点击各种下拉字段的_AfterUpdate事件。他们都执行,我可以告诉a)列表框更新和b)通过设置断点。

的问题是这样的: 当我改变下拉字段的值乡为例,它过滤的国家。如果我为税收设置了下拉字段,则它为税收提供文件,但忽略国家/地区下拉控件中设置的值(以及其他下拉菜单中的所有值)。

我的问题: 为什么会出现这种情况,我怎么能得到它的基础上的,一旦所有的下拉字段的值来过滤?

+0

这是所有'_AfterUpdate'事件运行查询?因为我没有看到你提到的“税收”过滤器? – Wouter

+0

@Wouter是这是所有_AfterUpdate事件运行的查询。我拿出税表来实验......对此抱歉。我会放回去并更新。 – rohrl77

+1

据我所知,SQL基本上没有错。但你不应该使用HAVING作为过滤器(也许这是问题)。在WHERE语句中使用它。 HAVING是用于聚合函数的,参见这里http://www.w3schools.com/sql/sql_having.asp – asdev

回答

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; 
+0

我刚试过,但我仍然得到相同的效果。 – rohrl77

+0

好吧,我很憎恶它没有改善任何东西。 @Wouter与vba的评论答案是合理的。我每次需要时都会使用这种方法。SQL更短,你不需要SQL中的NZ功能。 – asdev

+0

我保存了你的SQL ...所以非常感谢你!事实上,我刚刚设法解决了这个问题。你和Sheils Barra和@Wouter肯定帮助我走上正轨! – rohrl77

1

对不起不言自明的,但其他的文本框的值仍然存在后,你有更新的国家组合框?尝试将它们传递给消息框或将其存储在变量中,以便查看传递给查询的确切参数

+0

值在那里。我用'debug.print'来检查。好的建议,但。 – rohrl77

0

为了完整起见,我的回答很容易在未来找到其他人:

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

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 
+0

更改功能分请 – asdev

+0

我做了一个函数,因为它通过宏引用,我无法引用一个子。 – Wouter

+0

@Wouter感谢您的支持。我可能会回到未来的案例。 – rohrl77