Solved

Make table of Excel spreadsheets and tabs from a folder

Posted on 2009-05-12
13
422 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
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
 
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Syntax Error in Query 7 30
data analyst 3 48
DBF to ... Converter 5 32
How to revise an Access 2003 query into a SQL Server 2008 SQL statement? 1 24
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

919 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

20 Experts available now in Live!

Get 1:1 Help Now