It would be better to start with a fresh design, but this would do what you asked to do. Do all this in a junk version of your database, until you are sure it worked.
Caveats - not knowing anything about your data, just knowing your request, and assuming that combinations of ID and name won't repeat later in the data. There's a check step at the end for that.
First, if you are importing from a spreadsheet, then make sure that, during import, Access assigns an autokey. That way, the initial row order will be preserved.
Assuming you did that, your import table would look then like this
Code:
tblWorkSheet
WorkPK PK autonumber added during import
WorkID Number from Worksheet
WorkName Text from worksheet
Work03 thru 10 Other columns from worksheet
STEP ONE:
You create a second table tblKeys
Code:
tblKeys
GroupPK PK autonumber
WorkID Number from tblWorkSheet
WorkName Text from tblWorkSheet
WorkMinPK Number
WorkMaxPK Number
Add these fields to tblWorksheet
GroupFK Number - will load after a few steps below
MatchID Number - will load after a few steps below
UnMatchID Number - will load after a few steps below
UnMatchName Text - will load after a few steps below
STEP TWO:
Then you can do an insert to populate the tblKeys table.
Code:
Query1I:
INSERT INTO tblKeys (WorkID, WorkName, WorkMinPK, WorkMaxPK)
SELECT TW.WorkID, TW.WorkName, MIN(TW.WorkPK), MAX(TW.WorkPK)
FROM tblWorkSheet AS TW
GROUP BY TW.WorkID, TW.WorkName
ORDER BY TW.WorkID, TW.WorkName;
And now you have to test to see if there were any odd situations, where a given ID and Name occurred in multiple groupings.
Code:
SELECT
TM1.GroupPK, TM1.WorkMinPK, TM1.WorkMaxPK,
TM2.GroupPK, TM2.WorkMinPK, TM2.WorkMaxPK
FROM tblMinMax AS TM1, tblMinMax AS TM2
WHERE TM2.GroupPK =
(SELECT Min(TM3.GroupPK)
FROM tblMinMax AS TM3
WHERE TM3.GroupPK > TM1.GroupPK
AND TM3.WorkMinPK < TM1.WorkMaxPK);
If that query has no results, then the following steps will get you your results. If not, then you'll probably need VBA to deal with it.
STEP THREE:
Then assign the resulting autokey field back to tblworksheet, and also set all rows to think that the following row will match.
Code:
Query1U:
UPDATE tblworkSheet AS TW, tblKeys AS TK
SET TW.GroupFK = TK.GroupPK,
TW.MatchID = TK.WorkID
WHERE TW.WorkID = TK.WorkID
AND TW.WorkName = TK.WorkName;
STEP FOUR:
Now we have to correct the ones that don't.
Code:
Query2U:
UPDATE
tblworkSheet AS TW
INNER JOIN
tblKeys AS TK
ON TW.WorkPK = TK.WorkMaxPK
SET TW.MatchID = 0,
TW.UnMatchID = TW.WorkID
TW.UnMatchName = TW.WorkName;