Visual Basic Sample Application for Automating MTS Administration

The Visual Basic version 5.0 sample application demonstrates how to use the methods on the Catalog, CatalogObject, and CatalogCollections objects to automate basic administration functionality for a package named “Scriptable Admin Demo.”

Note You must configure your Visual Basic project to reference the MTS administrative type library (MTSAdmin type library). To reference the MTSAdmin type library, select the References option from the Visual Basic Project toolbar. Then browse the available reference files for the “MTS 2.0 Admin Type Library.” For late-binding variables (binding that occurs when you run the program), Visual Basic will locate the type library without further configuration if the MTXADMIN.DLL file is registered on your local machine.

To delete any existing packages named “Scriptable Admin Demo”
  1. Call the CreateObject method to instantiate a catalog object.
    Dim catalog As Object
    Set catalog = CreateObject("MTSAdmin.Catalog.1")
  2. Get a Packages collection object by calling the GetCollection method. The Packages collection returns without retrieving any data from the catalog so that the collection will be empty upon return from the GetCollection method.
    Dim packages As Object
    Set packages = catalog.GetCollection("Packages")
  3. Find the previous version of the "Scriptable Admin Demo" package by populating the Packages collection to read in all packages and search for "Scriptable Admin Demo." Enumerate through the collection, starting at the highest index so the Remove method can be called from within the loop. The Remove method releases the object, removes the object from the collection, and shifts the objects in the collection so that object(n+1) becomes object(n) for all n greater than or equal to the index being removed. The effect of Remove method on the collection object is immediate. The Item and Count methods called any time after the Remove method will reflect the change in the index. However, the removal of the package is not applied to the catalog until the SaveChanges method is called (see step 4).
    packages.Populate
    Dim pack As Object
    n = packages.Count
    For i = n - 1 To 0 Step -1
        If packages.Item(i).Value("Name") = "Scriptable Admin Demo" Then
            packages.Remove (i)
    End If
    Next
  4. Call the SaveChanges method to save changes to the data store.
    packages.SaveChanges
To create a new package named “Scriptable Admin Demo Package”
  1. Add a new package using the Add method, and note the package ID assigned. The Add method adds the object to the collection but does not apply the changes to the catalog until the SaveChanges method is called (see step 3). Note that the Add method will apply default values to all properties. The default ID will be a new unique identifier.
    Dim newPack As Object
    Dim newPackID As Variant
    Set newPack = packages.Add
    newPackID = newPack.Value("ID")
  2. Update the Name and SecurityEnabled properties.
    newPack.Value("Name") = "Scriptable Admin Demo"
    newPack.Value("SecurityEnabled") = "N"
  3. Call the SaveChanges method to save the new package to the catalog. The return value of this call is the number of objects changed, added, or deleted. If no changes were pending, the method returns 0.
    n = packages.SaveChanges
To update the “Scriptable Admin Demo” package properties and get the ComponentsInPackage collection.
  1. Call the PopulateByKey method to read the package back from the catalog. Pass an array containing the keys of the objects to read. In the sample code, we use an array containing a single element (the ID of the package just created).
    Dim keys(0) as Variant
    keys(0) = newPackId
    packages.PopulateByKey keys
  2. Get the package object from the collection
    Dim package As Object
    Set package = packages.Item(0)
  3. Update the SecurityEnabled property for the package.
    package.Value("SecurityEnabled") = "Y" 
  4. Call the GetCollection method to retrieve the ComponentsInPackage collection. Supply the key of the "Scriptable Admin Demo package as a parameter.
    Set components = packages.GetCollection("ComponentsInPackage",_       package.Key)
       
  5. Call the SaveChanges method to save the changes to the catalog.
    packages.SaveChanges
To install a componentasdefcomponent into the "Scriptable Admin Demo package":
  1. Call the GetUtilInterface method to get the component utility object. This object is used to install components.
    Dim util As Object
    Set util = components.GetUtilInterface
    On Error GoTo installFailed
  2. Call the InstallComponent method, passing in a string containing the name of the dynamic-link library (DLL) of the component to be installed. If the component does not have an external type library or a proxy-stub DLL, pass in empty strings as the second and third arguments. Note that you do not have to call the SaveChanges method after installing a new component. All components contained in a DLL will be installed by this method, and are immediately written to the catalog. Call the GetCLSIDs method to get the CLSIDs of the components installed.
    Form2.Show 1
    Dim thePath As String
    thePath = Form2.MTSPath + "\samples\packages\vbacct.dll"
    util.InstallComponent thePath, "", ""
    Dim installedCLSIDs() as Variant
    util.GetCLSIDs thePath, “”, installedCLSIDs
    On Error GoTo 0
  3. Call the PopulateByKey method to read back the components just installed. Note that the components installed into the package via the InstallComponent method are not visible in the collection until the Populate or PopulateByKey method is called to read the data from the catalog.
    components.PopulateByKey installedCLSIDs
To find and delete the Bank.CreateTable component from the "Scriptable Admin Demo package":
  1. Iterate through the components and change transaction attributes using the Item and Count methods.
    Dim component As Object
    n = components.Count
    For i = n - 1 To 0 Step -1
        Set component = components.Item(i)
        component.Value("Transaction") = "Required"
  2. Find and delete the Bank.CreateTable component by index. Note that you must iterate though the collection backwards in order to call the Remove method during the loop.
    If component.Value("ProgID") = "Bank.CreateTable" Then
            components.Remove (i)
        End If
    Next
  3. Retrieve a new count and iterate through the collection again. Note that the Bank.CreateTable component will not be deleted from the data store until the SaveChanges method is called. Display a message box that informs the user if the installation succeeded.
    n = components.Count
    For i = 0 To n - 1
        Set component = components.Item(i)
        Debug.Print component.Value("ProgID")
        Debug.Print component.Value("DLL")
    Next
    
    n = components.SaveChanges
    MsgBox "Scriptable Admin Demo package installed and configured."
    Exit Sub
    
    installFailed:
        MsgBox "Error code " + Str$(Err.Number) + " installing " + thePath + "  Make sure the MTS path you entered is correct and that vbacct.dll is not already installed."
    End Sub

See Also

MTS Administration Objects, MTS Collection Types, MTS Administration Object Methods, Automating MTS Administration with Visual Basic, Automating Advanced MTS Administration with Visual Basic