Forum Discussion

Kamana08's avatar
Kamana08
Copper Contributor
Mar 27, 2023

Copying Data From Excel file to Access Table

Hello All,  I have created one Shared Excel File in Access using below option: Now I want to copy data from this excel file to two different Access Tables one row at a time. How can I achieve...
  • George_Hepworth's avatar
    George_Hepworth
    Mar 27, 2023

    Kamana08 

     

    Excellent.

     

    There's a fairly straightforward way to deal with this with two queries, and no need for cumbersome recordsets.

     

    First, Append the appropriate field(s) in the new records to the existing table1 (table1 seems like such an ambiguous name, though, are you sure that's it's real name?).

     

    I assume that you have designated a Primary Key for table1 and that that Primary Key is what you need to insert with the records for table2. That's a standard approach, but to be sure, is that how you did this?

     

    Now, you have a new group of records in table1, with a Primary Key and unique values in your other field. You would normally join on Primary Keys/Foreign Keys to append the records to table2. However, since you want to append the newly created Primary Key to table2, you can take advantage of the fact this the required field for the first append is also unique.

     

    Join the original import to table1 on that field. Append the Primary Key from table1 and the other required field from the import into table2. (See why it's so ambiguous to alias tables and fields?) 

     

    Query one could be:

     

    INSERT INTO YourTable1NameGoesHere Values(TheRealNameofFieldOneGoesHere)

    SELECT Field1 

    FROM YourImportTableNameGoesHere

     

    Query two could be:

     

    INSERT INTO YourTable2NameGoesHere Values (PrimaryKeyField1NameFromTable1GoesHere, Field2)

    SELECT YourTable1NameGoesHere INNER JOIN YourImportTableNameGoesHere

    ON YourTable1NameGoesHere.Field1 = YourImportTableNameGoesHere.Field1

     

     

Resources