Results 1 to 15 of 15
  1. #1
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118

    Find nearest location

    I have Territory Mgmt database. Sometimes when I get a new lead I need to quickly find other leads in the database that are closest to it.
    Im currently not tracking latitude/longitude etc. Do I need to? Is there a way to set certain location marks by address?


    If so, once I put in the latitudel/longitude how do I create a search to find the nearest?
    Any suggestions, ideas or sample database would be helpful....Thanks

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    How is your VBA coming along? You would need to know Intermediate to Advanced VBA techniques to use apps like MapPoint. If you also have skills in XML, you can use Advanced VBA and XML to employ Google Maps API's.

  3. #3
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    You may want to look into Google Maps Javascript API - https://developers.google.com/maps/d...on/javascript/
    You would have to find the most suitable way for you to have your Access application interact with webpages to get the sort of result you want. Finding the nearest leads may be tricky and may exceed the limitations of free use, requiring you to spend money.

    If you want to use Access and only Access, then you will need Lat/Long for each lead. This is just a general outline of what steps to take:

    - Geocode all points of interest.
    - Find a way to geocode the new lead you are inputting.
    - Look into the Haversine Formula to determine the distance between the new lead's coordinates and ALL the other leads (this distance is as the crow flies). Then you can find the nearest ones.

    You'll probably want to do most of this using VBA.

  4. #4
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118
    Not advanced at all. Only know self taught coding with VBA. Never trained on it.

  5. #5
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118
    I think I would prefer to just use Access. I can do the leg work of putting all the lats and longs. I just need to know now how to enter a new lat and long and ask the database to tell me what address are closest?
    Any simple ideas?

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    You could look up the distance between two points using your web browser and type the value into a table.

  7. #7
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    wnicole,

    If you only want to use Access, you would require the following:

    • Each lead must have a stored address, latitude, and longitude
    • Any new lead must be entered with all 3 as well


    If you are able to do the above, then you can create a user-defined function that uses the Haversine formula to find the distances. For example, you can create a VBA Module and then have in it code that looks something like this

    Code:
    Public Function Haversine(OrigLat as Double, OrigLong as Double, DestLat as Double, DestLong as Double) as Double
    
        Dim pi as Double, AsinBase as Double
        pi = 4 * Atn(1)
    
        OrigLat = (OrigLat / 180) * Pi
        DestLat = (DestLat / 180) * Pi
        OrigLong = (OrigLong / 180) * Pi
        DestLong = (DestLong / 180) * Pi
        AsinBase = Sin(Sqr(Sin((OrigLat - DestLat) / 2) ^ 2 + _
        Cos(OrigLat) * Cos(DestLat) * Sin((OrigLong - DestLong) / 2) ^ 2))
        AsinBase = (AsinBase / Sqr(-AsinBase * AsinBase + 1))
        AsinBase = 2 * AsinBase * 6371      '6371 is the radius of the Earth in km
        AsinBase = AsinBase * 0.621371      'this is for conversion to miles
        Haversine = Round(AsinBase, 2)       'rounds to 2 decimal points
    
    End Function
    And then in a query, you can have a column with a calculated field that looks like this:

    Distance: Haversine(<LatOfNew>,<LongOfNew>, [Table Name]![Latitude], [Table Name]![Longitude])

    That should give you a general idea of what to do.

    HTH

  8. #8
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118
    When adding lat and long to my table. The field format for it should be what? Long integer? decimal places? Format. Im asking because when I copy the long and lat into the database it only give me whole numbers not the decimal places. Can you assist?

  9. #9
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Lat and Long fields should be datatype "Number", with Field Size as "Double".

  10. #10
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118
    I can only hope to be as skilled as you. Thanks so much. I'm now going to try your code. Scary :-)
    My thinking is I will put this is my query: Distance: Haversine(<LatOfNew>,<LongOfNew>, [Table Name]![Latitude], [Table Name]![Longitude])
    Then create a form and use the button to output the code? Correct.

  11. #11
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Nice UDF.......

  12. #12
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    wnicole,

    To have this available on a form, in the query, put something like this for a calculated field:

    Distance: Haversine([Forms]![Form Name]![Lat Field], [Forms]![Form Name]![Long Field], [Table Name]![Latitude], [Table Name]![Longitude])

    Then on your form, you can either have a button to open the query itself, or you can have a Subform whose Recordsource is the query.

    Do you want to only return the nearest site? Or any number of them based on distance?

  13. #13
    wnicole is offline Competent Performer
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    118
    Just like the top 5 nearest sites

  14. #14
    thebigthing313 is offline Competent Performer
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2014
    Posts
    119
    Then I would suggest going the subform route I described above. If you need help trying to apply all this, don't hesitate to ask!

  15. #15
    orange's Avatar
    orange is offline Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,850
    wnicole,
    How accurate do you need to be? If you don't have lat/long, you may not need them
    I have a sample database using Access with Google maps that will find distance between 2 points(lat/long) or 2 zips etc or any combination.
    see post #38 here

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Hyperlink Location
    By Mahendra1000 in forum Access
    Replies: 1
    Last Post: 09-26-2013, 09:00 AM
  2. Replies: 1
    Last Post: 03-14-2013, 10:16 AM
  3. Find the Nearest Previous Saturday
    By RachelBedi in forum Access
    Replies: 1
    Last Post: 09-21-2012, 12:51 PM
  4. Round up to nearest nickel
    By Rhubie in forum Access
    Replies: 15
    Last Post: 09-07-2012, 06:50 AM
  5. File Location
    By Mitch87 in forum Programming
    Replies: 1
    Last Post: 02-19-2010, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums