简体   繁体   English

MS Access SELECT DISTINCT WHERE查询作为组合框行源

[英]MS Access SELECT DISTINCT WHERE query as combobox row source

I want to simplify a bit of code I've been working on in a MS Access 2010 database I've inherited. 我想简化我在继承的MS Access 2010数据库中一直在研究的代码。 I want the user to enter a date in a form in a field [txtSelectDate], and then select from a combobox any [ProductName] for which a record is in the [uploads] table on the user's selected date. 我希望用户在[txtSelectDate]字段中的表单中输入日期,然后从组合框中选择用户在所选日期的[上载]表中记录的任何[产品名称]。

Currently I have a special query designed to do that, but I was brushing up on my SQL and I wondered if I could do it completely within an SQL expression in the Property Sheet> Data > Row Source field. 当前,我有一个专门用于执行此操作的查询,但是我正在重新检查SQL,我想知道是否可以在“属性表”>“数据”>“行源”字段中的SQL表达式中完全完成此操作。

My attempt was: 我的尝试是:

SELECT DISTINCT [Uploads].[ProductName] 
FROM Uploads 
WHERE [Uploads].[ImportDate] = [Forms]![Review]![txtSelectDate] 
ORDER BY [ProductName];

Which feels to me like it should work, but it gives me a blank combo with no options in it. 在我看来它应该可以工作,但是它给了我一个空白的组合,里面没有选项。 If I take away the WHERE, I get options, but I get every option, regardless of if the [ProductName] value in question was actually in the Uploads on the date selected. 如果删除WHERE,则会得到选项,但是会得到所有选项,无论所讨论的[ProductName]值是否在所选日期实际上位于“上载”中。

I think you need to refresh your form after the date is entered- assuming they're on the same form, add an after update event to your text box: 我认为您需要在输入日期后刷新表单-假设它们位于同一表单上,请在文本框中添加更新后事件:

Private Sub txtSelectDate_AfterUpdate()
    Me.refresh
End Sub

检查您的txtSelectDate是否正确转换为Date类型(或至少与Uploads.ImportDate匹配,无论它是什么类型),或者您的查询将返回零行?

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

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