简体   繁体   English

为什么我的查询不使用我的条件?

[英]Why doesn't my query use my criteria?

I have a db in Access and I'm trying to get a textbox to run my query and pass an other bounded textbox's value in as the criteria in DLookUp . 我在Access中有一个数据库,我试图获取一个文本框来运行我的查询,并在DLookUp传递其他有界文本框的值作为条件。 I have the query running in design view and when I enter the criteria directly it returns the correct results. 我在设计视图中运行查询,当我直接输入条件时,它将返回正确的结果。 When I open the report it gives me the sum of all the possible rows. 当我打开报告时,它给了我所有可能的行的总和。 In other words it doesn't filter the rows. 换句话说,它不会过滤行。

I haven't used Access in about twelve years, thankfully , and everything I've done up to this point has been tutorial/example patchwork, but here it is... 幸运的是 ,我已经十二年没有使用Access了,到目前为止,我所做的一切都是教程/示例拼凑而成的,但这是...

SQL Query: SQL查询:

SELECT Sum(IIf(Attended=-1,1,0)) AS attendance
FROM Students_Classes_Attendance
WHERE (((CStr([Students_Classes_Attendance].[Class_Id]))=[classId]));

DLookUp as Control Source: DLookUp作为控制源:

=DLookUp("[Total Attendance by Class]![attendance]",
         "[Total Attendance by Class]",
         "[Class_Id] =" & [Class_Id])

I'm lost at the moment. 我现在迷路了。 I'm guessing that the value isn't there before the query fires and since the criteria is an optional parameter that it's being passed null, but I would hope you'd get an error from that. 我猜测查询触发之前该值不存在,并且由于条件是将其传递为null的可选参数,但我希望您能从中得到一个错误。 Not that #Error is very meaningful anyway. 并不是说#Error还是很有意义的。

Does anyone know for certain the problem and the best way to correct it? 有谁能肯定地知道该问题以及纠正该问题的最佳方法? Thanks. 谢谢。

Edit: 编辑:
I did the changes recommended in the answer so now my DLookUp looks like... 我做了答案中建议的更改,所以现在我的DLookUp看起来像...

=DLookUp("[attendance]",
         "[Total Attendance by Class]",
         "[Class_Id] =" & [Class_Id])

...still returns the total for all rows. ...仍然返回所有行的总数。 Removing the criteria completely makes no difference either, which returns me to thinking it has something to do with the bound textbox not having a value. 完全删除条件也没有任何区别,这使我回想起它与绑定文本框没有值有关。

DLookup uses the following syntax: DLookup使用以下语法:

  1. Syntax for numerical values: 数值的语法:
    DLookup("FieldName" , "TableName" , "Criteria = n") DLookup(“ FieldName”,“ TableName”,“ Criteria = n”)

  2. Syntax for strings: (note the single apostrophe before and after the string value) 字符串的语法:(请注意字符串值前后的单撇号)
    DLookup("FieldName" , "TableName" , "Criteria= 'string'") DLookup(“ FieldName”,“ TableName”,“ Criteria ='string'”)

  3. Syntax for dates: (note the # before and after the date value) 日期的语法:(请注意日期值之前和之后的#)
    DLookup("FieldName" , "TableName" , "Criteria= #date#") DLookup(“ FieldName”,“ TableName”,“ Criteria =#date#”)

I believe you just need to remove the table name from the first parameter. 我相信您只需要从第一个参数中删除表名即可。 Try this: 尝试这个:

=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = " & [Class_Id])

Keep in mind that if Class_Id is a Text Field, you need to surround it by single quotes: 请记住,如果Class_Id是文本字段,则需要用单引号将其引起来:

=DLookUp("[attendance]", "[Total Attendance by Class]", "[Class_Id] = '" & [Class_Id] & "'")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM