Link to home
Create AccountLog in
Avatar of Jenkins
JenkinsFlag for United States of America

asked on

Query to import a selected Excel file into Access

I have an Access database with a table to be used for importing a periodic report from Excel. The table has 5 fields.


The format of the Excel import file will always be the same; that being the field names start on row 10 and there are 4 data fields. However,
the number of data rows will vary from one file to the next. Sometimes a file may have 100 records, another time it may have 150, etc, etc.

I want to skip over the first 10 rows (because I only want to import data contained in the 4 data fields) and terminate the import when
the row with "TOTAL" is reached. Or, in other words exclude the row with "TOTAL" in it was well as the first 10 rows.

In the Excel file, the month the report applies to always appears in cell C4. I would like to copy that value into Field5 of the Access table
for every single record.


Because this will be a recurring function, ideally what I'd like to be able to do from Access is click on a query name or a button on a form
and have it open windows explorer where I can navigate to and select the file I want.

Upon doing so, it would run an import automatically that includes and excludes the data as described above,

I have attached a sample Access database with expected results in its table as well as a sample Excel import file.



Thank you.
Avatar of Dale Fye
Dale Fye
Flag of United States of America image

Generally, when I do this, I do one of two things.  In both cases, I use the FileDialog to open the Windows Explorer dialog box and select a file.  Lots of examples here in EE for doing this.

Then, because you have no way of knowing where the Totals line is located, I use Excel Automation to open the Excel file and this is where the two methods differ.

1.  In method 1, I determine where the Total line is located, then close the Excel File, and then use the TransferSpreadsheet method in Access to link the spreadsheet with specific cell ranges identified (eg A10:F100 ) in the Transferspreadsheet method.  This way, Access only links the rows you need as a table which you can then use in an append query to get the data into your destination table.

2.  In method 2, I simply delete the unwanted header rows from the spreadsheet, then locate the Totals row and clear the contents of that row and all subsequent rows, then save the workbook with a new name.  I then use this new name to link the remaining rows to my database and import them into my Access table.

After importing the data via method  1 or 2, I will generally delete the linked table, and with Method #2, I also delete the file that I modified and saved.
IMHO TranferSpreadsheet is only useful when you are performing export/import without any modifications ..
If you want control over the process you need Excel Automation to "talk" to Excel directly and decide on each and cell what you want and what you don't
For start take a look here : Code to read Excel cells into Access table?
It might seem like a bit of hassle but the control over the process is rewarding and you can easily create a loop and import exactly what you want from where you want ...and put them again exactly where should be
ASKER CERTIFIED SOLUTION
Avatar of Mark Edwards
Mark Edwards
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account