简体   繁体   English

在Access 2010查询中使用IF THEN

[英]Using IF THEN in Access 2010 Query

I'm not very knowledgeable in coding of Access queries, so I hope someone can help with this issue. 我对Access查询的编码不是很了解,所以希望有人可以帮助解决这个问题。 I have a query (using the query builder) that has a field named RetrainInterval from table tblProcedures (this will return a number like 1, 3, 6, 12, etc.; the rotational months the particular document have to be retrained on) and another field named Training/Qualification Date from table tblTrainingRecords . 我有一个查询(使用查询生成器),该查询具有来自表tblProcedures名为RetrainInterval的字段(这将返回一个数字,例如tblProcedures等;必须重新训练特定文档的轮换月份),并且表tblTrainingRecords另一个名为Training/Qualification Date tblTrainingRecords

I want the query to look at the RetrainInterval for a given record (record field is ClassID in tblProcedures ) and then look at the Training/Qualification Date and calculate if that record should be in the query. 我希望查询查看给定记录的RetrainInterval (记录字段在tblProceduresClassID ),然后查看Training/Qualification Date并计算该记录是否应在查询中。

In a module I would do this: 在模块中,我将执行以下操作:

IF RetrainInterval = 1 Then
DateAdd("m",1,[Training/Qualification Date]) <add to query if <=today()+30>
ElseIf RetrainInterval = 3 Then
DateAdd("m",3,[Training/Qualification Date]) <add to query if <=today()+30>
ElseIF......

How can I translate this into something that would work in a query? 如何将其转换为可以在查询中使用的内容? My end goal is to generate a report that will show me what document class numbers are due within a specified time interval (say I enter 30 in the form textbox to represent any upcoming required training within 30 days of the query), but all of the calculations to determine this is based off of when the last training date was (stored in the training records table). 我的最终目标是生成一个报告,该报告将向我显示在指定的时间间隔内到期的文档类编号(例如,我在表单文本框中输入30,以表示查询后30天内即将进行的任何所需培训)。确定上次培训日期的时间(存储在培训记录表中)的基础上进行的计算。 I also want to make sure that I do not get multiple returns for the same class number since there will be multiple training entries for each class, just grab the minimum last training date. 我还想确保同一班级编号不会得到多次回报,因为每个班级都会有多个培训条目,只是获取最少的最后一次培训日期。 I hope I explained it well enough. 我希望我解释得足够好。 It's hard to put this into words on what I am trying to do without positing up the entire database. 在不存储整个数据库的情况下,很难说出我要做什么。

UPDATE 更新

I think I have simplified this a bit after getting some rest. 我想休息一会后,我已经简化了一点。 Here are two images, one is the current query, and one is what comes up in the report. 这是两张图片,一幅是当前查询,一幅是报告中显示的内容。 I have been able to refine this a bit, but now my problem is I only want the particular Class to show once on the report, not twice, even though I have multiple retrain due dates (because everything is looking at the table that holds the employee training data and will have multiple training's for each Class number). 我已经能够对此进行一些改进,但是现在我的问题是,即使我有多个重新培训的到期日期,我也只希望特定的类在报表上显示一次,而不是两次(因为所有内容都在查看包含员工培训数据,并且每个班级编号都会有多个培训)。 I would like to only show one date, the oldest. 我只想显示一个日期,最早的。 Hope that makes sense. 希望有道理。

Query - http://postimg.org/image/cpcn998zx/ 查询-http: //postimg.org/image/cpcn998zx/

Report - http://postimg.org/image/krl5945l9/ 报告-http://postimg.org/image/krl5945l9/

When RetrainInterval = 1, you add 1 month to [Training/Qualification Date] . 如果RetrainInterval = 1,则在[培训/资格日期]上增加1个月。

When RetrainInterval = 3, you add 3 months to [Training/Qualification Date] . RetrainInterval = 3时,您将[培训/资格日期]增加3个月。

And so on. 等等。

The pattern appears to be that RetrainInterval is the number of months to add. 模式似乎是RetrainInterval是要添加的月份数。 If that is true, use RetrainInterval directly in your DateAdd() expression and don't bother about IF THEN . 如果是这样, 在您的DateAdd()表达式中直接使用RetrainInterval ,不要担心IF THEN

DateAdd("m", RetrainInterval, [Training/Qualification Date])

You can not do that in a query. 您不能在查询中这样做。 Been there, cursed that! 到那儿,诅咒那个!

You can use the IFF( 2>x ; 1 ;0) Giving that if the first statement is true, 1 is returned, and 0 if false. 您可以使用IFF(2> x; 1; 0)来确定如果第一条语句为true,则返回1,如果为false,则返回0。 You can not return a criteria like IFF(2>x ; Cell>2 ; Cell>0) (Not possible) It will just return 0 if you try, i think. 您不能返回类似IFF(2> x; Cell> 2; Cell> 0)的条件(不可能)(如果可能),我想它只会返回0。 it will not give an error all the time. 它不会一直显示错误。

You have to use criterias! 您必须使用条件! I would to something like this picture: 我想要这样的图片: 例

I hope you follow, else let me know. 希望您关注,否则请告诉我。

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

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