HOW TO: Use the DeleteControl and DeleteReportControl Statements, and the CreateGroupLevel() Function in Access 2000 (210596)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q210596
Moderate: Requires basic macro, coding, and interoperability skills.

This article applies to a Microsoft Access database (.mdb) and to a Microsoft Access project (.adp).

IN THIS TASK

SUMMARY

This article shows you how to use the DeleteControl and DeleteReportControl statements and the CreateGroupLevel() function. If you intend to write your own form wizard or report wizard, you can use these statements to delete controls on a form or a report that is available in Design view, and you can use the function to create groups on a report that is available in Design view.

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.
CAUTION: If you follow the steps in this example, you modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and follow these steps on a copy of the database.

back to the top

DeleteControl and DeleteReportControl Statements

You can use DeleteControl and DeleteReportControl statements to delete a control that exists on a form or report in Design view. Both statements require a string value that represents the name of the form or report and a string value that represents the name of the control.

DeleteControl and DeleteReportControl are statements, not functions, and do not return a value. CreateGroupLevel() is a function and can return a value as shown below.

To create a form, create a button on the form, display a message, and then delete the button on the form, follow these steps:
  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. In the Database window, click Modules, and then click New to create a new module.
  3. Type the following new procedure:
    Public Sub TestForm()
    Dim MyForm As Form, MyControl As Control
    
        Set MyForm = CreateForm()                       ' create new form
        Set MyControl = CreateControl(MyForm.Name, 104) ' create command button on form
        MsgBox "About to Delete " & MyControl.Name
        DeleteControl MyForm.Name, MyControl.Name       ' delete button from form
    
    End Sub
    					
  4. Tile the Microsoft Access and Microsoft Visual Basic Editor windows, and then step through the procedure by clicking anywhere in the following line
    Public Sub TestForm()
    					
    and pressing function key F8. To see the creation and removal of the command button, you need to restore the newly created form before executing the following statement:
        MsgBox "About to Delete " & MyControl.Name
    					
back to the top

CreateGroupLevel() Function

When a report is open in Design view, you can create groups by using the CreateGroupLevel() function, which has the following syntax
CreateGroupLevel(ReportName$, Expression$, HasHeader, HasFooter)
				
where:
  • ReportName$ is a string expression identifying the report.
  • Expression$ is a string expression representing the name of the field or calculated field that you want to group by.
  • HasHeader and HasFooter are the literals TRUE or FALSE that indicate whether the group includes a group header or group footer, respectively.
To use CreateGroupLevel(), follow these steps:
  1. Start Microsoft Access and open the sample database Northwind.mdb.
  2. Create a new report in Design view, based on the Customers table.
  3. Add all fields from the Customers field list to the Detail section of the report.
  4. Save the report as rptCustomerTest.
  5. Preview the report and notice the record order.
  6. Create a new module and insert the following procedure:
    Sub NewGroup()
    
        Dim intGroupLevel
        DoCmd.OpenReport "rptCustomerTest", acViewDesign
        intGroupLevel = CreateGroupLevel("rptCustomerTest", "Country", True, False)
        MsgBox "Group created at level " & intGroupLevel
    
    End Sub
    					
  7. On the Run menu, click Run Sub/UserForm.
  8. Preview the report again, and notice the addition of the Country header and the sorting of the records by country.CreateGroupLevel() adds a group at the innermost level. For example, if one group already exists, CreateGroupLevel() creates a second group within the first group. CreateGroupLevel() returns a number that indicates which level it created, beginning with zero. In the example above, CreateGroupLevel() returns 0 because "Country" is the first group.
back to the top

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbhowto kbHOWTOmaster kbProgramming KB210596 kbAudDeveloper