I'm trying to use the MSXML.XMLHTTP object in Access VBA to upload and down load data from a web site. I found this code on Microsoft's MSDN site but it will not work.
The idea is to POST data to an asp/aspx page and receive data back. It WILL work SYNCHRONOUSLY but NOT ASYNCHRONOUSLY which is what I need. (Actually it works asynchronously if you poll the ReadyState property but I want to use the event/callback function. )
To use it asynchronously you define a function (onReadyStateChange) to fire off when the state changes. The problem is the onReadyStateChange function never gets called.
The example code (below) creates a class object 'MyReadyStateHandler' with only one method (SUB) 'OnReadyStateChange' (lines 8-11). An instance of this class is then assigned to the property 'OnReadyStateChange' of my XMLHTTP object 'XMLHttpRequest'. (line 5)
The 'true' argument on line 6 sets the method to 'asynchronous'.
I've tried this in VB
6, in Access 2002 and now in Access 2003 and it won't work. Does anybody have any idea why?
(I've been all over the Microsoft web site and can't find anything other than the sample code. I've found two references to the XMLHTTP object on this forum but they didn't address this issue.)
Here is the code:
' create the XMLHTTP object
1 Dim XMLHttpRequest As MSXML2.XMLHTTP40
2 Set XMLHttpRequest = New MSXML2.XMLHTTP40
' Create an instance of the wrapper class.
3 Dim MyOnReadyStateWrapper As MyReadyStateHandler
4 Set MyOnReadyStateWrapper = New MyReadyStateHandler
' Assign the wrapper class object to onreadystatechange.
5 XMLHttpRequest.OnReadyStateChange = MyOnReadyStateWrapper
' Get some stuff asynchronously.
6 XMLHttpRequest.open "POST", "http://localhost/test.xml", True
Add a Class Module and name it "MyReadyStateHandler"
Paste the following code into the class module:
8 Debug.Print Form1.XMLHttpRequest.readyState
' whenever readyState changes onReadyStateChange is called
' readyState = 1 while waiting for the async call to return
9 If Form1.XMLHttpRequest.readyState = 4 Then
10 MsgBox "Done"
11 End If