Application.DeleteControl 方法 (Access)

DeleteControl 方法可從表單刪除指定的控制項。

語法

運算式DeleteControl (FormNameControlName)

expression 代表 Application 物件的變數。

參數

名稱 必要/選用 資料類型 描述
FormName 必要 字串 表單的名稱,其中包含您想要刪除的控制項。
ControlName 必要 字串 您要刪除的控制項名稱。

傳回值

註解

例如,假設您有一個必須在每個使用者第一次登入資料庫時執行的程式。 您可以將表單上按鈕的 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 支援與意見反應