访问查询:使用用户输入到 select 列的查询 sql

Access Query: Use user input to select column for query in sql

我是 ACCESS 和 SQL 的新手。如果我误用了一些参考资料和术语,我深表歉意。我正在尝试使用用户输入 select 我的 table 中的一个字段,然后根据用户标准过滤该字段中的数据。

我有一个 table Emissions_Units 有很多列,我想为用户制作一个漂亮的表格 select 污染物(这是我 table) 然后 select 标准适用于该污染物(即 table 中的列)。 table中的污染物或列包括CO_EmissionsNO2_EmissionsPM_Emissions.

然后,一旦该列被 selected,我想根据用户的最大和最小标准过滤该列。然后让查询显示结果。

我包含了一些我一直在处理的 SQL 代码,这些代码不起作用,但可能会显示我正在尝试做的事情。我还尝试了 SWITCH 而不是 IIF。如果可能的话,我还想从 table 的其他列中获取与查询中的行匹配的信息。例如,我想从与满足用户输入结果的行关联的名称和位置列中提取信息。

PARAMETERS [Enter Pollutant to Review (CO, NO2, PM):] Text, [Enter Maximum Emissions (0.00 g/s):] Number, [Enter Minimum Emissions (0.00 g/s):] Number;

SELECT
    IIF([Enter Pollutant to Review (CO, NO2, PM):] = "CO", Emissions_Units.CO_Emissions AS CalcValues,
    IIF([Enter Pollutant to Review (CO, NO2, PM):] = "NO2", Emissions_Units.NO2_Emissions AS CalcValues,
    IIF([Enter Pollutant to Review (CO, NO2, PM):] = "PM", Emissions_Units.PM_Emissions AS CalcValues, 'Retry' )
  )
)
FROM Emissions_Units
WHERE CalcValues >= [Enter Minimum Emissions (0.00 g/s)] 
  AND CalcValues <= [Enter Maximum Emissions (0.00 g/s)];

此查询有效:

PARAMETERS [Enter Pollutant to Review (CO, NO2, PM):] Text, [Enter Minimum Emissions in gps] Number, [Enter Maximum Emissions in gps] Number;

SELECT *, Switch([Enter Pollutant to Review (CO, NO2, PM):] = "CO", Emissions_Units.CO_Emissions,
  [Enter Pollutant to Review (CO, NO2, PM):] = "NO2", Emissions_Units.NO2_Emissions,
  [Enter Pollutant to Review (CO, NO2, PM):] = "PM", Emissions_Units.PM_Emissions) AS CalcValues
 
FROM Emissions_Units

WHERE Switch([Enter Pollutant to Review (CO, NO2, PM):] = "CO", Emissions_Units.CO_Emissions,
  [Enter Pollutant to Review (CO, NO2, PM):] = "NO2", Emissions_Units.NO2_Emissions,
  [Enter Pollutant to Review (CO, NO2, PM):] = "PM", Emissions_Units.PM_Emissions)

BETWEEN [Enter Minimum Emissions in gps] AND [Enter Maximum Emissions in gps];

PARAMETERS 子句不喜欢 0.00,所以我改写了提示。但是,不是查询中的弹出提示,而是引用表单上的控件供用户输入。