Results 1 to 5 of 5
  1. #1
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127

    Copying field based on other closest matching values.

    I've been trying to come back to this and chip away for months with little progress. It's tough to explain so I've included a sample DB.



    What I want to do is compare the 'distance' between the two tables, tblKnown and tblFind.

    Using the closest matching 'distance' fields to join the records, the 'dip' value will be copied from tblKnown to the 'NearestDip' in tblFind. The same is needed for 'Dir' and 'NearestDir'. It is important that the project ID be matching as well.

    To try to sum this up again... The values in tblFind will be updated using values in tblKnown. Which records are selected is dictated by where 'Distance' in tblKnown most closely matches the 'Distance' in tblFind.

    I have filled a few of the values in tblFind as an example. But 'NearestDip' and 'NearestDir' will always be updated by the query (I hope).

    If the difference between 'Distance' is split evenly between two records in 'tblKnown', then I don't care which one gets picked. Preferably the one with the lowest 'Distance' value.

    I hope that makes sense. I've been scratching my head with DLookup, ELookup, etc. off and on for quite a while. If this can be done without more advanced functions then that would be great too.Nearest1.accdb

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Need to change Project field in tblKnown to text type. Then consider:

    Query1:

    SELECT tblFind.ID, tblFind.Project, tblFind.Distance AS FindDist, tblKnown.Distance AS KnownDist, Abs([tblKnown].[Distance]-[tblFind].[Distance]) AS Diff, tblKnown.Dip, tblKnown.Dir
    FROM tblKnown INNER JOIN tblFind ON tblKnown.Project = tblFind.Project
    ORDER BY tblFind.ID;

    Query2:

    SELECT Query1.* FROM Query1 WHERE Diff=DMin("Diff","Query1","ID=" & [ID]);

    or

    SELECT Query1.* FROM Query1 WHERE Diff IN (SELECT Top 1 Diff FROM Query1 AS Dupe WHERE Dupe.ID=Query1.ID ORDER BY Diff);
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127
    I think this works nicely. Thank you very much, June7.

    What would be the differences between the two options for 'Query2'?

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Domain aggregate functions can cause slower performance. But then so can some nested queries. Try both. What happens?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    JRodko is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2018
    Posts
    127
    Results are identical but the latter is much quicker.

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

Similar Threads

  1. Replies: 2
    Last Post: 09-03-2015, 04:38 PM
  2. Replies: 22
    Last Post: 01-23-2015, 01:16 PM
  3. Query to join records with the closest values
    By dipique in forum Queries
    Replies: 2
    Last Post: 09-08-2014, 04:11 PM
  4. Replies: 5
    Last Post: 07-29-2014, 11:20 AM
  5. Help copying field values within a form
    By cshort64 in forum Programming
    Replies: 3
    Last Post: 11-11-2011, 01:07 PM

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