Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
81
Добавлен:
11.05.2015
Размер:
2.11 Mб
Скачать

Using Implements in the class declaration

The next step is to add the Crystal Data Source interface to your class module.

1With the code window for the MyDataSource class module open, add the following code to the General Declarations section of the class.

Implements CRDataSourceLib.CRDataSource

2Open the drop-down list of objects in the upper left of the code window. You will see a new object has been added to the list: CRDataSource.

3Select CRDataSource from the list of objects. A new Property Get procedure is added to the class module for the FieldCount property of the CRDataSource object. Remember that in COM interfaces, properties are actually implemented as Get and Let procedures. For more information, refer to your Visual Basic documentation.

4Open the drop-down list in the upper right of the class module code window. Notice that several procedures appear corresponding to the properties and methods of the Crystal Data Source interface. In fact, the properties and methods you saw in the Object Browser are the same properties and methods listed here in the code window.

Implementing the functions

Once you have added the Crystal Data Source interface to your class module, you must implement all of the properties and methods in the interface to successfully produce a data source that can be compiled into an ActiveX DLL and used with the Active Data Driver. The first step to implementing all of the properties and methods is to add procedures to your class for each of the Crystal Data Source procedures.

The following topics are discussed in this section.

Adding procedures, Page 135

Implementing procedures, Page 136

Compiling the ActiveX DLL, Page 137

Adding procedures

When you selected the CRDataSource object in the object list in the previous section, you automatically added a procedure to the class for the FieldCount property. This property procedure appears in bold in the list of CRDataSource methods and properties to indicate that it has already been added.

1With the CRDataSource object selected in the code window, select Bookmark [Property Get] from the dropdown list in the upper right corner of the code window. A Property Get procedure appears in the class for the Bookmark property of CRDataSource.

2Repeat the process for the Property Let procedure of the Bookmark property. Keep in mind that Property Get procedures allow values to be retrieved from properties while Property Let procedures allow values to be assigned to properties.

Visual Basic Solutions

135

3Continue selecting each of the property and method procedures listed so that a procedure appears in your class for every property and every method defined by the Crystal Data Source interface.

4Notice that each of the procedures has been defined as Private. For our ActiveX DLL to expose these properties and methods to other applications, we need to change these to Public. Replace each Private statement with Public.

5Save your project to preserve all changes up to this point.

Implementing procedures

Exactly how you implement each of the properties and methods in the CRDDataSource interface depends upon the purpose and design of your application or component. To give you an idea of how to implement the procedures, though, the following code sample simply uses an ADO Recordset object connected to the Xtreme sample data DataSource. Obviously, this example has little value in a real application; an ADO Recordset can itself be reported on through the Active Data Driver. However, the example does illustrate how the properties and methods in the Crystal Data Source interface work.

Implements CRDataSourceLib.CRDataSource

Dim adoRs As ADOR.Recordset

Private Sub Class_Initialize()

Set adoRs = New ADOR.Recordset

adoRs.Open "Customer", "Xtreme sample data", _ adOpenKeyset, adLockOptimistic, adCmdTable

End Sub

Private Sub Class_Terminate()

adoRs.Close

Set adoRs = Nothing

End Sub

Public Property Let CRDataSource_Bookmark(ByVal RHS As Variant)

adoRs.Bookmark = RHS

End Property

Public Property Get CRDataSource_Bookmark() As Variant

CRDataSource_Bookmark = adoRs.Bookmark

End Property

Public Property Get CRDataSource_EOF() As Boolean

CRDataSource_EOF = adoRs.EOF

End Property

Public Property Get CRDataSource_FieldCount() As Integer

CRDataSource_FieldCount = adoRs.Fields.Count

End Property

Public Property Get CRDataSource_FieldName _ (ByVal FieldIndex As Integer) As String

CRDataSource_FieldName = adoRs.Fields(FieldIndex).Name End Property

Visual Basic Solutions

136

Public Property Get CRDataSource_FieldType _ (ByVal FieldIndex As Integer) As Integer

CRDataSource_FieldType = adoRs.Fields(FieldIndex).Type End Property

Public Property Get CRDataSource_FieldValue _ (ByVal FieldIndex As Integer) As Variant

CRDataSource_FieldValue = adoRs.Fields(FieldIndex).Value End Property

Public Sub CRDataSource_MoveFirst()

adoRs.MoveFirst

End Sub

Public Sub CRDataSource_MoveNext()

adoRs.MoveNext

End Sub

Private Property Get CRDataSource_RecordCount() As Long

CRDataSource_RecordCount = adoRs.RecordCount

End Property

Compiling the ActiveX DLL

Once you have finished implementing all of the properties and methods, you can compile the ActiveX DLL. When compiling ActiveX components, Visual Basic registers the component in the Windows Registry database. The name of the project, MyDataSourcePrj in this case, is used as the name of the component. The name of the class module, MyDataSource for this example, becomes the name of a creatable object. Once compiled, the component can be referenced by another application.

1Make sure you save the entire project so that all source code is preserved.

2From the File menu, select Make MyDataSource.dll. Note that the name of the DLL that will be created is based on the name of your Visual Basic project file (.VBP), not on the project name as specified by the (Name) property.

3When the Make Project dialog box appears, select the location where the new DLL should reside.

4Click OK, and the new DLL is created and registered.

Passing the CRDataSource object to the Active Data Driver

Using an object that implements the Crystal Data Source interface is a straightforward process, much like using any ActiveX component in an application. A Reference to the component must first be made, then an instance of the component object must be created in the application, and finally, the properties and methods of the object can be used. In this example, we will use the ActiveX DLL we created to obtain a MyDataSource object that we can pass to the Active Data Driver in a report generated using the Crystal Designer Component.

For this example, we will assume you have created an application in Visual Basic and designed a report using the Crystal Report Designer Component. For more information on the Crystal Report Designer Component, see The Seagate Crystal Report Designer Component - Introduction, Page 146.

Visual Basic Solutions

137

If you want to create a new report that can use the MyDataSource ActiveX DLL, create the report using three fields corresponding to the Customer ID, Customer Name, and City fields in the Customer table of the Xtreme sample data ODBC data source. To make things simple, you can use ADO to connect directly to those three fields. The purpose of this tutorial is simply to teach the techniques, not, necessarily, to produce a real application.

Adding a reference to MyDataSourcePrj

With your application open in Visual Basic:

1.Select References from the Project menu. The References dialog box will appear.

2.Scroll through the list of Available References to locate the MyDataSourcePrj component.

3.Add a check mark to the check box next to MyDataSourcePrj, and click OK. The component is now available to your application.

4.Open the Object Browser in Visual Basic, and select the MyDataSourcePrj library. Notice that the MyDataSource object is available and that this object contains all of the properties and methods that you implemented in the MyDataSource ActiveX DLL. Additionally, each of these properties and methods corresponds to a property or method in CRDataSource.

Creating an instance of MyDataSource

This section assumes you are already familiar with how to pass a new data source to the Active Data Driver at runtime. If you need more information on using the Active Data Driver, refer to Active Data Driver, Page 118. The following steps simply illustrate how to assign the myDs object created above to the Active Data Driver so that a report will use it as the source of data at runtime.

To actually use the MyDataSourcePrj component, you must create an instance of the MyDataSource object, then assign that object to the Report object displayed by your application. Assuming you created a report in your application using the Crystal Designer Component and accepted default settings for adding the Crystal Smart Viewer/ActiveX to your project:

1Open the code window for the form containing the CrystalSmart Viewer/ActiveX.

2In the General Declarations section for the form, add the following code:

3Dim myDs As New MyDataSourcePrj.MyDataSource

4In the Form_Load procedure, add the following line before the Report object is assigned to the ReportSource property of the CRViewer1 object:

Report.Database.SetDataSource myDs

NOTE: This example is based on a Visual Basic application created using the Crystal Designer Component, not the Crystal Report Engine Automation Server, Page 111. If you are using the Report Engine Automation Server to assign the new data source to the Active Data Driver, refer to the instructions under Pass the Recordset to the Active Data Driver, Page 122 in the section on the Active Data Driver, Page 118.

Visual Basic Solutions

138

Соседние файлы в папке crystal