Lawrence Barnes
asked on
Make table of Excel spreadsheets and tabs from a folder
Hello EE,
* I need to have Access create a table that lists the filename, filedate, filesize, filepath for a given folder for Excel, Text, and CSV files. (This example please use c:\IncomingData.)
* I also need a way to identify and import multiple tabs within the same Excel file. I imagine that there would be a separate table for each Excel file with a row for each tab.
I will be using the above to import the contents of these files (and tabs) into one table that has the column headers of Field01, Field02, Field03, etc. Based on the Filename (and tab name for Excel) I have a schema table that identifies the contents of each field for further processing.
I appreciate any help in getting this information into Access.
Thank you,
LVBarnes
* I need to have Access create a table that lists the filename, filedate, filesize, filepath for a given folder for Excel, Text, and CSV files. (This example please use c:\IncomingData.)
* I also need a way to identify and import multiple tabs within the same Excel file. I imagine that there would be a separate table for each Excel file with a row for each tab.
I will be using the above to import the contents of these files (and tabs) into one table that has the column headers of Field01, Field02, Field03, etc. Based on the Filename (and tab name for Excel) I have a schema table that identifies the contents of each field for further processing.
I appreciate any help in getting this information into Access.
Thank you,
LVBarnes
Will the Excel tabs already have tables or must you create the tables as you identify the tabs?
For the second step:
1. Create a table (tblTabs: FileName(Text), FilePath(Text).
2. In a code module, add the code in the snippet.
3. Place your cursor in the code and run it (F5).
1. Create a table (tblTabs: FileName(Text), FilePath(Text).
2. In a code module, add the code in the snippet.
3. Place your cursor in the code and run it (F5).
Public Function GetTabs()
Dim rst As New ADODB.Recordset, strSQL As String
Dim strFile As String, strTab As String
Dim xl As Object, sheet As Object
Set xl = CreateObject("Excel.Application")
With rst
.Open "SELECT FileName, FilePath FROM tblFiles", _
CurrentProject.Connection, adOpenStatic, adLockReadOnly
Do While Not .EOF
' if this file is an Excel file,
If LCase(Right(.Fields("FileName"), 3)) = "xls" Then
xl.Workbooks.Open .Fields("FilePath")
For Each sheet In xl.ActiveWorkbook.Sheets
strSQL = "INSERT INTO tblTabs (FileName, TabName) " & _
"VALUES ('" & .Fields("FileName") & "', '" & sheet.Name & "')"
CurrentProject.Connection.Execute strSQL
Next sheet
xl.ActiveWorkbook.Close
xl.Application.Quit
End If
.MoveNext
Loop
.Close
End With
End Function
For the third step:
1. Import your text/comma separated file manually and create an import specification.
2. In a code module, add the code in the snippet.
3. Change "TableName" in the code to the name of the table you will import into.
4. Change "SpecName" in the code to the name of the specification you created in step 1.
5. Place your cursor in the code and run it (F5).
Please let me know if you have any questions.
1. Import your text/comma separated file manually and create an import specification.
2. In a code module, add the code in the snippet.
3. Change "TableName" in the code to the name of the table you will import into.
4. Change "SpecName" in the code to the name of the specification you created in step 1.
5. Place your cursor in the code and run it (F5).
Please let me know if you have any questions.
Public Function ImportFiles()
Dim strSQL As String, rst As New ADODB.Recordset
strSQL = "SELECT tblFiles.FileName, tblFiles.FilePath, tblTabs.TabName " & _
"FROM tblFiles LEFT JOIN tblTabs ON tblFiles.FileName = tblTabs.FileName;"
With rst
.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
Do While Not .EOF
' if this is an Excel file,
If LCase(Right(.Fields("FileName"), 3)) = "xls" Then
' import the this worksheet to the table
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"TableName", .Fields("FilePath"), False, .Fields("TabName")
' else, if this is not an Excel file,
Else
' import the text file to the table
DoCmd.TransferText acImportDelim, "SpecName", "TableName", _
.Fields("FilePath"), False
End If
.MoveNext
Loop
.Close
End With
End Function
ASKER
<Will the Excel tabs already have tables or must you create the tables as you identify the tabs?>
Users are currently merging the contents of all the tabs on a spreadsheet onto one tab, and then uploading the merged tab into the same table. (They currently do this with over 200 spreadsheets/textfiles manually!) So all of the updates are going into the same table. I'm using the Filename, TabName, FilePath, FileDate, to keep track of the data's original source.
Thank you for the post, I'll be trying this out shortly.
ASKER
Hello CWood,
First and Second step are perfect. I think I missed something on the third step as I'm getting Run-time error '3011' where the jet engine cannot find the first tab in the spreadsheet. I've pasted the code I used for this step. I have also opened tblFiles and tblTabs and verified that the filename is the same in both locations.
I've gone over it a few times, hopefully you will see that I missed.
Thank you,
LVBarnes
First and Second step are perfect. I think I missed something on the third step as I'm getting Run-time error '3011' where the jet engine cannot find the first tab in the spreadsheet. I've pasted the code I used for this step. I have also opened tblFiles and tblTabs and verified that the filename is the same in both locations.
I've gone over it a few times, hopefully you will see that I missed.
Thank you,
LVBarnes
Public Function ImportFiles()
Dim strSQL As String, rst As New ADODB.Recordset
strSQL = "SELECT tblFiles.FileName, tblFiles.FilePath, tblTabs.TabName " & _
"FROM tblFiles LEFT JOIN tblTabs ON tblFiles.FileName = tblTabs.FileName;"
With rst
.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
Do While Not .EOF
' if this is an Excel file,
If LCase(Right(.Fields("FileName"), 3)) = "xls" Then
' import the this worksheet to the table
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"tblImport", .Fields("FilePath"), False, .Fields("TabName")
' else, if this is not an Excel file,
Else
' import the text file to the table
DoCmd.TransferText acImportDelim, "ImportSpec1", "tblImport", _
.Fields("FilePath"), False
End If
.MoveNext
Loop
.Close
End With
End Function
Please add the code shown in the snippet and check the tab name in the Immediate window (Ctrl+G) and compare it against the tab name in the Excel file.
Public Function ImportFiles()
Dim strSQL As String, rst As New ADODB.Recordset
strSQL = "SELECT tblFiles.FileName, tblFiles.FilePath, tblTabs.TabName " & _
"FROM tblFiles LEFT JOIN tblTabs ON tblFiles.FileName = tblTabs.FileName;"
With rst
.Open strSQL, CurrentProject.Connection, adOpenStatic, adLockReadOnly
Do While Not .EOF
' if this is an Excel file,
If LCase(Right(.Fields("FileName"), 3)) = "xls" Then
' import the this worksheet to the table
' === ADD CODE ============================
Debug.Print "TabName:" & .Fields("TabName")
'==========================================
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, _
"tblImport", .Fields("FilePath"), False, .Fields("TabName")
' else, if this is not an Excel file,
Else
' import the text file to the table
DoCmd.TransferText acImportDelim, "ImportSpec1", "tblImport", _
.Fields("FilePath"), False
End If
.MoveNext
Loop
.Close
End With
End Function
ASKER
Immediate window = TabName.Sheet1
Spreadsheet First Tab = Sheet1
Spreadsheet First Tab = Sheet1
Please try this:
1. Delete any data in your tblTabs table.
2. Replace the GetTabs function with the one in the snippet.
3. Run this function.
4. Change the fields in the tblImport table to F1, F2, etc.
5. Run the ImportFiles function.
The range in the DoCmd.TransferSpreadsheet method (the last attribute) requires a range of cells in addition to the tab name. Sorry I missed that.
1. Delete any data in your tblTabs table.
2. Replace the GetTabs function with the one in the snippet.
3. Run this function.
4. Change the fields in the tblImport table to F1, F2, etc.
5. Run the ImportFiles function.
The range in the DoCmd.TransferSpreadsheet method (the last attribute) requires a range of cells in addition to the tab name. Sorry I missed that.
Public Function GetTabs()
Dim rst As New ADODB.Recordset, strSQL As String
Dim strFile As String, strTab As String
Dim xl As Object, sheet As Object, rng As Object
Set xl = CreateObject("Excel.Application")
With rst
.Open "SELECT FileName, FilePath FROM tblFiles", _
CurrentProject.Connection, adOpenStatic, adLockReadOnly
Do While Not .EOF
' if this file is an Excel file,
If LCase(Right(.Fields("FileName"), 3)) = "xls" Then
xl.Workbooks.Open .Fields("FilePath")
For Each sheet In xl.ActiveWorkbook.Sheets
Set rng = sheet.UsedRange
strSQL = "INSERT INTO tblTabs (FileName, TabName) " & _
"VALUES ('" & .Fields("FileName") & "', '" & sheet.Name & _
"!" & rng.Address(False, False) & "')"
CurrentProject.Connection.Execute strSQL
Next sheet
xl.ActiveWorkbook.Close
xl.Application.Quit
End If
.MoveNext
Loop
.Close
End With
End Function
ASKER
First let me say that you are incredible and your directions are clear and concise and we are sooo close.
It looks like there is one small thing to work around. tblImport originally had fields named Field01, Field02, Field03, etc. I renamed them to F1, F2, F3... and the Excel spreadsheet imported correctly (all of the tabs.) Once the spreadsheets were completed I received another Error code saying that Field01 was not available. I think the Transferspreadsheet is looking for F1, F2, F3 and the TransferText is looking for Field01, Field02, Field03. Go figure.
I could have each file type transfer to its own table and then merge them together if there is no programmitic setting that would solve the above. What do you think?
It looks like there is one small thing to work around. tblImport originally had fields named Field01, Field02, Field03, etc. I renamed them to F1, F2, F3... and the Excel spreadsheet imported correctly (all of the tabs.) Once the spreadsheets were completed I received another Error code saying that Field01 was not available. I think the Transferspreadsheet is looking for F1, F2, F3 and the TransferText is looking for Field01, Field02, Field03. Go figure.
I could have each file type transfer to its own table and then merge them together if there is no programmitic setting that would solve the above. What do you think?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Agreed and thank you!
You are welcome. Good luck on your project.
1. Create a table (tblFiles) with fields: FileName(Text), FileDate(Text),FileSize(Nu
2. In a code module, add the code in the snippet.
3. Place your cursor in the code and run it (F5).
Open in new window