Microsoft KB Archive/303322

= BUG: Error Message When onreadystatechange Used with XML HTTP Objects in Visual Basic =

Article ID: 303322

Article Last Modified on 9/25/2001

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft XML Parser 2.5
 * Microsoft XML Parser 2.6
 * Microsoft XML Core Services 4.0
 * Microsoft XML Parser 3.0 Service Pack 1
 * Microsoft XML Core Services 4.0

-



This article was previously published under Q303322



SYMPTOMS
When you attempt to set the onreadystatechange property of the XMLHTTP object within Microsoft Visual Basic to a function or variable, you may receive the following error message:

Run-time error '424':

Object Required



CAUSE
The onreadystatechange property is designed for use in scripting environments and is not readily accessible in Microsoft Visual Basic or Microsoft Visual C++. Note that this problem and its accompanying workarounds also apply to the ServerXMLHTTP object.



RESOLUTION
To work around this problem, do one of the following:
 * Poll the readystate property by using a timer. When the data is ready, turn the timer off.
 * Use DOMDocument to load the XML data and handle the state by using the withevents keyword. Note that if you are using XMLHTTP to post the data first, this resolution does not work.
 * Create a wrapper class and create a procedure to handle the event, then set the procedure to be the default and bind the class to the onreadystatechange XMLHTTP event.



STATUS


Steps to Reproduce Behavior
 In Visual Basic, create a new Standard EXE project. Add a reference to the Microsoft XML 3.0 library.

NOTE: If you do not have this version installed, see the &quot;References&quot; section. Add 4 command buttons to Form1. Change the captions of the buttons to the following:

 Add a timer to the form.  Paste the following code in Form1: Option Explicit

Public XMLHttpRequest As MSXML2.XMLHTTP Public WithEvents XMLDom As MSXML2.DOMDocument30

Private Function FunctionReadyStateChange Debug.Print XMLHttpRequest.readyState End Function

Private Sub Command1_Click FailedOnReadyState End Sub

Private Sub Command2_Click TimerResolution End Sub

Private Sub Command3_Click ClassResolution End Sub

Private Sub Command4_Click DOMResolution End Sub

Private Sub FailedOnReadyState On Error GoTo FailedState If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing Set XMLHttpRequest = New MSXML2.XMLHTTP ' Assign the wrapper class object to onreadystatechange. XMLHttpRequest.OnReadyStateChange = FunctionReadyStateChange

' Get some XML asynchronously. XMLHttpRequest.open &quot;GET&quot;, &quot;http://localhost/test.xml&quot;, True XMLHttpRequest.send Exit Sub

FailedState: MsgBox Err.Number & &quot;: &quot; & Err.Description

End sub

Private Sub TimerResolution If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing Timer1.Interval = 1 Set XMLHttpRequest = New MSXML2.XMLHTTP ' Get some XML asynchronously. XMLHttpRequest.open &quot;GET&quot;, &quot;http://localhost/test.xml&quot;, True XMLHttpRequest.send End Sub

Private Sub ClassResolution If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing Dim MyOnReadyStateWrapper As MyReadyStateHandler Set XMLHttpRequest = New MSXML2.XMLHTTP

' Create an instance of the wrapper class. Set MyOnReadyStateWrapper = New MyReadyStateHandler

' Assign the wrapper class object to onreadystatechange. XMLHttpRequest.OnReadyStateChange = MyOnReadyStateWrapper

' Get some XML asynchronously. XMLHttpRequest.open &quot;GET&quot;, &quot;http://localhost/test.xml&quot;, True XMLHttpRequest.send

End Sub

Private Sub DOMResolution If Not XMLHttpRequest Is Nothing Then Set XMLHttpRequest = Nothing If Not XMLDom Is Nothing Then Set XMLDom = Nothing Set XMLDom = New MSXML2.DOMDocument30 XMLDom.async = True XMLDom.Load &quot;http://localhost/test.xml&quot; End Sub

Private Sub Timer1_Timer Debug.Print XMLHttpRequest.readyState If XMLHttpRequest.readyState = 4 Then MsgBox &quot;Done&quot; Timer1.Interval = 0 End If End Sub

Private Sub XMLDom_onreadystatechange Debug.Print XMLDom.readyState If XMLDom.readyState = 4 Then MsgBox &quot;Done&quot; End If End Sub </li>  Add a class to the project and name it MyReadyStateHandler. Paste the following code into the class: Option Explicit

Sub OnReadyStateChange Debug.Print Form1.XMLHttpRequest.readyState If Form1.XMLHttpRequest.readyState = 4 Then MsgBox &quot;Done&quot; End If End Sub </li> In MyReadyStateHandler, select OnReadyStateChange. On the menu, click Tools, and then click Procedure Attributes.</li> The name that appears in the box should be OnReadyStateChange. Click Advanced.</li> In the ProcedureID box, select Default.</li> Click OK to close the Procedure Attributes dialog box.</li>  Create a new file in your localhost folder and name it Test.xml. Paste the following XML in the file: <?xml version=&quot;1.0&quot;?> <Root> <Testing>This is to test the onreadystatechange event on the XMLHTTPRequest or DOMDocument</Testing> <Testing>This is due to the event not being declared in the type library</Testing> </Root> </li> Run the form.</li> Click Fail. The error message appears.</li> To view one or more of the possible resolutions for this problem, do one of the following: <ul> To view the Polling resolution, click Polling Using Timer. The states appear in the immediate window, and a message box that reads &quot;Done&quot; appears when the document is loaded.</li> To view the Class Wrapper resolution, click Using Class Wrapper. The states appear in the immediate window, and a message box that reads &quot;Done&quot; appears when the document is loaded.</li> To view the DOMDocument resolution, click Using DOMDocument. The states appear in the immediate window, and a message box that reads &quot;Done&quot; appears when the document is loaded.</li></ul>

NOTE: For any of the steps above, you can place breakpoints at various places to step through the code.</li></ol>

<div class="references_section">