• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1129
  • Last Modified:

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

0
darbid73
Asked:
darbid73
  • 7
  • 5
  • 4
2 Solutions
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now