BUG: Error Message When onreadystatechange Used with XML HTTP Objects in Visual Basic (303322)



The information in this article applies to:

  • Microsoft XML 2.0
  • Microsoft XML 2.5
  • Microsoft XML 2.6
  • Microsoft XML 3.0
  • Microsoft XML 3.0 SP1
  • Microsoft XML 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

MORE INFORMATION

Steps to Reproduce Behavior

  1. In Visual Basic, create a new Standard EXE project.
  2. Add a reference to the Microsoft XML 3.0 library.

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

    Command Button CaptionChange to...
    Command1Fail
    Command2Polling Using Timer
    Command3Using Class Wrapper
    Command4Using DOMDocument
  4. Add a timer to the form.
  5. 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 "GET", "http://localhost/test.xml", True
        XMLHttpRequest.send
        
        Exit Sub
    
    FailedState:
        MsgBox Err.Number & ": " & 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 "GET", "http://localhost/test.xml", 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 "GET", "http://localhost/test.xml", 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 "http://localhost/test.xml"
        
    End Sub
    
    Private Sub Timer1_Timer()
        Debug.Print XMLHttpRequest.readyState
        If XMLHttpRequest.readyState = 4 Then
            MsgBox "Done"
            Timer1.Interval = 0
        End If
    End Sub
    
    Private Sub XMLDom_onreadystatechange()
        Debug.Print XMLDom.readyState
        If XMLDom.readyState = 4 Then
            MsgBox "Done"
        End If
    End Sub
    					
  6. 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 "Done"
        End If
    End Sub
    					
  7. In MyReadyStateHandler, select OnReadyStateChange. On the menu, click Tools, and then click Procedure Attributes.
  8. The name that appears in the box should be OnReadyStateChange. Click Advanced.
  9. In the ProcedureID box, select Default.
  10. Click OK to close the Procedure Attributes dialog box.
  11. Create a new file in your localhost folder and name it Test.xml. Paste the following XML in the file:
    <?xml version="1.0"?>
    <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>
    					
  12. Run the form.
  13. Click Fail. The error message appears.
  14. To view one or more of the possible resolutions for this problem, do one of the following:
    • To view the Polling resolution, click Polling Using Timer. The states appear in the immediate window, and a message box that reads "Done" appears when the document is loaded.
    • To view the Class Wrapper resolution, click Using Class Wrapper. The states appear in the immediate window, and a message box that reads "Done" appears when the document is loaded.
    • To view the DOMDocument resolution, click Using DOMDocument. The states appear in the immediate window, and a message box that reads "Done" appears when the document is loaded.
    NOTE: For any of the steps above, you can place breakpoints at various places to step through the code.

REFERENCES

For the latest information and downloads for the Microsoft XML parser, see the following Microsoft Developer Network (MSDN) Web sites: For additional information, click the article number below to view the article in the Microsoft Knowledge Base:

278674 Determine the Version of MSXML Parser Installed on a Computer


Modification Type:MajorLast Reviewed:9/25/2001
Keywords:kbbug kbDSupport KB303322