Connecting Tech Pros Worldwide Forums | Help | Site Map

Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?

Newbie
 
Join Date: Feb 2007
Posts: 15
#1: Jan 17 '08
Hello Guys,

I have a challenge here and would really appreciate some help. I have customer bills that are all done in excel with a template and stored in directories by month order. At the end of the month or periodically, I need to query and print a report giving customer name, account number, date of service and cost for a particular service/item for all customers who received such a service/item (at times up to 300 customer bills in excel format will be present in the directory). Is it possible to write a script either in access or excel that will search all the files in the directory and prepare such a report? Searching each individual file and having to type or cut and paste the details each time the report is required is quite tedious. This information is stored in the same cell in each bill as a template is used. Does anyone have any idea how to work around this?

Stephen

Expert
 
Join Date: Sep 2007
Posts: 256
#2: Jan 20 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Hi Stephan

Nobody seems to have answered this so I am replying to say there is somebody out here!

I understand from your description that you have a separate worksheet for each customer bill. This is a nightmare scenario (which is probably why nobody has replied!), so my first suggestion is why don't you write a new app in Access and start over!?!

Nevertheless, just listing the steps I would take to build such an app;-
You need a database with at least one table to recieve the data.
You will need a form which needs a field for the location of the latest folder, assuming you can enter this at the end of a month and then click to run a procedure to import the data. You could add a Common Dialog box to help browse and find this folder.
You need to write a procedure which will loop through the files in the folder. As a start I would just display each file name in a message box to ensure they were being read. You can use the Dir() function recursively (loop untill Dir = "", read Help on Dir Function) and it will find the next file, next file etc..
Once I was sure I was finding each file I would try opening it. The following 'snippets' could probably be used in your program

Expand|Select|Wrap|Line Numbers
  1. Dim xcel As Object
  2.     Set xcel = CreateObject("Excel.Application")
  3.  
  4. 'Open spreadsheet, 
  5.     xcel.workbooks.Open "NextFileName"
  6.     xcel.Visible = False
  7.  
  8. 'Select Sheet    
  9.     xcel.Sheets("Sheet1").Select
  10.  
  11. ' Read data from cells into fields of recorset
  12.     !CustName = xcel.Range("A2").Value
  13.     !AccNo = xcel.Range("C3").Value
  14.     !ODate = xcel.Range("D4").Value
  15.     'etc
NextFileName would be the full path + filename including .wks to the current spreadsheet.
You would need to setup a recordset outside the loop and open it so you can write data to the fields you require. (I have not shown setting up the recordset but !CustName is obiously the field for the Customer Name in the recordset you are using)
"A2","C3", "D4" are just my arbitary names for the cells in the spreadsheet template that hold the data you require to extract.

I have not attempted to write the loops, within loops, within loops that are needed for this exercise but having given it a bit of thought it is quite 'do-able' (which is more than I thought at the begining)

I hope this helps a little !

S7
Newbie
 
Join Date: Feb 2007
Posts: 15
#3: Feb 18 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Quote:

Originally Posted by sierra7

Hi Stephan

Nobody seems to have answered this so I am replying to say there is somebody out here!

I understand from your description that you have a separate worksheet for each customer bill. This is a nightmare scenario (which is probably why nobody has replied!), so my first suggestion is why don't you write a new app in Access and start over!?!

Nevertheless, just listing the steps I would take to build such an app;-
You need a database with at least one table to recieve the data.
You will need a form which needs a field for the location of the latest folder, assuming you can enter this at the end of a month and then click to run a procedure to import the data. You could add a Common Dialog box to help browse and find this folder.
You need to write a procedure which will loop through the files in the folder. As a start I would just display each file name in a message box to ensure they were being read. You can use the Dir() function recursively (loop untill Dir = "", read Help on Dir Function) and it will find the next file, next file etc..
Once I was sure I was finding each file I would try opening it. The following 'snippets' could probably be used in your program

Expand|Select|Wrap|Line Numbers
  1. Dim xcel As Object
  2.     Set xcel = CreateObject("Excel.Application")
  3.  
  4. 'Open spreadsheet, 
  5.     xcel.workbooks.Open "NextFileName"
  6.     xcel.Visible = False
  7.  
  8. 'Select Sheet    
  9.     xcel.Sheets("Sheet1").Select
  10.  
  11. ' Read data from cells into fields of recorset
  12.     !CustName = xcel.Range("A2").Value
  13.     !AccNo = xcel.Range("C3").Value
  14.     !ODate = xcel.Range("D4").Value
  15.     'etc
NextFileName would be the full path + filename including .wks to the current spreadsheet.
You would need to setup a recordset outside the loop and open it so you can write data to the fields you require. (I have not shown setting up the recordset but !CustName is obiously the field for the Customer Name in the recordset you are using)
"A2","C3", "D4" are just my arbitary names for the cells in the spreadsheet template that hold the data you require to extract.

I have not attempted to write the loops, within loops, within loops that are needed for this exercise but having given it a bit of thought it is quite 'do-able' (which is more than I thought at the begining)

I hope this helps a little !

S7

Thanks Sierra. Its quite a tall order but I'm trying it out. Having loads of difficulties but I think I am getting thorough. Yes it would have really been better in access but these are files that were already created in microsof excel for the past six years so this confusion is all i have to work with.
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#4: Feb 19 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Quote:

Originally Posted by Stephenoja

Hello Guys,

I have a challenge here and would really appreciate some help. I have customer bills that are all done in excel with a template and stored in directories by month order. At the end of the month or periodically, I need to query and print a report giving customer name, account number, date of service and cost for a particular service/item for all customers who received such a service/item (at times up to 300 customer bills in excel format will be present in the directory). Is it possible to write a script either in access or excel that will search all the files in the directory and prepare such a report? Searching each individual file and having to type or cut and paste the details each time the report is required is quite tedious. This information is stored in the same cell in each bill as a template is used. Does anyone have any idea how to work around this?

Stephen

The following code, although not Optimized, will do exactly what you are requesting, except for the simple part of running a Query against the final Table results (tblCustomerInfo). It actually works quite well on several Excel Spreadsheets but I have no idea how it will perform on 300+. Rather than going into prolonged explanations, I'll simply post the code along with some assumptions, and should you have any questions, please feel free to ask. These assumptions are critical, follow them exactly:
  1. Assumptions
    1. All your Excel Spreadsheets reside in the C:\Customers Directory as in C:\Customers\Customer John Doe.xls.
    2. In each Spreadsheet, the data resides in a consistent location, in this case Customer Name is in Cell "A1", Account Number is in Cell "B1", and Order_Date is in "C1".
    3. tblCustomerInfo (create it) with Fields [Name], [Account#], and [Order_Date].
Expand|Select|Wrap|Line Numbers
  1. Dim strPath As String, appExcel As Excel.Application, strFolderPath As String
  2. Dim MyDB As DAO.Database, MyRS As DAO.Recordset, strSQL As String
  3.  
  4. DoCmd.SetWarnings False
  5.   strSQL = "Delete * From tblCustomerInfo;"
  6.   DoCmd.RunSQL strSQL
  7. DoCmd.SetWarnings True
  8.  
  9. Set MyDB = CurrentDb()
  10. Set MyRS = MyDB.OpenRecordset("tblCustomerInfo", dbOpenDynaset)
  11.  
  12. strFolderPath = "C:\Customers\"
  13. strPath = "C:\Customers\*.xls"     'Set the path.
  14.  
  15. strPath = Dir(strPath, vbNormal)   'Retrieve the first entry.
  16. Set appExcel = CreateObject("Excel.Application")
  17.  
  18. Do While strPath <> ""    'Initiate the loop
  19.   appExcel.Workbooks.Open strFolderPath & strPath
  20.   appExcel.Visible = False
  21.   appExcel.Sheets("Sheet1").Select
  22.     With MyRS
  23.       .AddNew
  24.         !Name = appExcel.Range("A1").Value
  25.         ![Account#] = appExcel.Range("B1").Value
  26.         ![Order_Date] = appExcel.Range("C1").Value
  27.       .Update
  28.     End With
  29. strPath = Dir           'Next entry
  30. Loop
  31.  
  32. appExcel.Quit
  33. Set appExcel = Nothing
  34.  
  35. MyRS.Close
  36. Set MyRS = Nothing
  37.  
  38. MsgBox "This loooooong process has completed!"
  39.  
SAMPLE OUTPUT (tblCustomerInfo):
Expand|Select|Wrap|Line Numbers
  1. ID    Name            Account#       Order_Date
  2. 10    Customer 1    123467         1/23/2008
  3. 11    Customer 2    91023556Y55     4/23/2008
  4. 12    Customer 3    HGT5543FR     11/29/2008
  5.  
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#5: Feb 19 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Quote:

Originally Posted by Stephenoja

Hello Guys,

I have a challenge here and would really appreciate some help. I have customer bills that are all done in excel with a template and stored in directories by month order. At the end of the month or periodically, I need to query and print a report giving customer name, account number, date of service and cost for a particular service/item for all customers who received such a service/item (at times up to 300 customer bills in excel format will be present in the directory). Is it possible to write a script either in access or excel that will search all the files in the directory and prepare such a report? Searching each individual file and having to type or cut and paste the details each time the report is required is quite tedious. This information is stored in the same cell in each bill as a template is used. Does anyone have any idea how to work around this?

Stephen

For my own curiosity, and to further assist you, I ran my code on 3 separate trials against 50 Excel Spreadsheets, the results are as follows:
Expand|Select|Wrap|Line Numbers
  1. 145.10 seconds ==> 2.42 minutes
  2. 144.03 seconds ==> 2.40 minutes
  3. 151.03 seconds ==> 2.52 seconds
  4.  
  5. Average Process Time for 3 Trials : 2.45 minutes
The code ran without a hitch. On 300+ Spreadsheets, I would start the process go out to lunch, and when you returned it should be finished. If you are going to use this approach, let me know since there are a few things that I would like to point out, and the code itself can probably be improved.
Expert
 
Join Date: Sep 2007
Posts: 256
#6: Feb 20 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Nice one ADenzii !

What have you got loaded in References to allow you to
Expand|Select|Wrap|Line Numbers
  1.  Dim appExcel As Excel.Application
I have to create OBJECT then set the Object to Excel.Application as two lines.

S7
ADezii's Avatar
Expert
 
Join Date: Apr 2006
Location: Philadelphia
Posts: 5,219
#7: Feb 20 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Quote:

Originally Posted by sierra7

Nice one ADenzii !

What have you got loaded in References to allow you to

Expand|Select|Wrap|Line Numbers
  1.  Dim appExcel As Excel.Application
I have to create OBJECT then set the Object to Excel.Application as two lines.

S7

Thanks sierra7, the Reference is to:
Expand|Select|Wrap|Line Numbers
  1. Microsoft Excel XX.X Object Library
Expert
 
Join Date: Sep 2007
Posts: 256
#8: Feb 20 '08

re: Searching Multiple Excel Files & Printing Report in MS Access-Is this possible?


Quote:

Originally Posted by ADezii

Thanks sierra7, the Reference is to:

Expand|Select|Wrap|Line Numbers
  1. Microsoft Excel XX.X Object Library

Duh!!
Thanks! I had a reference to Office loaded which obviously didn't work.
S7
Reply