INFO: Do Not Use ObjectContext in Class_Initialize and Class_Terminate Events

ID: Q250309


The information in this article applies to:
  • Microsoft Transaction Server 2.0
  • Microsoft Visual Basic, version 6.0


SUMMARY

Every Microsoft Transaction Server (MTS) object has an associated context object. The context object provides context for the execution of an instance, including transaction, activity, and security properties. When an MTS object is created, MTS automatically creates a context object for it. When the MTS object is released, MTS automatically releases the context object.

You can access an MTS object's context by calling the GetObjectContext function. The GetObjectContext function returns a reference to the IObjectContext interface. The context object is not available during calls to the component's Class_Initialize and Class_Terminate event. If you call one of its methods then the following error message will appear:

Run-time error '91':
Object variable or With block variable not set


MORE INFORMATION

Simple Server

  1. Open a new ActiveX DLL project in Visual Basic.


  2. From the Project menu, select References and Set "Reference to Microsoft Transaction Server Type Library".


  3. In class properties set MTSTransactionMode to anything other than NotAnMTSObject and add the following code.


  4. 
    Private mobjContext As ObjectContext
    Private Sub Class_Initialize()
        Set mobjContext = GetObjectContext()
    End Sub
    
    Private Sub Class_Terminate()
        Set mobjContext = Nothing
    End Sub
    
    Public Sub Test()
        mobjContext.SetComplete
    End Sub 
  5. Compile the component and recompile with Version Compatibility to Binary Compatible.


  6. Register the component with MTS.


Simple Client
  1. Open a Standard EXE Visual Basic application to test the component.


  2. Add a button to Form1.


  3. Add the following code to Command1_Click().


  4. 
    Private Sub Command1_Click()
        Dim o As Object
        Set o = CreateObject("project1.class1")
        o.Test
    End Sub 
  5. Run the client application and click the button. The following error message appears:


  6. Run-time error '91':
    Object variable or With block variable not set
  7. You can resolve this issue by changing the Simple Server code to the following:
  8. 
    Implements ObjectControl
    Private mobjContext As ObjectContext
    
    Public Sub Test()
        mobjContext.SetComplete
    End Sub
    
    Private Sub ObjectControl_Activate()
        Set mobjContext = GetObjectContext()
    End Sub
    
    Private Function ObjectControl_CanBePooled() As Boolean
    
    End Function
    
    Private Sub ObjectControl_Deactivate()
        Set mobjContext = Nothing
    End Sub 


REFERENCES

For additional information, please see the following article(s) in the Microsoft Knowledge Base:

Q170156 INFO: VB 6.0 Readme Part 12: Transaction Server (MTS) Issues

Additional query words:

Keywords : kbMTS kbMTS200 kbVBp600 kbDSupport
Version : winnt:2.0; :6.0
Platform : winnt
Issue type : kbinfo


Last Reviewed: January 24, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.