DeleteControl 方法可從表單刪除指定的控制項。
語法
表達。DeleteControl (FormName、ControlName)
expression 代表 Application 物件的變數。
參數
| 名稱 | 必要/選用 | 資料類型 | 描述 |
|---|---|---|---|
| FormName | 必要 | 字串 | 包含您要刪除之控制項的表單名稱。 |
| 控制項名稱 | 必要 | 字串 | 您要刪除之控制項的名稱。 |
傳回值
無
註解
例如,假設您有一個程序,必須在每個使用者第一次登入資料庫時執行。 您可以將表單上按鈕的 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 支援與意見反應。