Solved

Make table of Excel spreadsheets and tabs from a folder

Posted on 2009-05-12
13
419 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
  • 8
  • 5
13 Comments
 
LVL 16

Expert Comment

by:Chuck Wood
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 16

Expert Comment

by:Chuck Wood
Comment Utility
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
Comment Utility

<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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 16

Expert Comment

by:Chuck Wood
Comment Utility
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
Comment Utility
Immediate window = TabName.Sheet1
Spreadsheet First Tab = Sheet1
0
 
LVL 16

Expert Comment

by:Chuck Wood
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Agreed and thank you!
0
 
LVL 16

Expert Comment

by:Chuck Wood
Comment Utility
You are welcome. Good luck on your project.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

771 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now