ACC: CreateControl Creates Empty Container for OLE Controls

Last reviewed: August 28, 1997
Article ID: Q147134
The information in this article applies to:
  • Microsoft Access versions 7.0, 97

SYMPTOMS

Advanced: Requires expert coding, interoperability, and multiuser skills.

When you use the CreateControl() function to programmatically create an OLE control, only an empty container is created.

CAUSE

The CreateControl() function does not add the OLE data needed to make the control functional. This occurs even if the procedure sets the Class or OLEClass properties of the control.

RESOLUTION

In order to create an OLE control at run time, you must first create a "template" form which contains all OLE controls that your procedure needs to create at run time. Then, the procedure must set the OLEData property of the new custom control to the OLEData property of the OLE control on the "template" form.

Create a form with all OLE controls you plan for your procedure to create at run time. The procedure must set the OLEData property of the new OLE control to the OLEData property of the appropriate control on the template form. The following steps demonstrate how to do this with the Calendar control included with Microsoft Access for Windows 95.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to your version of the "Building Applications with Microsoft Access" manual.

  1. Open the sample database Northwind.mdb.

  2. Create a new blank form named Template.

  3. Insert a Calendar control and name it Calendar.

  4. Create a module and type the following line in the Declarations section if it is not already there:

          Option Explicit
    

  5. Type the following procedure:

          Sub CreateCalendar()
             Dim frm As Form
             Dim ctl As Control
             DoCmd.OpenForm "Template", acDesign , , , , acHidden
             Set frm = CreateForm()
             Set ctl = CreateControl(frm.Name, acCustomControl, acDetail)
             ctl.OLEData = Forms!Template!Calendar.OLEData
             DoCmd.Restore
             DoCmd.Close acForm, "Template"
          End Sub
    
    

  6. To test this function, type the following line in the Debug window, and then press ENTER:

          CreateCalendar
    

    Note that a new form is created with a Calendar control.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Open the sample database Northwind.mdb.

  2. Create a module and type the following line in the Declarations section if it is not already there:

          Option Explicit
    

  3. Type the following procedure:

          Sub CreateCalendar()
             Dim frm As Form
             Dim ctl As Control
             Set frm = CreateForm()
             Set ctl = CreateControl(frm.Name, acCustomControl, acDetail)
             ctl.Class = "MSACAL.MSACALCtrl.7"
             ctl.OLEClass = "Calendar Control"
          End Sub
    
    

  4. To test this function, type the following line in the Debug window, and then press ENTER:

           CreateCalendar
    

    Note that a new form is created with an empty OLE control container even though the procedure set the Class and OLEClass properties.

REFERENCES

For more information about the CreateControl() function, search the Help Index for "CreateControl Function," or ask the Microsoft Access 97 Office Assistant.

Keywords          : kbprg PgmHowTo
Version           : 7.0 97
Platform          : WINDOWS
Hardware          : x86
Issue type        : kbprb
Solution Type     : kbcode


================================================================================


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: August 28, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.