Link to home
Start Free TrialLog in
Avatar of SteveL13
SteveL13Flag for United States of America

asked on

Why get empty records when doing an Excel import?

Why do I sometimes get empty records in a table when I import from an Excel file?  Here is an example of my VBA code for the import:

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "tblTEMPBaselineSurveyDataImport", Me.FileList, True
SOLUTION
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SteveL13

ASKER

But the rows in the Excel file are empty.  There appears to be no data in any of the cells.
there may be someghing there thats not obvious likeva carraige return.

if you specifically delete the  blank rows from excel before the
import do you still get empty records?
No.  If I intentionally delete what appears to be empty rows in the Excel file prior to import I get no empty records.
so looks like there is something there.

u
you cld run
docmd.runsql "delete * from yourtable where yourcol is null"
after the transferspreadsheet
But then I'll end up with gaps in the auto-number (PK) field in the table.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since Excel allows empty cells/rows, it "remembers" cells/rows that once contained data.  If you select cells and press the delete key, any visible data will be deleted, HOWEVER, Excel REMEMBERS that they once held data.  You must use the delete command from the ribbon to actually delete rows and columns that are no longer needed.

The best way to avoid issues with imports is to link to the spreadsheet and use an append query to select rows with data and append them.  The query also allows you to do some formatting and validation if you need to.
import to temp table.
do cleanup
append to live table
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
fair point
Actually, Pat, if you use the tabledef object, you can simply modify the connection property of the linked table and do a refreshlink to avoid having to delete and relink.
Dale, I'm not sure if that avoids the small bloat caused by relinking but if it does, then that is clearly superior.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mark, interesting.  I have to leave for the day but will take a deeper look at this tomorrow.
I found Mark's solution to work best for me.  Thank you to the others for contributing.
Steve:  Glad I could help.  I've found a working example to be the best way to show others how to do VBA programming.

There are numerous other methods for importing files of various kinds into Access.  Some include being able to pick only the columns you want to import and pulling data from anywhere in the workbook without having to open it.  Which method you choose depends on the files involved, the state of your data (SQL Compliant or Non-SQL Compliant), data types in the column, the use of named-ranges and Excel tables, and where it is located in the workbook.  The same goes for exporting TO Excel.