Search code examples
sqlexcelms-accessjoinmerge

Combine sorted records in MS Access


I have two tables in MS Access with the same number of records that I want to "merge" together based on the alphabetical sorting of a common column (col3 in example below). The values in col3 are not unique, and there are no columns or combination of columns in the tables that can be used as a composite key, so an inner join returns excess values. All I want to do is match up records row for row based on the sorted order of col3. Example:

Table 1

col1 col2 col3
A1 A2 A3
B1 B2 B3
C1 C2 C3
C1 C2 C3

Table 2

col3 col4 col5
A3 E1 E2
B3 F1 F2
C3 G1 G2
C3 G1 G2

Output Table

col1 col2 col3 col4 col5
A1 A2 A3 E1 E2
B1 B2 B3 F1 F2
C1 C2 C3 G1 G2
C1 C2 C3 G1 G2

I have tried to add column to each table with unique values using autonumber, but run into the "File sharing lock count exceeded. Increase MaxLocksPerFile registry entry." error, and I don't have registry editing permissions.

I have also tried exporting each table to excel, sorting based on col3, then reimporting the combined table, but this is tedious and leads to errors when re-importing into access.


Solution

  • Add an AutoNumber field to (a copy of) each table.

    This will be assigned values from 1 and up.

    Then join in those two fields:

    Select 
        T1.Field1, T1.Field2, T1.Field3, T2.Field1, T2.Field2, T2.Field3 
    From 
        T1
    Inner Join 
        T2 On T2.Id = T1.Id
    

    Addendum:

    As a table per definition has no ordering, you will then have to:

    • create and use a select query for each table to specify the order
    • create two new empty tables having an AutoNumber field in addition to the fields of the queries
    • create an append query for each select query using this as source to append the records to the two new tables
    • modify the query above to join not the source tables but the new tables