DTS Packages in Visual Basic

DTS Programming

DTS Programming

DTS Packages in Visual Basic

To create a Package object in Microsoft® Visual Basic®, you declare an object variable of the appropriate type and then create the object with the Visual Basic New operator.

The Package2 class of Microsoft SQL Server™ 2000 extends the Package class of SQL Server 7.0. For more information, see Extended DTS Objects.

However, Package2 objects cannot be created, and a Package2 object variable cannot be declared WithEvents. To create a Package object that is compatible with SQL Server 7.0, or one that does not use the new package features, use the following code example:

'Declare the object variable.
Private WithEvents objPackage As DTS.Package
. . .
'Create the package object.
Set objPackage = New DTS.Package

The WithEvents keyword must be omitted if package events are not to be handled. For more information about handling package events, see DTS Package Events in Visual Basic.

Creating a Package2 Object

To create a Package2 object that makes available the new DTS features, use the following code example:

'Declare the object variables.
Private objPackage As DTS.Package2
Private WithEvents objPkgEvents As DTS.Package
. . .
'Create the package object.
Set objPackage = New DTS.Package
Set objPkgEvents = objPackage
The declaration of and assignment to objPkgEvents must be omitted if package events are not to be handled.

When using late binding in Visual Basic, object variables are declared As Object. In the following example, the new package object is created the same way it was created in SQL Server 7.0:

'Declare the object variable.
Private objPackage As Object
. . .
'Create the package object.
Set objPackage = New DTS.Package