简体   繁体   English

在Microsoft Access VBA中将SQL查询分配给RowSource时出错

[英]Error while assigning an SQL Query to RowSource in VBA in Microsoft Access

I'm trying to run an SQL Query in VBA to populate a combobox. 我正在尝试在VBA中运行SQL查询以填充组合框。 I'd like the combobox to display all Start Times ([Schedule Data].[ApptStart]) on a given date, chosen from a date picker ([ApptDate] on the form.) I've tried various iterations of the statement below but can't seem to get anything but errors and a blank combo box. 我希望组合框显示给定日期的所有开始时间([Schedule Data]。[ApptStart]),该日期是从日期选择器(窗体上的[ApptDate]中选择的)。我尝试了以下语句的各种迭代但除了错误和空白组合框外,似乎什么也没得到。

Me.ComboTimeList.RowSource =
 "SELECT [Schedule Data].[ApptStart]
  FROM [Schedule Data]
  WHERE [Schedule Data].[ApptDate] = #" & Me.[ApptDate] & "#"

I don't know if this is just a syntax issue or if I'm missing some bigger concept. 我不知道这仅仅是语法问题,还是我缺少更大的概念。 Any help is appreciated. 任何帮助表示赞赏。 I'm happy to provide any additional needed info. 我很乐意提供任何其他所需的信息。 Thanks! 谢谢!

edit: The full error message reads: 编辑:完整的错误消息显示为:

Syntax error in date in query expression '[Schedule Data].[ApptDate] = #' 查询表达式'[Schedule Data]。[ApptDate] =#'中日期的语法错误

Sorry for neglecting to include that in the original message. 抱歉,忽略了将其包含在原始邮件中。

My code should have been: 我的代码应该是:

Me.ComboTimeList.RowSource =
 "SELECT [Schedule Data].[ApptStart] & _
  FROM [Schedule Data] & _ 
  WHERE [Schedule Data].[ApptDate] = #" & Me.[ApptDate] & "#;"

Additionally, Me.[ApptDate] was returning "Null" because I had the code in the Change event instead of AfterUpdate event. 此外,Me。[ApptDate]返回“ Null”,因为我在Change事件中而不是AfterUpdate事件中包含了代码。

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

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