I have been pulling my hair out I've been so frustrated with this one! I need to calculate the Azimuth or true north bearing between two sets of geographic coordinates in Microsoft Access. I found the following code by doing a google search but its not giving me correct results! Half the time its accurate and the other half it gives me a weird random angle. Heres the code:

public function fAzimuth(lat1 as double, lat2 as double, lon1 as double, lon2 as double) as double
dim Arctan as double
dim x as double
dim y as double

x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) *cos(lon2 - lon1)
y = sin(lon2 - lon1) * cos(lat2)
Arctan = excel.application.Atan2(x, y)

If Arctan < 0 and lon1 > lon2 then
fAzimuth = (excel.application.degrees(Arctan) + 360) mod 360
else
fAzimuth = 360 - ((excel.application.degrees(Arctan) + 360) mod 360
end if
end function

Here is an example of it giving me a weird random angle:
?fAzimuth(36.07553, 36.1414, 66.83867, 66.90865)
359

It says the angle is 359 degrees but when you plot both points, it's obvious that the angle should be closer to 45 degrees or 315 degrees if its going counter clockwise. I have no idea how it came up with this angle!

Any help will be greatly appreciated.