简体   繁体   English

MS Access在SQL字段中使用ComboBox值

[英]MS Access Using ComboBox Value in SQL Field

Trying to set up a query where one of the fields is selected by the value in a combobox. 尝试建立一个查询,其中通过组合框中的值选择字段之一。 Can't figure out the right syntax. 无法找出正确的语法。 I'm in the SQL view of the query builder, using the following: 我在查询生成器的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

The combo box selects the tail number of the aircraft which is a checkbox in the table. 组合框选择飞机的机尾号,该机尾号是表中的复选框。 There are multiple aircraft tail numbers as checkbox fields. 有多个飞机尾号作为复选框字段。 So if there were no combobox and I wanted the query to use as example aircraft 416, the query would be: 因此,如果没有组合框,而我希望该查询用作示例飞机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

When I test this query it works as I need it to. 当我测试此查询时,它可以按需要工作。

So the question is how to I create the query with the combobox in a form identifying the field name of a table? 因此,问题是如何使用组合表以标识表的字段名称的形式创建查询?

Build the Query SQL viacode in the combobox afterUpdate event. 在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

That said, it seems like you have built a spreadsheet instead of a database. 就是说,您似乎已经建立了电子表格而不是数据库。 Your tables are not normalized. 您的表未规范化。 Aircraft should be records, not columns. 飞机应该是记录,而不是栏目。 I suggest you read up on database design and normalization. 我建议您阅读有关数据库设计和规范化的内容。

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

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