Microsoft KB Archive/255625
Article ID: 255625
Article Last Modified on 5/12/2003
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
This article was previously published under Q255625
After an object raises a trappable error, the client checks the Err object and finds that the error number is zero and the error description is empty.
This problem occurs only when the client application is compiled to native code and when the client does not have a local or global variable that stores a reference to the object that raises the error.
Assume that you have an object called Object1 and it has a property called ChildObject that returns a reference to another object, Object2. If you call a method on Object2 using a syntax such as:
you see this type of behavior because no variable holds a reference to Object2.
The workaround for this problem is to assign an object to a variable and avoid using the preceding syntax. Do the following:
Dim MyObject as Object2 Set MyObject = Object1.ChildObject ' call the method using the MyObject variable MyObject.Method
Another workaround is to compile your project to p-code.
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.
Steps to Reproduce Behavior
- Start a new ActiveX DLL project. Class1 is created by default.
- From the Project menu, select Project1 Properties, and then set the name to MyDll.
- From the Project menu, add a new class module to this project. This is called Class2 by default.
Paste the following code into the Class1 code module:
Option Explicit Public Sub DoSomething Err.Raise 101 End Sub Private Sub Class_Terminate Dim i as Long ' if you comment the following line, the error object is not reset On Error Resume Next i=0 End Sub
Paste the following code into the Class2 code module:
Option Explicit Public Property Get ChildObject() As Class1 Set ChildObject = New Class1 End Property
- Save the project and compile it as MyDll.Dll.
- Start a new Standard EXE project. Form 1 is created by default.
- From the Project menu, select References, select MyDll, and then click OK to add a reference to the DLL you just created.
- Add a CommandButton to Form1. This is Command1 by default.
Paste the following code into Form1's code module:
Option Explicit Private Sub Command1_Click() Dim MyObj As New MyDll.Class2 On Error GoTo p1 MyObj.ChildObject.DoSomething Exit Sub p1: MsgBox "Err.Number=" & Err.Number End Sub
- From the Project menu, select Project1 Properties, and then select the Compile tab. Select Compile to P-Code.
- From the File menu, select Make to compile the Standard EXE project to p-code. Run the executable and click on Command1. Note that you get a message box with "Err.Number=101" as expected.
- Dismiss the message box and stop the project to return to the design environment.
- From the Project menu, select Project1 Properties, and select the Compile tab. Select Compile to Native Code.
- From the File menu, select Make to compile the project to native code. Run the executable and click on Command1. Note that you get a message box with "Err.Number=0" which is not as expected. The problem is caused by the Class_Terminate event. When you use the syntax Object1.ChildObject.Method, Object2 gets released before the next line of code is executed, which triggers the Class_Terminate event, and the Err object is reset in the terminate event procedure. If you comment out the line that uses the Err object in this event, the problem does not happen.
Keywords: kbbug kbnofix KB255625