Using the MTSEnvironment Object

You can use MTS to help prevent memory leaks. MTS keeps track of all the memory you reserve and then, when necessary, frees it for you if you haven't done so explicitly. This works only if you have first used MTS to create the objects you want to control. That is, you create the MTSEnvironment object, and then you create another object in the context of the MTSEnvironment object.

First, the MTS ObjectContext itself must be instantiated:

'--- Create MTS Object context
Private MTS As New MTSEnvironment

See Declaring MTS Objects in the CML to find out why you should use New here.

To create external COM components, call CreateInstance on the ObjectContext of MTS, passing the ProgID of the component you want to create. For example, the following code is from the Admin component.

Set cn = MTS.CreateInstance("ADODB.Connection")
…
Set rs = MTS.CreateInstance("ADODB.Recordset")
…

The various methods of the Admin component perform database access and create ADODB objects to do so. But instead of using COM to create them, it uses MTS: