简体   繁体   English

在访问查询中将 combobox 值作为比较运算符传递?

[英]Pass combobox value as a comparison operator in access query?

I have a form with a combobox "cmb1" and a textbox "txt1".我有一个带有 combobox“cmb1”和一个文本框“txt1”的表单。 the combobox can be empty or can hold a comparison operator ">" or "<" or "=" without the quotes while the value of the comparison can be entered in the textbox txt1 as shown in the picture below frm1 and I have this query as shown below qry_employee combobox 可以为空,也可以包含不带引号的比较运算符“>”、“<”或“=”,而比较值可以输入到文本框 txt1 中,如下图frm1所示,我有这个查询如下图qry_employee

I tried to pass the value from the combobox as a comparison operator and the textbox value in the criteria in the "emplyee_age" field but I couldn't.我试图将 combobox 中的值作为比较运算符传递,并将文本框值传递给“emplyee_age”字段中的标准,但我做不到。 for example例如

  1. If cmb1 is empty, then the query should show all employees.如果 cmb1 为空,则查询应显示所有员工。
  2. If cmb1 value is ">" and txt1 value is "22", then the query should show all employees whose age is greater then 22如果 cmb1 值为“>”且 txt1 值为“22”,则查询应显示所有年龄大于 22 岁的员工
  3. if cmb1 value is "=" and txt1 value is "30", then query should show all employees whose age is equal to 30... etc In short, the comparison operator should be taken from the value of the cmb1 while the value of the comparison should be taken from txt1 and both are used in the criteria field in the query.如果 cmb1 值为“=”且 txt1 值为“30”,则查询应显示年龄等于 30 的所有员工...等等 简而言之,比较运算符应取自 cmb1 的值,而值应该从 txt1 中进行比较,并且两者都用于查询的条件字段中。 I no comparison operator is chosen, the query returns all values.我没有选择比较运算符,查询返回所有值。 I'm using MS Access 2007.我正在使用 MS Access 2007。

I tried for hours but couldn't get it to work.我尝试了几个小时,但无法正常工作。 All your help is highly appreciated.非常感谢您的帮助。

after so many trial and error, I have finally got it to work.经过如此多的尝试和错误,我终于让它工作了。 It works perfectly.它工作得很好。 I will post it here for anyone who might search for the same thing:) here is the sql for the query我会将它发布在这里,供任何可能搜索相同内容的人使用:) 这是查询的 sql

SELECT tbl_employees.ID, tbl_employees.emplyee_name, tbl_employees.employee_age FROM tbl_employees WHERE (((IIf(IsNull([Forms]![frm1]![cmb1]),True,Eval(([tbl_employees].[employee_age]) & [Forms]![frm1]![cmb1] & [Forms]![frm1]![txt1])))<>False));

Have a great day:)祝你有美好的一天:)

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

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