Forget all your functions, this could be done in a single SQL. (I'll write a few simple queries instead, so you can follow how it works.)
To simpify the look of the code, I've shortened all the names like this -
Code:
tbl1 (Was Table_1)
SerNo, (was Serial_Number)
D1, (Was Insertion_Data1)
F1 (Was Feature1)
tbl2
SerNo,
D2A,
D2B,
F2
tbl3
SerNo,
D3,
F3
(same changes as tbl3 for tbl4 and tlb5)
Here's the query that will get you the feature, if any, in a range in tbl2. I asssumed it was possible for there to be no range match, which is why I'm using a left join. If "feature" is not a text field, then change the NZ function to numeric as appropriate.
Code:
Query QRng2
SELECT tbl1.SerNo, First(NZ(tbl2.F2,"")) as MinF2
FROM tbl1 LEFT JOIN tbl2 ON tbl1.SerNo = tbl2.SerNo
WHERE tbl1.D1 BETWEEN tbl2.D2A AND tbl2.D2B
GROUP BY SerNo;
Here's the queries that will get you the features 3-5 with the smallest differences.
Code:
Query QMin3
SELECT tbl1.SerNo, First(NZ(tbl3.F3,"")) As MinF3
FROM tbl1 INNER JOIN tbl3 ON tbl1.SerNo = tbl3.SerNo
WHERE ABS(tbl1.D1 - tbl3.D3) IN
(SELECT MIN(ABS(t1.D1 - t3.D3))
FROM tbl1 AS t1 INNER JOIN tbl3 AS T3 ON t1.SerNo = t3.SerNo
WHERE t1.SerNo = tbl1.SerNo)
GROUP BY SerNo;
Query QMin4
SELECT tbl1.SerNo, First(NZ(tbl4.F4,"")) As MinF4
FROM tbl1 INNER JOIN tbl4 ON tbl1.SerNo = tbl4.SerNo
WHERE ABS(tbl1.D1 - tbl4.D4) IN
(SELECT MIN(ABS(t1.D1 - t4.D4))
FROM tbl1 AS t1 INNER JOIN tbl4 AS T4 ON t1.SerNo = t4.SerNo
WHERE t1.SerNo = tbl1.SerNo)
GROUP BY SerNo;
Query QMin5
SELECT tbl1.SerNo, First(NZ(tbl5.F5,"")) As MinF5
FROM tbl1 INNER JOIN tbl5 ON tbl1.SerNo = tbl5.SerNo
WHERE ABS(tbl1.D1 - tbl5.D5) IN
(SELECT MIN(ABS(t1.D1 - t5.D5))
FROM tbl1 AS t1 INNER JOIN tbl5 AS T5 ON t1.SerNo = t5.SerNo
WHERE t1.SerNo = tbl1.SerNo)
GROUP BY SerNo;
Each of those queries will return exactly one record per serial_number in tbl1. You should test each one independently to make sure that it is pulling the data you want.
And here's your final query:
Code:
SELECT
tbl1.SerNo,
tbl1.F1,
QRng2.MinF2,
QMin3.MinF3,
QMin4.MinF4,
QMin5.MinF5
FROM
((((tbl1 INNER JOIN QRng2 ON tbl1.SerNo = QRng2.Serno)
INNER JOIN QMin3 ON tbl1.SerNo = QMin3.Serno)
INNER JOIN QMin4 ON tbl1.SerNo = QMin4.Serno)
INNER JOIN QMin5 ON tbl1.SerNo = QMin5.Serno);
Once that's tested, you can change it into a MakeTable query and you're done.