Solved

Make table of Excel spreadsheets and tabs from a folder

Posted on 2009-05-12
13
426 Views
Last Modified: 2013-11-27
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
0
Comment
Question by:Lawrence Barnes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24364153
For the first step:
1. Create a table (tblFiles) with fields: FileName(Text), FileDate(Text),FileSize(Number-Long Integer), and FilePath.
2. In a code module, add the code in the snippet.
3. Place your cursor in the code and run it (F5).
Public Function GetFileData()
    Dim fso As New Scripting.FileSystemObject
    Dim fil As Scripting.File, fld As Scripting.Folder
    Dim strSQL As String
    ' if the folder exists,
    If fso.FolderExists("C:\IncomingData") Then
        ' set the folder
        Set fld = fso.GetFolder("C:\IncomingData")
        ' loop through the files in the folder,
        For Each fil In fld.Files
            ' if the file is one of the target types,
            If LCase(Right(fil.Name, 3)) = "xls" Or LCase(Right(fil.Name, 3)) = "txt" Or _
              LCase(Right(fil.Name, 3)) = "csv" Then
                ' add the file to the table
                strSQL = "INSERT INTO tblFiles (FileName, FileDate, FileSize, FilePath) " & _
                    "VALUES ('" & fil.Name & "', '" & fil.DateCreated & "', " & fil.Size & _
                    ", '" & fil.Path & "')"
                CurrentProject.Connection.Execute strSQL
            End If
        Next fil
    End If
End Function

Open in new window

0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24364174
Will the Excel tabs already have tables or must you create the tables as you identify the tabs?
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24364676
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).
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

Open in new window

0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24364809
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.
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

Open in new window

0
 
LVL 5

Author Comment

by:Lawrence Barnes
ID: 24365105

<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.
0
 
LVL 5

Author Comment

by:Lawrence Barnes
ID: 24367430
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



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

Open in new window

0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24367482
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

Open in new window

0
 
LVL 5

Author Comment

by:Lawrence Barnes
ID: 24367587
Immediate window = TabName.Sheet1
Spreadsheet First Tab = Sheet1
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24368049
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.
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

Open in new window

0
 
LVL 5

Author Comment

by:Lawrence Barnes
ID: 24368655
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?

0
 
LVL 16

Accepted Solution

by:
Chuck Wood earned 500 total points
ID: 24368807
The only thing that would do that, I think, would be if you put headers on your text and csv files (F1, F2, ...) or on your Excel files (Field01, Field02, ...). It would probably be easier for you to import them into two separate tables and then run a simple append query to merge one of the tables into the other and a delete query to clear the redundant data.
0
 
LVL 5

Author Closing Comment

by:Lawrence Barnes
ID: 31580491
Agreed and thank you!
0
 
LVL 16

Expert Comment

by:Chuck Wood
ID: 24369058
You are welcome. Good luck on your project.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question