Q: I’m new to access and my question is: would you help me to convert decimal latitude and longitude into degrees/minutes/seconds? I want the user to enter a latitude coordinate (47.6913) into a Latitude text box and convert it into degrees/ minutes/seconds/direction (47º 41” 28.8‘ N) and output it in a different text box. Also I would like a another text box for the user to enter a longitude coordinate (122.3144) and the output to a different text box (122º 18” 51.8‘ W). Thanks, Harry
A: Well, if I understand converting from decimal to degress, you take the fractional component (in the case of 47.6913 it would be 0.6913) and multiply it by 60 to get minutes, which would be 41.478. Now take that fraction and multiply by 60... 0.478 x 60 = 28.68 which gives you your seconds. So, the code should look something like this:
MyDecimalValue = 47.6913
MyWholeDegrees = Int(MyDecimalValue) '47
MyRemainder = Round(MyDecimalValue - MyWholeDegrees, 4) '.691
MyMinutes = MyRemainder * 60 '41.477
MyWholeMinutes = Int(MyMinutes) '41
MyRemainder = Round(MyMinutes - MyWholeMinutes, 4) '.478
MySeconds = MyRemainder * 60 '28.68
You'll notice a SLIGHT math error pops up because computers suck (47.6913 - 47 should be .6913 but it's .69129999). You can fix this by rounding off your answers to 4 or 5 decimal places.
Now to get this information into your database, just set up the appropriate fields where needed, and then use an AFTERUPDATE event to populate them. Here's a tutorial on that:
/tips/access/afterupdate-event-vba
Hope this helps. -RR