How to use WITHEVENTS with MSXML2.XMLHTTP40

Below is my rather simple code to get the returned XML from a HTTP Get.

I see in the Object browser that the XMLHTTP40 object has "onreadystatechange" which is not an event but says it is an object and "Register a complete event handler"

So how can I do a "Public WITHEVENT .........." so that I can then add this event?

or

Or can i set up an event to know when the response is there?
Dim HttpReq As New MSXML2.XMLHTTP40
  
HttpReq.Open "GET", "http://www.google.co.uk/ig/api?weather=london,england&hl=en", False
HttpReq.send
Debug.Print HttpReq.responseText

Open in new window

LVL 20
darbid73Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
You'd declare your XMLHTTP40 variable at the Form level (assuming you're doing this on a form). In the General Declarations section of your form module:

Private WithEvents HttpReq As MSXML2.XMLHTTP40

Now you'd have available all the event from that library; select the value "HttpReq" from your left side combo dropdown (the ones at the top of the code page), and then select the relevant Events from the right side combo. These events will fire whenever the XMLHTTP40 library tells them to, and your UI can react to them.
0
darbid73Author Commented:
Hi LSMConsluting,

THat is what I thought, but have you tried it.    I do not get any errors but it is not added to the object drop down list and thus there are no events in the left one.

I am wondering if I need to send the responsetext directly to a DOM object and then get the events of this dom object.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If you can't sink the library, then the library may not be compatible with VBA. I haven't tried it, but that's the method you use to sink events into a VBA code module.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I should say I haven't tried it with your specific library - I use WithEvents all the time, but not with your specified library.
0
darbid73Author Commented:
OK so then my question should be a little bit more specific I think.  Something like this

Can anybody sink the events of MSXML2.XMLHTTP40 using MIcrosoft XML v,4.0 .......or if that does not work any of the other Miscrosoft XLM versions.


0
darbid73Author Commented:
As I said in my opening question the "onreadystatechange" does not appear to be a normal event that you can sink.
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Have you seen this article:

http://msdn.microsoft.com/en-us/library/ms757030(VS.85).aspx

It supplies very specific syntax which must be used when working with that event:

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

I realize your initial library is different, but I'm wondering if it will work if you change XMLHTTP60 to XMLHTTP40.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
darbid73Author Commented:
thanks this pretty much explains the problem I am having.  I like how other peoples google searches give different results :-)

"The onreadystatechange callback function was not implemented as a COM automation event in the IXMLHTTPRequest and IServerXMLHTTPRequest components. This is because these components are heavily used in scripting environments, many of which do not support COM events. The onreadystatechange callback function was intended to be easy to use when working with scripting clients such as VBScript and JScript.      
Because the onreadystatechange property was not implemented through COM-based automation events, Visual Basic (and C/C++) applications need to implement this callback functionality differently.      "


Lets see if I can solve it.  Of course I will be back if I cannot.
0
JezWaltersCommented:
Since you're passing an Async value of False in your Open call, the ResponseText should be ready as soon as the Send call completes.
Or am missing something obvious?
0
JezWaltersCommented:
When I wanted to read some XML returned from the Google geocoder API, I used the Load method of the MSXML2.DOMDocument60 object
Dim docResponse As MSXML2.DOMDocument60  ' Add Microsoft XML, v6.0 library Reference

Set docResponse = New MSXML2.DOMDocument60

docResponse.async = False
If docResponse.Load("http://www.google.co.uk/ig/api?weather=london,england&hl=en") Then  ' Read successful
    ' Parse docResponse
End If

Open in new window

0
darbid73Author Commented:
Thanks for that.

I will have a look at the load method.

I do not suppose you have a URL for a geocoder API that will work even when the client computer is behind a proxy VPN

see my question here http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_25604776.html#a29092954
0
JezWaltersCommented:
I was doing postcode lookups and I used:
http://maps.google.com/maps/api/geocode/xml?address=BA1 1LZ+UK&sensor=false
but I don't know if you'll have proxy issues with it - it's probably quickest to just try!  :-)
0
JezWaltersCommented:
You don't need to register with Google for this service, and you don't need to specify a key - although you might get an "OVER_QUERY_LIMIT" status response from time to time!
0
JezWaltersCommented:
By the way, the old key-based Google geocoder (which returns a completely different XML document structure) has now been deprecated.
0
darbid73Author Commented:
yeh I know it does not need a key but it only does a geocode of a given address, what I want is to know where the user is.  The usual lookups are server sided so they will see an IP address from a proxy for example which would be wrong.  But that is a little off topic here.
0
darbid73Author Commented:
Thanks guys.  Jez I appreciate your help too.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.