如何通过 VBA 在 Access 报表上创建控件?

How can I create a control on an Access report via VBA?

到目前为止,我在报表上手动创建了有限数量 (12) 的控件,而我的代码基本上是根据用户选择的数量打开或关闭(成功)它们。 现在我的客户希望用户对报告的控件数量 "no limit"。

我的策略是删除控件并动态创建所需的数字(通过代码)。 但是,当我尝试创建控件时,出现 运行 时间错误 2450 "cannot find the referenced form"。 片段:

    DoCmd.OpenReport "My_report", acViewDesign
    Dim ctl As Control
    Set ctl = CreateControl(FormName:="My_report", ControlType:=acTextBox, _
            Section:=acDetail, left:=2880, top:=0, width:=967, height:=312)
    ' ... here lies code that adjusts attributes of the controls
    DoCmd.OpenReport "My_report", acViewReport

执行在 CreateControl 行停止:...找不到引用的表单 'My_report'。 (工作时,实际的CreateControl代码会在一个循环中,几何参数被变量替换。)

我看到的所有讨论都与 forms 有关,但是 CreateControl Method 的文档说第一个参数是 "name of the open form or report on which you want to create the control" 所以它应该适用于 也报告。 请注意,报表 在设计模式下打开的。知道我做错了什么吗?

不幸的是,在线帮助/文档有时是错误的或至少是误导性的。

对此有一个单独的方法CreateReportControl()