Freigeben über


Visual Basic Concepts

Creating Data Sources

A data source is an object that binds other objects to data from an external source. The foundation for a data source object is a data-aware class module, which is essentially a class module that exposes interfaces to an external source of data. Data-aware classes are covered in depth in "Programming with Objects" in the Programmer's Guide.

Data-aware classes can also be used as the basis for ActiveX components. One common example of a data-aware component is the ADO Data control, which provides a visual interface for binding controls to a database through ADO. Although you could create a data-aware class that does the same thing as the ADO Data control, sharing that class between multiple applications or multiple programmers could prove difficult.

A much better approach would be to create an ActiveX component that duplicates or expands on the functionality of the ADO Data control. As with any ActiveX component, this could take any one of several forms: an ActiveX control, an ActiveX DLL, or an ActiveX EXE. In any case, your ActiveX data source can be easily shared, simplifying access to data regardless of where that data resides: in a local database like Access, in a remote database such as SQL Server, or even in a private OLE DB data store.

The series of step-by-step procedures in this chapter will demonstrate two different approaches to creating data source components. First we'll build an ActiveX data source control, MyDataControl, which emulates the ADO Data control. Next we'll bind the MyDataControl to other controls using both simple and complex binding. Finally, we'll create an ActiveX DLL, MyData, that demonstrates binding to a private data store in an OLE DB simple provider.

The procedures for creating the data source components build on each other. The sequence in which you perform the procedures is therefore important. You should already be familiar with the basics of building ActiveX components before proceeding with this example.

Topics

Creating the MyDataControl Project

Sets up a new project for an ActiveX data source control that duplicates the ADO Data control.

Drawing the MyData Control

Creates the user interface for MyData data source control.

Adding the AXDataSource Project

Creates a test project and a project group for the MyData data source control.

Adding Data Handling Code

Illustrates the data handling procedures required for the MyData data source control.

Running the MyDataControl Project

Demonstrates the functionality of the MyData data source control using both ODBC and OLE DB interfaces.

Creating the MyData Component Project

Sets up a project for an ActiveX DLL data source component.

Creating the MyOSPObject Class

Creates a class that implements the OLE DB Simple Provider interfaces.

Creating the MyDataSource Class

Creates a class that uses the MyOSPObject class to provide data.

Testing the MyData Component

Demonstrates the use of the MyDataComponent ActiveX DLL.

Data Sources Recap

Summarizes the concepts involved in creating ActiveX data source components.

These procedures will be easier to follow if you set up your Visual Basic development environment to show the necessary windows.

Before You Begin

  1. On the View menu, click Toolbox to open the Toolbox.

  2. On the View menu, click Project Explorer to open the Project Explorer window. The Project Explorer window will be used extensively to switch between project files.

  3. If the Project Explorer window is in Folder view, click the Toggle Folders button on the Project Explorer window toolbar to turn the folders off.

  4. On the View menu, click PropertiesWindow to open the Properties window.

  5. On the View menu, click ImmediateWindow to open the Immediate window. You will need this window open at design time, in order to demonstrate the control's code running at design time.

  6. On the Tools menu, click Options to open the Options dialog box.

    Select the Editor tab, and make sure the Require Variable Declaration box is selected. This makes it much easier to catch typing errors.

    Select the Environment tab. Make sure Prompt To Save Changes is checked, then click OK. This will make it easy to save the changes to the project as you go along.

For More Information*   See "Creating Data-Aware Classes" in "Programming with Objects" in the *Programmer's Guide.

Sample application

Axdata.vbg

Provides a prebuilt example of the data source project discussed in this chapter. In addition, the bitmaps and text file needed for the sample are located in the same directory as the Axdata.vbg sample, which is listed in the directory.