The tblDistanceMatrix table is exactly right to model this. To populate it
a query like this should fill it with all possible permutations, but
excluding each location linked to itself:
INSERT INTO tblDistanceMatrix (fromLocationID, toLocationID)
SELECT L1.LocationID, L2.LocationID
FROM tblLocation AS L1, tblLocation AS L2
WHERE L1.LocationID <> L2.LocationID
ORDER BY L1.LocationID, L2.LocationID;
For editing the data how about using an unbound form with a subform bound to
the tblDistanceMatrix table in it? The unbound parent form could have a
combo or list box listing all locations like so:
As its RowSource property:
SELECT LocationID, Location FROM tblLocation ORDER BY Location;
and other properties as so:
BoundColumn 1
ColumnCount 2
ColumnWidths 0cm;8cm (or rough equivalent in inches but the first
dimension must be zero to hide the first, LocationID, column, so only the
location shows)
The subform would be based on a query joining the tblLocation table to the
tblDistanceMatrix table as its Recordsource:
SELECT fromLocationID, Location, Distance
FROM tblLocation INNER JOIN tblDistanceMatrix
ON tblDistanceMatrix.toLocationID = tblLocation.LocationID
ORDER BY Location;
The subform, in continuous form, would have two bound controls, text boxes
bound to the Location and Distance columns, but you'll probably want the
former's Enabled property to be set to False and its Locked property to True
to make it read-only.
The subform will be linked to the main form by using the name of the combo
or list box as the LinkMasterFields property of the subform control and
fromLocationID as the LinkChildFields property. When a location is selected
in the combo or list box on the main parent from the subform will show all
locations linked to that location and the distances in each case, which the
user can then edit if necessary.
Ken Sheridan
Stafford, England
David Grant said:
Thanks for the prompt reply. What I actually want to do is provide a grid
interface which allows the user to view and/or edit cable distance values
between electrical device locations in a large building. I provided the
example of the road map grid because I thought it would be easier to
explain.
So what I have is a table: tblLocation. I created a second table
tblDistanceMatrix which contains a concatonated primary key (fromLocationID
& toLocationID) and a numberic field called "distance". tblLocation and an
aliased version of itself link one-to-many to tblDistanceMatrix. The table
behaves as expected, the problem now is how to design a query that does what
I want.
Given a blank tblDistanceMatrix, and 4 locations defined in tblLocation, I
want a query to return the following (as an example):
fromLocationID | toLocationID | Distance
-----------------------------------------
1|1|
1|2|
1|3|
1|4|
2|1|
2|2|
2|3|
2|4|
3|1|
....etc
Is this possible?
Thanks.