我尝试创建一个可以从MS Access中的按钮运行的查询。我创建的SQL查询如下:
PARAMETERS CASNUMBER Text ( 255 );
SELECT DISTINCT Chemical.Chemical_Name, Hazard.Hazard_Code, Hazard.Hazard_Text
FROM Chemical, Chemical_Hazard, Hazard
WHERE Chemical.Chemical_Id = Chemical_Hazard.Chemical_Id
and Chemical_Hazard.Hazard_Id = Hazard.Hazard_Id
and [CASNUMBER] = Chemical.CAS
如何在没有返回值时显示MsgBox?
答案 0 :(得分:0)
我建议使用JOIN
而不是WHERE
来加入表格。此外,您可以避免使用参数,在大多数情况下,参数需要VBA代码才能执行查询。只需创建不带参数的查询,将CAS添加到列列表中:
SELECT DISTINCT Chemical.Chemical_Name
,Hazard.Hazard_Code
,Hazard.Hazard_Text
,Chemical.CAS
FROM (
Chemical_Hazard INNER JOIN Chemical ON Chemical.Chemical_Id = Chemical_Hazard.Chemical_Id
)
INNER JOIN Hazard ON Chemical_Hazard.Hazard_Id = Hazard.Hazard_Id
然后在代码中检查CAS:
If DCount("*", "MyQuery", "CAS=" & lngCASNumber) = 0 then
MsgBox "CAS not found"
End If