DeleteControl, DeleteReportControl Statements

Description

You can use the DeleteControl and DeleteReportControl statements to delete a specified control from a form or report.

For example, suppose you have a procedure that must be run the first time each user logs into your database. You can set the OnClick property of a button on the form to this procedure. Once the user has logged on and run the procedure, you can use the DeleteControl statement to dynamically remove the command button from the form.

Syntax

DeleteControl formname, controlnameDeleteReportControl reportname, controlname

The DeleteControl and DeleteReportControl statements have the following arguments.

Argument Description
formname, reportname A string expression identifying the name of the form or report containing the control you want to delete.
controlname A string expression identifying the name of the control you want to delete.


Remarks

The DeleteControl and DeleteReportControl statements are available only in form Design view or report Design view, respectively.

Tip If you are building a wizard that deletes a control from a form or report, your wizard must open the form or report in Design view before it can delete the control.

See Also

CreateControl, CreateReportControl Functions.

Example

The following example creates a form with a command button and displays a message asking if the user wants to delete the command button. If the user chooses Yes, the command button is deleted.


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?"
    intDialog = vbYesNo + vbCritical + vbDefButton2
    intResponse = MsgBox(strMsg, intDialog)
    If intResponse = vbYes Then
        ' Delete control.
        DeleteControl frm.Name, ctlNew.Name
    End IfSub