【发布时间】:2019-06-26 11:43:36
【问题描述】:
我遇到了一个我无法弄清楚的错误。我写了一个子程序,在设计视图中打开一个表单,删除所有动态控件,然后添加请求的数字。
sub 以两种不同的方式被调用。用户打开表单(通过主菜单表单)填写新表单(删除动态控件然后重新创建)或创建表单后,用户可以单击表单以添加更多的控件行。主菜单和按钮窗体都调用相同的子菜单,但是当单击按钮时代码卡住并且错误 29054 'Microsoft Access 无法添加、重命名或删除控件( s) 你请求的。' 被抛出。调试按钮已停用,因此我看不到实际卡住的是哪一行,但是当我单步执行代码时,这是弹出错误之前的最后一行(下面上下文中的最后一个缩进块):
With Application.CreateControl("BOM5", acComboBox, acDetail, frm.Controls("Tabs").Pages(PageNum).Name, , ChangeTypeLeft, LastControlTop, ChangeTypeWidth, ControlHeight)
其余代码如下。
DoCmd.OpenForm "BOM5", acDesign
Set frm = Application.Forms("BOM5")
' Cycle through controls and set LastControlTop based on the last dynamic control, if any
For i = 0 To frm.Controls.Count - 1
Set ctl = frm.Controls(i)
Debug.Print ctl.Name
If ctl.Tag Like DYNAMIC_TAG & "*" Then
If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
LastControlTop = ctl.Top + ControlHeight + ControlPadding
FormRowCount = FormRowCount + 1
End If
Else
FormRowCount = (FormRowCount / 6) + 1 ' Convert number of fields to number of rows then add one to start new batch of controls
Exit For
End If
Next
PageNum = frm.Controls("Tabs").Pages.Count - 1 ' .Pages has an index of 0. Getting PageNum to follow suit with the -1
' Add controls for inputting parts
For FormRowCount = FormRowCount To NewControlCount
With Application.CreateControl("BOM5", acComboBox, acDetail, frm.Controls("Tabs").Pages(PageNum).Name, , ChangeTypeLeft, LastControlTop, ChangeTypeWidth, ControlHeight)
.Name = "ChangeType" & FormRowCount
.Tag = DYNAMIC_TAG
.RowSourceType = "Table/Query"
.RowSource = "ChangeType"
End With
最后一个 for 循环有 5 个其他 With Application.CreatControl 语句,但我只展示了第一个。其他 5 个类似,除了文本框而不是组合。
我以前遇到过这个错误,但我认为我通过将 DoCmd.OpenForm 语句移动到代码的不同部分来解决它(比如在 if 语句或 for 循环之外或不允许它得到的地方称为),但我认为这不会解决它。此外,第一个 for 循环可以正确迭代,因为我看到它抓住了最后一个动态控件的控件高度。
【问题讨论】:
-
在您的代码中,我没有看到删除现有控件的尝试。当您使用按钮运行 Sub 时,您确定应该创建的控件不存在吗?
-
糟糕,忘了包括那个位。删除是在一个单独的子中。最初它在同一个子中,但是当我添加按钮时将其拆分,这样我就不会删除我想添加到的控件。我想我倾向于忘记我拆分了操作。