我有一个带有一些列表框的表单,可用于从列表中选择一个值。我有一个使用表单值作为过滤器的查询。该组合工作正常 - 直到我尝试使用 VBA 脚本打开查询。那时,我收到一条错误消息,说明查询需要参数。代码段是:
查询 - [实体选择] - 使用表单(一次仅使用一个列表框 - 这会找出它是哪一个并从表 [实体列表] 中选择适当的项目):
SELECT val(forms.selections.select_year) as Yr, *
FROM [entity list]
where [group]=forms!selections!select_group
UNION select val(forms.selections.select_year) as Yr, * from [entity list] where [G_SG]=forms!selections!select_subgroup
UNION select val(forms.selections.select_year) as Yr, * from [entity list] where [ID_Name]=forms!selections!select_entity
UNION select val(forms.selections.select_year) as Yr, * from [entity list] where forms!selections!select_group like "All*";
查询 - 使用先前查询的结果:
SELECT ...
FROM [DTXX Detail]
INNER JOIN [Entity selections] ON ([DTXX Detail].Yr = [Entity selections].Yr) AND ([DTXX Detail].entityid = [Entity selections].EntityID)
ORDER BY ...
这些工作正常 - [实体选择] 查询返回适当的行,第二个查询成功地使用第一个作为过滤器并返回适当的行。
然后,我添加了一个 VBA 脚本,将上面的一些报告输出到 Excel 文件中(这个脚本在其他数据库中也可以使用,没有任何表格)。脚本的相关部分是
Source = "[" & Exports.Fields("Source") & "]"
Source = "Select * from " & Source & ";"
If ((Source <> "") And (WS_Name <> "")) Then
'Get the source data
Set source_data = DB.OpenRecordset(Source)
Source
结束为Select * from [report - details]
,这是上面第二个查询的名称。没什么复杂的。我得到的错误是:
Runtime error 3601. Too few parameters. Expected 4
脚本处于活动状态时,查询似乎无法访问表单。但是在监视窗口中,脚本知道所有表单控件的值。
你能帮忙吗?谢谢你。