开发者

Access VBA: Run-time error '94': Invalid use of Null

开发者 https://www.devze.com 2023-02-20 02:17 出处:网络
I try to calculate the distance between two points (given the latitude/longitude of those points in decimal format).

I try to calculate the distance between two points (given the latitude/longitude of those points in decimal format).

VBA code:

Const pi = 3.14159265358979

Function distance(lat1, lon1, lat2, lon2)
Dim t开发者_Go百科heta, dist
theta = lon1 - lon2
dist = Sin(deg2rad(lat1)) * Sin(deg2rad(lat2)) + Cos(deg2rad(lat1)) * Cos(deg2rad(lat2)) * Cos(deg2rad(theta))
dist = acos(dist)
dist = rad2deg(dist)
distance = (dist * 60 * 1.1515) * 1.609344
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

Function deg2rad(deg)
deg2rad = CDbl(deg * pi / 180)
End Function

Function rad2deg(rad)
rad2deg = CDbl(rad * 180 / pi)
End Function

And I get error Run-time error '94': Invalid use of Null in this line "deg2rad = CDbl(deg * pi / 180)". I write a query:

SELECT DISTINCT 

([band].E_laip+([band].E_min*(1/60))+([band].E_sec*(1/3600))) AS Band_E_dec, 
([band2].E_laip+([band2].E_min*(1/60))+([band2].E_sec*(1/3600))) AS Band2_E_dec,
([band].N_laip+([band].N_min*(1/60))+([band].N_sec*(1/3600))) AS Band_N_dec, 
([band2].N_laip+([band2].N_min*(1/60))+([band2].N_sec*(1/3600))) AS Band2_N_dec,

distance([Band_N_dec],[Band_E_dec],[Band2_N_dec],[Band2_E_dec]) AS Atstumas

FROM [band] LEFT JOIN band2 ON [band].Stotis = band2.Stotis;

Maybe someone has an idea? Thanks in advance.


If you are using a LEFT JOIN, you allow nulls in your JOINed table when there is no matching row. I would guess you have at least one record in your band table where there's no matching row in band2 on the stotis field.

To find out, do a:

SELECT *
FROM band
LEFT JOIN band2
ON [band].Stotis = band2.Stotis
WHERE band2.stotis IS NULL

If you get any hits you have a referential data issue.

Your solution would be to either do a LEFT JOIN using WHERE Band2.stotis IS NOT NULL or just do an INNER JOIN with the same criteria, which will only return matching rows in both tables.

0

精彩评论

暂无评论...
验证码 换一张
取 消