Silversonic
Nov 9 2009, 05:09 AM
I need to make a distance calculator between two points using their postcode - one postcode that varies and one that stays the same.
This is for my project, someone gave me the visual basic code on how to do this so I could work over it on the weekend but I still don't understand how it works. Can someone help me explain each part so that maybe I could incorporate it in to my own database? I know it works because someone else has gotten it to.
Here it is:
Private Sub distance_Click()
On Error GoTo ErrHandler
Dim winCon As Object
Dim conReturn As String
Set winCon = CreateObject("WinHttp.WinHttpRequest.5.1")
webAddress = "http://www.multimap.com/directions/?mode=driving&countryCode_1=GB&countryCode_2=GB&qs_1=me24pb&qs_2=" & qs_2
winCon.Open "GET", webAddress, False
winCon.send
conReturn = winCon.responseText
responseText = Mid(conReturn, InStr(conReturn, """miles""") + 8, 30)
Distance_Travelled = Left(responseText, InStr(responseText, "}") - 1)
NormalExit:
Exit Sub
ErrHandler:
MsgBox ("The place names/postcodes either do not exist or" & vbCrLf & "exist in more than one place." & vbCrLf & vbCrLf & "Please use different addresses")
GoTo NormalExit
DoCmd.Save
End Sub
Alan_G
Nov 9 2009, 06:41 AM
Hi
Assuming that Distance_Travelled is the name of an unbound textbox on your form, and the qs_2 variable (the very last one on the webAddress line) is the name of another textbox that will supply the destination postcode, you should just need to add
Dim webAddress As String, responseText As String
to your variable declarations. I'd also put
Set winCon = Nothing
directly underneath NormalExit: as well. I'd imagine that the qs_2 variable would also need to be enclosed in quotes as it's a text value.
Compile your code and it should work OK
Doug Steele
Nov 9 2009, 07:22 AM
Alan's given you advice on how to improve that code, so I'll just explain what it does.
Set winCon = CreateObject("WinHttp.WinHttpRequest.5.1") instantiates the variable winCon as an instance of a WinHttpRequest object, which is part of the Microsoft Windows HTTP Services (WinHTTP) protocol which provides developers with an HTTP client API to send requests through the HTTP protocol to other HTTP servers. (Think of it as being a browser in memory)
webAddress = "http://www.multimap.com/directions/?mode=driving&countryCode_1=GB&countryCode_2=GB&qs_1=me24pb&qs_2=" & qs_2 defines the actual web reference in which you're interested.
winCon.Open "GET", webAddress, False opens a connection to that web reference, using the GET protocol (there are two different methods available: GET and PUT).
winCon.send actually sends the request to that reference.
conReturn = winCon.responseText gets the response from the web service and stores it in variable conReturn (think of this as being conReturn contains the HTML or. more likely, XML that the browser would see if you went to that URL)
responseText = Mid(conReturn, InStr(conReturn, """miles""") + 8, 30) looks for the phrase "miles" (including the quotes) in that response. Assuming it finds it, it takes 30 characters after that phrase, and stores it in variable responseText.
Distance_Travelled = Left(responseText, InStr(responseText, "}") - 1) looks for a closing brace (}) in what's been stored in responseText, and puts everything in front of that closing brace into variable Distance_Travelled.
Silversonic
Nov 9 2009, 10:12 AM
Thanks a ton guys, got it to work =)
Alan_G
Nov 9 2009, 10:16 AM
Great - glad Doug and I could help
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please
click here.