Microsoft Knowledge Base Email Alertz

When you attempt to set the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 303322 - Last Review: September 25, 2001 - Revision: 1.2

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

This article was previously published under Q303322

On This Page

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.

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:

    Collapse this tableExpand this table
    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:
XML/XSL
http://msdn.microsoft.com/xml (http://msdn.microsoft.com/xml)
onreadystatechange Property (IXMLHTTPRequest)
http://msdn.microsoft.com/en-us/library/ms762767(VS.85).aspx (http://msdn.microsoft.com/en-us/library/ms762767(VS.85).aspx)
For additional information, click the article number below to view the article in the Microsoft Knowledge Base:
278674  (http://kbalertz.com/Feedback.aspx?kbNumber=278674/EN-US/ ) Determine the Version of MSXML Parser Installed on a Computer

APPLIES TO
  • Microsoft XML Parser 2.0
  • Microsoft XML Parser 2.5
  • Microsoft XML Parser 2.6
  • Microsoft XML Parser 3.0
  • Microsoft XML Parser 3.0 Service Pack 1
  • Microsoft XML Core Services 4.0
Keywords: 
kbbug kbnofix KB303322
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate