Different problem

1. Senior Member
Join Date
Dec 1969
Posts
159

Different problem

I have the following code:<BR><BR>dim strZipCode, intMiles, intDegree, intELatitude, intELongitude, intALatitude, intALongitude, intDistance, strSign<BR><BR>Const pi = 3.14159265358979<BR> <BR>Function GetDistance(Lat1, Long1, Lat2, Long2, Unit)<BR> Dim x<BR> <BR> &#039; do the nasty calcs<BR> x = (Sin(DegToRads(Lat1)) * Sin(DegToRads(Lat2)) + Cos(DegToRads(Lat1)) * _<BR> Cos(DegToRads(Lat2)) * Cos(Abs((DegToRads(Long2)) - (DegToRads(Long1)))))<BR> &#039; Get Acos(x)<BR> x = Atn((Sqr(1 - x ^ 2)) / x)<BR> &#039; Get distance in kilometers<BR> GetDistance = 1.852 * 60.0 * ((x / pi) * 180)<BR><BR> &#039; Convert units if necessary<BR> Select Case UCase(Unit)<BR> Case "M"<BR> GetDistance = GetDistance / 1.609344<BR> Case "N"<BR> GetDistance = GetDistance / 1.852<BR> End Select<BR><BR>End Function<BR><BR>&#039;This function converts decimal degrees (e.g. 45.210104) to radians<BR>Function DegToRads(Deg)<BR> DegToRads = CDbl(Deg * pi / 180)<BR>End Function<BR>&#039;- - - - - - - - FUNCTION - - - - - - - - - - -<BR><BR><BR><BR>&#039;Find Lat and Long for entered Zip Code<BR>strSQL = "SELECT * FROM qryTest WHERE Zip=&#039;" & Request("zipCode") & "&#039;"<BR>Set rsZip = objConData.Execute(strSQL)<BR><BR>rsZip.MoveFirst< BR>Do While Not rsZip.EOF<BR> response.write "AgentZip = " & rsZip("AgentZip") & " Distance: " & rsZip("Distance") & "<BR>"<BR> rsZip.MoveNext<BR>Loop<BR><BR><BR>In the qryTest query it calls the GetDistance function. but when I run the query I get a function not defined error.<BR><BR>How would I define the function? I put it on the ASP page as well as the Global module in Access.<BR><BR>MTmace

2. Senior Member
Join Date
Dec 1969
Posts
96,118

Don't do it this way...

First of all, store the Lat&#039;s and Long&#039;s in the DB *in radians*, so you don&#039;t have to do the silly convert to radians over and over!<BR><BR>Second of all, just use a rough estimate in your SQL query, else you will be doing all this ugly math for every record in the table!<BR><BR>Something like:<BR><BR>SELECT ... WHERE Abs( Lat - @LAT ) &#060; @LATDIFF AND Abs( Lon - @LON ) &#060; @LONDIFF<BR><BR>Where your VBS code supplies numeric values for all the @xxx values.<BR><BR>This will get you all the records that fall within a rough square around the center point. <BR><BR>*Now* you apply the more precise calcs you showed to get the records within a certain radius. And you do that in VBS code.<BR><BR>

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•