DeleteControl 方法从窗体中删除一个指定的控件。
语法
表达式。DeleteControl (FormName、 ControlName)
expression:表示 Application 对象的变量。
参数
| 名称 | 必需/可选 | 数据类型 | 说明 | 
|---|---|---|---|
| FormName | 必需 | 字符串 | 包含要删除的控件的窗体的名称。 | 
| ControlName | 必需 | 字符串 | 要删除的控件的名称。 | 
返回值
Nothing
注解
例如,假设有一个过程,每个用户首次登录数据库时必须运行该过程。 可以将窗体上按钮的 OnClick 属性设置为此过程。 用户登录并运行过程后,可以使用 DeleteControl 方法从窗体中动态删除命令按钮。
DeleteControl 方法仅在窗体设计视图或报表设计视图中分别可用。
注意
如果正建立一个从窗体或报表中删除控件的向导,该向导必须先在“设计”视图中打开这个窗体或报表,然后才能删除控件。
示例
下面的示例创建带有命令按钮的窗体,并且显示提示信息询问用户是否要删除这个命令按钮。 如果用户选择 “是”,则会删除命令按钮。
Sub DeleteCommandButton() 
 Dim frm As Form, ctlNew As Control 
 Dim strMsg As String, intResponse As Integer, _ 
 intDialog As Integer 
 
 ' Create new form and get pointer to it. 
 Set frm = CreateForm 
 ' Create new command button. 
 Set ctlNew = CreateControl(frm.Name, acCommandButton) 
 ' Restore form. 
 DoCmd.Restore 
 ' Set caption. 
 ctlNew.Caption = "New Command Button" 
 ' Size control. 
 ctlNew.SizeToFit 
 ' Prompt user to delete control. 
 strMsg = "About to delete " & ctlNew.Name &". Continue?" 
 ' Define buttons to be displayed in dialog box. 
 intDialog = vbYesNo + vbCritical + vbDefaultButton2 
 intResponse = MsgBox(strMsg, intDialog) 
 If intResponse = vbYes Then 
 ' Delete control. 
 DeleteControl frm.Name, ctlNew.Name 
 End If 
End Sub
支持和反馈
有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。