Create a query from your Zips table that has Zip, City, State, longitude,
latitude in that order.
Create and unbound form and place two comboboxes (Combo1 and Combo2) with the
aformentioned query as the source, sorted the way you want. Place the
following code behind the AfterUpdate of the Combo2:
Private Sub Combo2_AfterUpdate()
Dim lon1, lon2, lat1, lat2, radi, GetDistance, x As Double
Dim FrmCity, ToCity, FrmState, ToState As String
radi = 3956 'set the earth's radius
lon1 = Left(Me.Combo1, InStr(Me.Combo1, "/") - 1)
lat1 = Right(Me.Combo1, Len(Me.Combo1) - InStr(Me.Combo1, "/"))
lon2 = Left(Me.Combo2, InStr(Me.Combo2, "/") - 1)
lat2 = Right(Me.Combo2, Len(Me.Combo2) - InStr(Me.Combo2, "/"))
x = (Sin(DegToRads(lat1)) * Sin(DegToRads(lat2)) + Cos(DegToRads(lat1))
* Cos(DegToRads(lat2)) * Cos(Abs((DegToRads(lon2)) - (DegToRads(lon1)))))
' Get Acos(x)
x = Atn((Sqr(1 - x ^ 2)) / x)
FrmCity = Me.Combo1.Column(1)
FrmState = Me.Combo1.Column(2)
ToCity = Me.Combo2.Column(1)
ToState = Me.Combo2.Column(2)
Me.Distance = "From " & FrmCity & ", " & FrmState & " to " & ToCity & ",
" & ToState & " is " & Round(radi * x, 2) & " miles"
End Sub
You will also need two functions:
Function DegToRads(Deg)
DegToRads = CDbl(Deg * pi / 180)
End Function
Function acos(rad)
If Abs(rad) <> 1 Then
acos = pi / 2 - Atn(rad / Sqr(1 - rad * rad))
ElseIf rad = -1 Then
acos = pi
End If
End Function
Zipz.zip at this location:
http://www.census.gov/geo/www/gazetteer/gazette.html
seems to be a pretty comprehensive zip code file. You'l have to change all
the longitudes to negative numbers.
There is a file at that same URL that show the layout of the file.
The mileages I get seem pretty close for zips I entered except for some
places that are close so there is no guarantee on accuracy.
sorry for the previous bad info. Hope this helps.
Bob said:
Hold off onthe equation! Three problems:
1)arctan is atn in VBA
2)sqrt is sqr in VBA
3)It doesn't give the correct distance
I'm searching for the problem.
You can find a free .csv of zip codes with longitude and latitude here:
[quoted text clipped - 17 lines]