473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Lookup then find closest entry based on 2 values

I am using Access 2007 and am building a database of postal codes & lat/long coordinates. I want to end up with something where you can input a postal code, then return an address from a different table with the nearest latitude & longitude. I have seen many examples of "nearest value" code but none that incorporate finding the minimum absolute difference between 2 values.

In other words, I have 2 tables: PostalReference and SiteList. PostalReference contains every postal code & its associated lat-long:

A0A 47.0073 -52.9589
B0H 45.6051 -61.6975

et cetera. I want to be able to input a postal code, then find the associated lat/long and the entry with the nearest lat/long from the SiteList table.
Feb 10 '14 #1

✓ answered by Rabbit

For 1, you can use a query that takes your parameter and returns the lat and long.

For 2, you can use the haversine formula to calculate the distance. If you don't know the formula, you can read more about it here: https://en.wikipedia.org/wiki/Haversine_formula

For 3, you take the distances calculated from 2 and find the minimum.

Depending on how many sites you have, I would suggest against calculating it every time. It can be an expensive operation and take a while if you have a lot of sites. Instead I would calculate it once for each site and store a foreign key to the sites in the postal code table.

9 1887
mcupito
294 256MB
You didn't tell us how the 2 tables are connected. Is there a PK in PostalReference that is a FK in SiteList that you use to look up these values?
Feb 10 '14 #2
There are matching postal codes in both databases that match up. I have seen that article; the problem I have is in looking up an inputted postal code (like a parameter, [postal code]) and finding its lat & long, then looking up the closest postal code in another table based on the lat & long.

So, that article would be perfect if I was inputting latitude and finding closest latitude, but that is not quite what I need.
Feb 10 '14 #3
NeoPa
32,556 Expert Mod 16PB
Not clear on the question, but I expect you need to have three steps to the process :
  1. Convert Post Code to Lat/Long.
  2. Search for closest match in Lat/Long table.
  3. Convert reult back to Post Code.
Is there any of those steps you feel you need help with?
Feb 10 '14 #4
Yes, except for 2. It is to search for the closest match in a different table. I need help for all of those steps. Specifically I cannot find the answer to:

1. How to take inputted parameter and find the lat/long from it
2. How to find the nearest value of lat/long in the SiteList table (the min (abs ()) code I have seen examples of do not account for needing to find the nearest of 2 paired values)
3. Return the entry with that closest lat/long.

I have been doing a lot of googling trying to find the answer, to no avail. If this has already been answered somewhere please point me in the right direction.
Feb 10 '14 #5
mcupito
294 256MB
Okay - for 1) If you are inputting the postal code into a text box or a form, what is the name of the control on the form for the query? That would go in the "Criteria" section of the query.

Expand|Select|Wrap|Line Numbers
  1. Like [Forms]![YourForm]![PostalCodeTxt]
By the design of RDMS, because the PostalCode is a PK, it would give you the correlating Lat/Long associated with the value you pass in for the Postal Code.

For 2.) You're going to have to find the difference of all lat/long values that pertain to the value from #1 within the SiteList Table.

Maybe you can do this with a Temp Table?

For 3) You would take the difference from #2) and take the DMin() of the [DifferenceField] that you're going to create. That way, you are getting the smallest difference in distance, thus - the closest Site.

I believe that is the correct logic. If not, someone should correct me (like usual :) )
Feb 11 '14 #6
NeoPa
32,556 Expert Mod 16PB
In your OP (Original Post) you say :
"PostalReference contains every postal code & its associated lat-long"

Getting one from the other is a simple matter running a query with a filter that shows what you already have. Is this something you're likely to have problems with (As this is very much the very basics of working with databases so that could represent a problem)?
Feb 11 '14 #7
Rabbit
12,516 Expert Mod 8TB
For 1, you can use a query that takes your parameter and returns the lat and long.

For 2, you can use the haversine formula to calculate the distance. If you don't know the formula, you can read more about it here: https://en.wikipedia.org/wiki/Haversine_formula

For 3, you take the distances calculated from 2 and find the minimum.

Depending on how many sites you have, I would suggest against calculating it every time. It can be an expensive operation and take a while if you have a lot of sites. Instead I would calculate it once for each site and store a foreign key to the sites in the postal code table.
Feb 11 '14 #8
Rabbit, I didn't know of the Haversine formula, and the breakdown of the logic is what I needed. Thanks so much everyone for all your help!
Feb 11 '14 #9
mcupito
294 256MB
Glad you figured it out.
Feb 11 '14 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Randy Crockett | last post by:
I have created a very simple DLL in VC++ 6.0, then created a very simple App in VC++ 6.0 to use the DLL and this works fine When I try to use the same DLL in C#, with DLLImport, the app finds the...
0
by: Sam Fields | last post by:
I have found very little regarding the error "Unable to find an entry point named EnumerateSecurityPackagesW in DLL security.dll. ". I have an ASP.NET Web Service being accessed via SSL. I found...
1
by: TRI_CODER | last post by:
I am trying to solve the following exception. The exception occurs when my ASP.NET code behind code attemtps to access a remore site using SSL. Please note that all certificates are valid and the...
6
by: Prashant Bhuptani | last post by:
Hi Guys, I am trying to use a C++ dll in VB.NET code. I have imported the dll in the following manner: <code> Imports System.Runtime.InteropServices
5
by: Pratibha | last post by:
hi, i made a dll named rtbdts.dll and call it from vb.net through dllimport <DllImport("C:\misc\rtbdll\rtbdts.dll")> Public Shared Function Init() As String End Function But it returns the...
3
by: Saman | last post by:
I have a third party dll and I am sure that it is not an activeX or dotnet assembly . I have check it up with Dependency Walker software and found the list of it's exported function in C++ syntax...
3
by: Gomathi | last post by:
hi all, In ASP.Net , i'm using a dataset. I want to find whether it has any values or not. How to do that? Thanks in advance. Regards, Gomathi
1
by: raam_kimi | last post by:
Hi All I got some problem in importing advapi32.dll when i call the LogonUser method it throws an error like this 'Unable to find an entry point named LogonUser in DLL advapi32.dll.' ...
1
by: svjim | last post by:
I am working on an iPhone application which uses a version of safari (UIWebView) to display web pages. I want to be able to capture images shown on the webview. To do so, I can capture a "touch...
4
by: AbbasBD | last post by:
I want to delete entry in table where linked table has no record. Statement is - ----------- DELETE .* FROM WHERE (((.BuyInvcID) In ( SELECT . FROM WHERE . Is Null )));
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.