协助ms访问sql查询

时间:2013-10-17 17:17:00

标签: sql ms-access filtering

我有一个名为“搜索”的表单,其中包含有关“交易搜索”下的部分的问题。它根据全名(Combo40)的选择调用查询,该查询来自名为“个人”的表格,用户在Combo40中选择的委员会在委员会字段中做出了贡献:

委员会查询:

SELECT Transactions.[Committee Name] FROM Transactions GROUP BY Transactions.[Committee   Name], Transactions.[Employee Name] HAVING (((Transactions.[Employee Name])=[Forms]![Search]![Combo40]));

我正在尝试添加一个额外的参数,该参数还会比较个人提供的年份,这是在交易表格中称为Combo46的特定字段。

新查询:

SELECT Transactions.[Committee Name] FROM Transactions GROUP BY Transactions.           [Committee Name], Transactions.[Employee Name] HAVING (((Transactions.[Employee Name])=Forms!Search!Combo40) And (((Transactions.Combo46)=Forms!Search!Combo51)))

Forms!Search!Combo40 = pulls the values from a table called “Individuals”
Transactions.Combo46 = year (i.e. 2011, 2012, 2013 etc.)
Forms!Search!Combo51 = year (i.e. 2011, 2012, 2013 etc.)

当我在表单上做出选择时,我会看到一个窗口,要求我输入Combo 46的值,委员会列表显示该个人的所有记录,而不仅仅是我输入的年份的交易。我希望它根本不会出现弹出窗口,而是从Transactions表中获取Combo46的值,并将其与Combo51中输入的值进行比较。

screenshot1

screenshot2

2 个答案:

答案 0 :(得分:3)

评论太长而且不清楚,所以我使用这个空间:

我认为表列Transactions.Combo46不存在,所以Transactions forCombo46由Access for Input填充,它应该是Transactions.Year或者其他含义:

SELECT Transactions.[Committee Name]
FROM Transactions
GROUP BY Transactions.[Committee Name], Transactions.[Employee Name]
HAVING (((Transactions.[Employee Name])=Forms!Search!Combo40)
And (((Transactions.Year)=Forms!Search!Combo51)))

答案 1 :(得分:2)

jacouh所说的肯定是答案(例如,当Combo46成为Transactions的成员时,您的SQL指的是Forms!Search的成员。作为附录,我会简化这样的事情:

(1)将组合框命名为合理的(例如cboNamecboYear),而不是将它们留作Combo40Combo51(哎呀!)。

(2)我似乎不需要GROUP BY

SELECT DISTINCT [Committee Name] FROM Transactions
WHERE ([Employee Name] = Forms!Search!cboName) AND ([Year] = Forms!Search!cboYear)