Microsoft访问IIf的查询

时间:2016-06-07 16:11:28

标签: ms-access

我有一个包含3个值的组合框:"是,否,*"。 *旨在表示是和否选项。

在名为service的字段的条件框内的查询中。已完成我已下面的IIf声明:

IIf([Forms]![serviceReportForm]![workcompletedDropdown] =" Yes",([service]。[CompletedOn])不为空,([service]。[CompletedOn])Null )

我正在尝试确保在组合框中选择“是”时,会显示包含已完成的值的任何记录。如果未选择,则不显示,如果选择*,则显示所有记录。目前,代码根本没有显示任何记录。你能告诉我吗?

下面的SQL

SELECT customer.CustomerID,
 customer.[Customer Name],
 Equipment.Description,
 Equipment.PurchaseID,
 service.Date,
 service.Fault,
 service.JobNo,
 service.costOfRepair,
 service.ServiceID,
 service.CompletedOn,
 CalloutUrgency.CalloutDescription,
 CalloutUrgency.CalloutUrgencyID,
 service.AgentID,
 agent.Serviceagentname,
 supplier.supplierID,
 supplier.supplierName,
 Equipment.ManufacturerID,
 Manufacturer.[Manufacturer Name]
FROM Manufacturer 
INNER JOIN ((customer 
INNER JOIN (supplier 
INNER JOIN Equipment 
    ON supplier.supplierID = Equipment.SupplierID) 
    ON customer.CustomerID = Equipment.CustomerID) 
INNER JOIN (agent 
INNER JOIN (CalloutUrgency 
INNER JOIN service 
    ON CalloutUrgency.CalloutUrgencyID = service.CalloutUrgencyID) 
    ON agent.AgentID = service.AgentID) 
    ON Equipment.EquipmentID = service.EquipmentID) 
    ON Manufacturer.ManufacturerID = Equipment.ManufacturerID
WHERE (((customer.[Customer Name]) Like [Forms]![ServiceReportForm]![houseDropdown]) 
AND ((service.Date) Between [Forms]![ServiceReportForm].[fromDate] And [Forms]![ServiceReportForm].[toDate]) 
AND ((service.CompletedOn)=IIf([Forms]![ServiceReportForm]![workcompletedDropdown]="Yes",Not Null,Null)) 
AND ((CalloutUrgency.CalloutDescription) Like [Forms]![ServiceReportForm]![urgencyDropdown]) 
AND ((agent.Serviceagentname) Like [Forms]![ServiceReportForm]![serviceagentDropdown]) 
AND ((supplier.supplierName) Like [Forms]![ServiceReportForm]![supplierDropdown])) 
    OR (((service.Date) Between [Forms]![ServiceReportForm].[fromDate] And [Forms]![ServiceReportForm].[toDate]) 
    AND (([Forms]![ServiceReportForm])=" All"));

2 个答案:

答案 0 :(得分:2)

你正在处理IIF函数,好像它返回一个字符串然后插入到你的查询中然后执行查询。但是,这不是处理事物的顺序。 IIF应返回要检查的值或布尔本身。替换这个

((service.CompletedOn)=IIf([Forms]![ServiceReportForm]![workcompletedDropdown]="Yes",Not Null,Null)) 

用这个

IIf([Forms]![ServiceReportForm]![workcompletedDropdown]="Yes",service.CompletedOn is Not Null,service.CompletedOn is Null)

另请注意,由于您无法使用=测试null,因此我删除了=,您必须使用is

在选择*时返回所有记录

IIF([Forms]![ServiceReportForm]![workcompletedDropdown] = "*"
  ,True
  , IIf([Forms]![ServiceReportForm]![workcompletedDropdown]="Yes"
    ,service.CompletedOn is Not Null
    ,service.CompletedOn is Null))

答案 1 :(得分:0)

它必须与null检查语法和使用IIF

有关

在SQL

中替换它

((service.CompletedOn)=IIf([Forms]![ServiceReportForm]![workcompletedDropdown]="Yes",Not Null,Null))

有了这个

((service.CompletedOn Is Not Null) AND ([Forms]![ServiceReportForm]![workcompletedDropdown]="Yes")) OR ((service.CompletedOn Is Null) AND ([Forms]![ServiceReportForm]![workcompletedDropdown]<>"Yes"));