Quickly copy Excel rows to Access

CedarTree

Registered User.
Local time
, 23:31
Joined
Mar 2, 2018
Messages
417
Hello - I have an Excel workbook that has a complex structure, and so I can't just import the whole thing into Access using VBA. Rather, I want to import certain rows into a pre-existing Access table (all Cols exist in Access, but all Cols may or may not exist in Excel). I have coding working to import one cell at a time, but as you can imagine, this takes too long. Is there a more efficient way to grab the whole table of data, or at least a row at a time? Thanks!!!
 
PS, I believe you can export a recordset to a specific Excel location (using recordsets?)... so this would go the other way.
 
Perhaps use sql?

Something like this to get you started

SELECT *
FROM (SELECT * FROM [sheet1$D25:F40] AS xlData IN 'C:\path\XLfile.XLSX'[Excel 12.0;HDR=no;IMEX=0;ACCDB=Yes] WHERE True) AS XL;

change sheetname/range/path/filename to suit. Set HDR=Yes if the first row contains headers. I've included a criteria you can modify to reduce the number of records if required otherwise you can remove

From this you can convert to an update or append query as required. Utilise functions like Val, CDbl, cCurr etc to change datatypes if required
 
Works like a charm. Follow-up question is how to add a running "row" # to the SQL statement in case a specific record needs to be audited. Thanks!
 
what does that mean? An autonumber is unique to the record, not to one or more fields
 
Right - the row # needs to be tied to another column which tells me the Excel worksheet. So row #1 might repeat many times.
 
It's the row number in the Excel sheet. I wonder if I could use the row function on Excel somehow in the SQL statement?
 

Users who are viewing this thread

Back
Top Bottom