尝试设置一个查询,其中一个字段由组合框中的值选择。无法找出正确的语法。我在查询构建器的SQL视图中使用以下命令:
SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, Deviations.Active, Deviations.[Forms]![DeviationSelectionForm]![cmbAircraft]
FROM Deviations
WHERE Deviations.[Forms]![DeviationSelectionForm]![cmbAircraft]=True
ORDER BY Deviations.Deviation DESC
组合框选择飞机的尾号,这是表格中的复选框。有多个飞机尾号作为复选框字段。因此,如果没有组合框并且我希望查询用作示例飞行器416,则查询将是:
SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, Deviations.Active, Deviations.[416]
FROM Deviations
WHERE Deviations.[416]=True
ORDER BY Deviations.Deviation DESC
当我测试这个查询时,它可以正常工作。
所以问题是如何使用组合框以标识表的字段名称的形式创建查询?
答案 0 :(得分:1)
在combobox afterUpdate事件中构建查询SQL viacode。
Dim strSql as String
strSql = "SELECT Deviations.Deviation, Deviations.Rev, Deviations.Title, Deviations.Issued, " _
& "Deviations.ExpiryDate, Deviations.ExpiryHours, Deviations.ExpOther, " _
& "Deviations.Active, Deviations." & Me!cmbAircraft _
& " FROM Deviations " _
& "WHERE Deviations." & Me!cmbAircraft & " =True " _
& "ORDER BY Deviations.Deviation DESC"
CurrentDb.QueryDefs(<yourqueryname>).SQL = strSql
也就是说,您似乎已经构建了一个电子表格而不是数据库。 您的表格未规范化。飞机应该是记录,而不是列。我建议你阅读数据库设计和规范化。