Microsoft Office 2000 Developer   

Creating a DataEnvironment Object for Use in a DLL File

See Also

When you use the Data Environment in VBA to create a data source, the DataEnvironment object that you create is a publicly creatable object. A publicly creatable object is an object that can be created (or referenced) by another application. The application can then use the object's public functions and properties. (In contrast to a private function or property, which is only usable by the object and is inaccessible to other applications.)

Similarly, you can directly reference a DataEnvironment object and access its public functions and procedures. But first, you must create the DataEnvironment object, then wrap the object in a DLL file. This topic demonstrates how to create a public function for the DataEnvironment object and how to create the DLL file.

For more information about creating the DataEnvironment object, see Creating a Data Source Using the Data Environment Designer. For information about using the DLL and creating an instance of the DataEnvironment object, see Using the DLL in an Office Application.

Creating a DLL File

In Office 2000 Developer, in order to use the Data Environment as a data source in your Office application, it must be compiled as a DLL file, which is done using the Make command of the Visual Basic Editor. Once this is done, the DLL is portable — after being registered on a machine, it can be used from any application. (The DLL is registered automatically on the development machine when it is compiled. When you use the Package and Deployment Wizard to distribute a project, the DLL will also be registered on any machine that uses the Setup.exe program.)

To compile the DataEnvironment object as a DLL

  1. Open the Visual Basic Editor (ALT+F11).

  2. On the File menu, select New Project. Then double-click Empty Project to insert a new project.

  3. On the Insert menu, select Data Environment.

  4. On the File menu, click Save. In the Save Project As dialog box, type a file name for the project. Also right-click Project1 and select Project Properties. Change the Project1 name to be the same as the file name. The name you type will become the name of the DLL and VBA file. (In this example, we saved as deNwind.)

  5. Configure the Data Environment designer. See Creating a Data Source Using the Data Environment Designer for an example of configuring the Data Environment designer.

  6. Rename the project that contains the Data Environment designer to one that is meaningful to you. (The name will become important later.)

  7. In the VBA Project Explorer, right-click the Data Environment icon. The DataEnvironment object's code window will be displayed.

  8. Create as many public functions or properties as required. For more information about public functions, see Introduction to Procedures in the Visual Basic Programmer's Guide.

    The following example returns a specific recordset named rsCustomers from the DataEnvironment object.

    Public Function ReturnRS(rsName As String) As Recordset
       Select Case rsName
       Case "Customers"
          If deNwind.DataEnvironment1.rsCustomers.State = adStateClosed Then
             deNwind.DataEnvironment1.rsCustomers.Open
          End If
          Set ReturnRS = deNwind.DataEnvironment1.rsCustomers
       Case Else
          ' Handle unknown cases here
       End Select
    End Function
    
  9. Click Save to save the project.

  10. On the File menu, click Make projectName.DLL

More information about creating DLL files can also be found in the MSDN Library. See Creating an ActiveX DLL in the Component Tools Guide of the Visual Basic documentation.