Being able to determine the distance between two places is of an enormous benefit in many software applications. There are simply hundreds of uses and we at Mill House Data Solutions have used it to great effect many, many times. There are online companies who can offer, for a subscription, automated access to their distance check software but this will not only cost, but will rely on internet access as well.
This method will only calculate the distance in Kilometres (divide the result by 1.609344 for miles) "as the crow files" but it is extremely useful when, for instance, you are marketing everyone within a fixed radius.
So how do we do it? The method uses the "Haversine" formula which assumes that the Earth is a sphere. OK we know that the Earth is not a perfect sphere, but when you are checking between two map co-ordinates it is more than accurate enough for these purposes.
OK so without going into the technicalities of how the formula works, we shall get straight on with the coding requirements for VBA.
You will need a table in your application containing all the UK postcode variations and the X & Y axis co-ordinates. We would be very happy to provide a copy.
The following VBA Code attached to the "Calculate Distance" click event is used in a simple form where the user enters the start and end postcodes and the distance is calculated.
txtPostCodeStart and txtPostCodeEnd are blank text boxes for entering your criteria.
txtStartLat, txtEndLat, txtStartLong and txtEndLong are text boxes that are automatically populated through the"after update" event on the text boxes "txtPostCodeStart" and "txtPostCodeEnd" using simple RecordSet vba programming.
Whilst we have demonstrated a simple form, the basic formula is there to be used in a host of different ways. We hope you will find this as useful as we have!
The author of this article has chosen to make this article available with free reprint rights. Click here to copy this article.
Disclaimer: All information on this site is provided for informational purposes only! By no means is any
information presented herein intended to substitute for the advice provided to you by any health care or other professional
or organization.