I have a SQL statement (saved as “LocationSearch” in Access) that calculates distance between two points and returns the “Distance” as a generated field.
SELECT Int((3963*(Atn(-(Sin(LATITUDE/57.2958)*
Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
Cos([@lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
Cos([@lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
Cos([@lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)))*10)/10 AS Distance, *
FROM Locations
ORDER BY (3963*(Atn(-(Sin(LATITUDE/57.2958)*
Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
Cos([@lng]/57.2958-LONGITUDE/57.2958))/Sqr(-(Sin(LATITUDE/57.2958)*
Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
Cos([@lng]/57.2958-LONGITUDE/57.2958))*(Sin(LATITUDE/57.2958)*
Sin([@lat]/57.2958)+Cos(LATITUDE/57.2958)*Cos([@lat]/57.2958)*
Cos([@lng]/57.2958-LONGITUDE/57.2958))+1))+2*Atn(1)));
All the nasty math code you see is what calculates the distance (in miles) in the SQL statement using Latitude and Longitude coordinates.
However, the problem is that the Distance field that is generated by the SQL statement seems to be returned as a string. If I then add SQL code that asks for locations between a distance of 0 and 45 miles, it returns ANY Distance value that starts between “0” and “45”. This includes a location with a distance of “1017” miles. Apparently, the Distance field is a text field, not a number field. So I can’t use the “BETWEEN” statement. I also can’t evaluate using “<” and “>” because it has the same problem.
I saved the SQL query above as a saved query called “LocationSearch”. This way I can run secondary queries against it, like this:
SELECT * FROM LocationSearch WHERE Distance < @MaxDistance
Access will ask for the @lat, @long and @MaxDistance parameters, then the locations will be returned in a recordset, ordered by distance. However, the problem that occurs is when I enter a MaxDistance of 45. With a table containing locations on the West Coast of the US, and a @lat of 47 and a @long of -122 (near Seattle), Access returns the following:

Notice also that the “Distance” field is right-formatted so it appears to be a numeric field, yet for some reason the query returns a location in San Diego, which is 1,017 miles away. My guess is that it was evaluating the Distance field as a text field, and in an ASCII comparison, I believe that “1017” lies between “0” and “45”.
One other thing: I’m using ASP 3.0 (classic) to access this query using JET OLEDB 4.0.
Anyone know how to define the Distance field as a number?
Thanks!
— EDIT —
Using HansUp’s idea from his answer below, I tried this query to force Access to consider the Distance field as a Single precision number:
SELECT * FROM LocationSearch WHERE CSng(Distance) < @MaxDistance
Even this returned the exact same results as before which included the location in San Diego, 1017 miles away.
If you can’t find a way to return numerical values instead of text from that
Durationfield expression, use your query as a subquery, then castDurationin the containing query.That approach also makes for a nicer
ORDER BY… if that counts for anything. 🙂