如何在Access中进行查询将一个tempvar识别为有效字段(参数)

时间:2019-07-19 18:21:46

标签: sql ms-access ms-access-macros

我正在努力在Access中设置一个宏,该宏将自动运行追加查询。查询将向特定表中添加新行,并基于日期(星期结束)提供新行的数据。目前,我认为每个查询都会提示您请求的数据的结束日期。我真的不想运行宏,以使提示多次显示以输入相同的日期。

根据我在其他主板上的发现,应该可以通过在宏中使用SetTempVar操作定义一个tempvar来实现此目的,并从弹出的Input框提取变量。我将输入框的结果格式化为日期。宏的其余部分设置为对每个查询运行OpenQuery操作。在查询本身中,我将对先前提示的参数([输入结束日期])使用的位置替换为对tempvar的引用([TempVars]![EndDate])。

宏代码:

SetTempVar
      Name: EndDate
Expression: CDate(InputBox("Enter the end date of the week being added:","End Date","Enter Date"))

OpenQuery
Query Name: qryBizDByPersonAppend
      View: Datasheet
 Date Mode: Edit

OpenQuery
Query Name: qryBizDByTypeAppend
      View: Datasheet
 Date Mode: Edit

OpenQuery
Query Name: qryBizDByWhereAppend
      View: Datasheet
 Date Mode: Edit


Code for one of the queries:

TRANSFORM Count(tblCombined.Calendar) AS CountOfCalendar
SELECT [TempVars]![EndDate] AS [End]
FROM tblCombined
WHERE (((tblCombined.StartTime) Between #1/1/2019# And #12/31/2019#) AND ((tblCombined.Type) In ("Advisor Meeting","Advisor Training","Employee Group Presentation","Employer HR Meeting","Client Group Presentation","Client Group Webinar","Employee Group Presentation","Employee Group Webinar","Benefit Fair","Added Time")) AND ((tblCombined.Canceled) Is Null) AND ((tblCombined.DateScheduled)<=[TempVars]![EndDate]))
GROUP BY [TempVars]![EndDate]
PIVOT tblCombined.Calendar;

运行宏时,出现错误,提示[TempVars]![EndDate]一旦进入OpenQuery操作,就不会被识别为有效的字段或表达式。以前使用个别提示的参数可以正常工作。

1 个答案:

答案 0 :(得分:0)

查询无法识别temp var的原因是因为它没有在任何地方定义(在实际字段中或作为“参数”)。通过在每个查询中指定它,我能够使temp var用作参数。 Screenshot of tempvar defined in query as parameter.