Ms-access – Function which evaluates conditionally

ms access

I am using MS Access 2010 and trying to create a parameterized query on one table.

My table has many latitude/longtitude coordinates. I wish to pass one lat long point (two parameters) into a query and return all table records which are within a given distance.

The distance calculation is quite involved when working in spherical space.

I want to avoid executing the full spherical calc if it is obvious that the point being considered is not a suitable candidate (the vast majority of the table's candidates.)

Given that the MS Access function IIF() will evaluate everything despite suitability, as will switch(), etc. I would like guidance which would help me avoid executing the longer calculation. I can write VBA, but I do not know if calling a VBA function every time a record is considered would be very efficient.

Thanks!

Best Answer

Rather than calculating the distance between two points on the fly, you can narrow your selection by approximation.

Since we know that one degree of latitude is a nearly-constant 111km, a search for locations within 100km should rule out points greater than one degree of latitude distant.

Longitude is more complex because of the latitudinal delta (~111km per degree at the equator to 0 at the poles), but there are ways to approximate it. One method would be to simply calculate, in advance, the distance between degrees of longitude for each whole degree of latitude and store the result in a table. That table could be joined on the search target table using a rounded value for the input latitude.

Movable Type has a well-known, informative page dedicated to great circle navigation and related calculations.

Note: I realize this doesn't answer the actual question, but it does address the underlying need for a performant method of lat-long distance calculation, which I hope is helpful.