473,395 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Exporting to multiple spreadsheets

32
Hi,

I have a spreadsheet which I've imported into one table, 'Assets', in Access. In the table, I have to separate the data based on one attribute: 'empID'. empID is the employee ID and can occur in multiple rows, so it's not the primary key.

What I need to do is this:

1. go through the entire Assets table and produce a spreadsheet for each empID. So for each unique empID, a spreadsheet will be made containing all the data related in that record. If an empID has more than one entry in the table, it will be included on a new line in the spreadsheet.

ex.

Assets
empID --- country --- name
eR123 --- Canada --- Jason
eN432 --- China --- Clifford
eR123 --- Canada --- Jason
eU543 --- Mexico --- Alex

This would give 3 new spreadsheets names eR123.xls with 2 rows, eN432.xls and eU543.xls both with 1 row each.

2. These speadsheets have to be named based on their empID. So all spreadsheet for empID 'eR123' would be named 'eR123.xls'. I have a template spreadsheet that can be used, but I'm not sure how to name them dynamically using VB or macros. I have several thousand records to process so doing this manually isn't an option.

Any help or direction would be very much appreciated!!

Thanks,
Jason
Jun 18 '08 #1
7 3773
PianoMan64
374 Expert 256MB
Well, you're going to need some help with setting some things up.

I've included an example of what you're going to need to do, but I will explaine it for you, of what I've done.

I'm assuming you already have a table that has all these items in it. You will need to get a list of all the field names that you're going to be using and include those in the code example that I've sent along.

You will need to setup a Export table called ExportTBL.
Add all the fields that you want to export.
Then you will need to create a group query by pasting the following code into the SQL view of a query.

Expand|Select|Wrap|Line Numbers
  1. SELECT EmpID from [Table Name of All Entries] GROUP BY EmpID
  2.  
Once you done that create a blank form that is not bound to any table or query.

Then create a button by click on command button in toolbar and drawing a command button. Name the command button whatever you'd like it to be.

Then on the OnClick Event, paste the following code into the form.

Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim MyRS As DAO.Recordset
  3. Dim MyLt As DAO.Recordset
  4. Dim MyEx As DAO.Recordset
  5. Dim CurrentRS As DAO.Recordset
  6.  
  7. Set MyDB = CurrentDb()
  8. Set MyRS = MyDB.OpenRecordset("MainTable", dbOpenSnapshot)
  9. Set MyLt = MyDB.OpenRecordset("List", dbOpenSnapshot)
  10.  
  11. Do While Not MyLt.EOF
  12. MyRS.FindFirst ("[EmpID]='" & MyLt!EmpID & "'")
  13. Set MyEx = MyDB.OpenRecordset("ExportTBL", dbOpenDynaset)
  14. If Not MyRS.NoMatch Then
  15. Do While Not MyRS.EOF
  16. MyEx.AddNew
  17. MyEx!EmpID = MyRS!EmpID
  18. MyEx!Country = MyRS!Country
  19. MyEx!UserName = MyRS!UserName
  20. MyEx.Update
  21. MyRS.FindNext ("[EmpID]='" & MyLt!EmpID & "'")
  22. If MyRS.NoMatch Then Exit Do
  23. Loop
  24. End If
  25. MyEx.Close
  26. DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel5, "ExportTBL", "C:\" & MyLt!EmpID, True
  27. DoCmd.SetWarnings False
  28. DoCmd.RunSQL "DELETE * FROM ExportTBL", False
  29. DoCmd.SetWarnings True
  30. MyLt.MoveNext
  31. Loop
  32.  
PLEASE MAKE SURE THAT YOU REFERENCE THE DAO LIBRARY ON YOUR SYSTEM. you get there by going into the Visual Basic Editor, Then clicking on Tools, Reference, and then Locate Microsoft DAO 3.x version that you have installed on your computer.

That should do what you need to do. If you have any questions, I've attached the database to the reponse if you have any formating or syntax questions.

Hope that helps,

Joe P.
Attached Files
File Type: zip TestExcel.zip (21.4 KB, 164 views)
Jun 19 '08 #2
jkwok
32
Hi Joe P,

This looks amazing! I really appreciate all the time and effort you put into helping me out! I'll try this and let you know how it turns out.

Thanks again!

Jason
Jun 20 '08 #3
jkwok
32
Hey PianoMan64,

Your code worked perfectly. I was able to create all 1800 spreadsheets and they were are properly named! Thank you very much!

I have one more issue though. When the spreadsheets are created, is there anyway to have them use a template I have? The person who will be using this has a preset format she has to use, along with a few macros, and has made a template for it.

It's currently named assets.xlt. I was looking at the TransferSpreadsheet method and I didn't see an argument for a template to be included. I believe there is one in the OutputTo method, however I don't know if that would work the way I need it to.

So, using the code already layed out for me by PianoMan64, can anyone think of a way to include the use of this Excel template I have?

Thanks,
Jason
Jun 25 '08 #4
jkwok
32
If it's of any help, I'm using Access and Excel 2003. I've been searching the msdn site but haven't found much of anything relevant, but it must be possible to export to a template right? :D

Thanks again,
Jason
Jun 27 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
Well, no I'm sorry to say. I guess not too many people actually use Excel templates. However, using VBA and Excel automation the attached two subroutines can replace the DoCmd.TransferSpreadsheet method call in Joe's code, allowing you to specify the template filename as the input file

Usage:
Replace the Docmd line with
Expand|Select|Wrap|Line Numbers
  1. TransferSpreadsheet TableorQueryName, templatename, outputfilename
Include the full path in the filenames.
Expand|Select|Wrap|Line Numbers
  1. 'This replacement for DoCmd.TransferSpreadsheet provides ability to specify the
  2. 'input Excel file to transfer query or table data from Access
  3. 'An Excel template file can be used as the input filename if required
  4. 'All filenames should be specified as full paths.
  5. '
  6. 'To use these please ensure that there are references to
  7. 'the DAO and Excel object libraries in the VB environment
  8. '- select Tools, References and ensure that
  9. 'Microsoft DAO 3.6 Object Library (or later) is ticked, and the
  10. 'Microsoft Excel 11 Object Library (or later)
  11. '
  12. Public Sub TransferSpreadsheet(ByVal Tablename As String, Optional ByVal InputxlFilename As String = "", Optional ByVal ExportxlFileName As String = "")
  13.     On Error GoTo Err_Handler
  14.     Dim objExcel As Excel.Application
  15.     Dim TheFilename As String
  16.     Set objExcel = New Excel.Application
  17.     If InputxlFilename = "" Then
  18.         objExcel.Workbooks.Add
  19.     Else
  20.         objExcel.Workbooks.Open (InputxlFilename)
  21.     End If
  22.     TransferQueryData Tablename, objExcel
  23.     If ExportxlFileName = "" Then
  24.         TheFilename = objExcel.Application.GetSaveAsFilename(, "Excel WorkBook (*.xls), .xls")
  25.     Else
  26.         TheFilename = ExportxlFileName
  27.     End If
  28.     objExcel.DisplayAlerts = False
  29.     objExcel.ActiveWorkbook.SaveAs FileName:=TheFilename
  30.     objExcel.DisplayAlerts = True
  31.     objExcel.Quit
  32.     Set objExcel = Nothing
  33.     Exit Sub
  34. Err_Handler:
  35.     MsgBox Err.Description, vbExclamation, "Error " & Err.Number
  36. End Sub
  37.  
  38. Private Sub TransferQueryData(ByVal QueryName As String, ByRef objExcel As Excel.Application)
  39.     Dim TheQuery As DAO.Recordset, DataCopied As Boolean
  40.     Dim N As Integer, R As Long, i As Integer
  41.     If Len(QueryName) = 0 Then
  42.         MsgBox "No query name supplied - data not transferred", vbCritical
  43.         Exit Sub
  44.     End If
  45.     On Error GoTo Err_Handler
  46.     Set TheQuery = CurrentDb.OpenRecordset(QueryName)
  47.     If TheQuery.EOF Then
  48.         'no records - exit after closing recordset
  49.         TheQuery.Close
  50.         Exit Sub
  51.     End If
  52.     TheQuery.MoveLast
  53.     R = TheQuery.RecordCount
  54.     TheQuery.MoveFirst
  55.     N = TheQuery.Fields.Count
  56.     'copy query data to second and subsequent rows
  57.     With objExcel.ActiveSheet
  58.         .Cells(2, 1).CopyFromRecordset TheQuery
  59.     End With
  60.     ' transfer field names to first row
  61.     With objExcel.ActiveSheet
  62.         For i = 0 To N - 1
  63.             .Cells(1, i + 1) = TheQuery.Fields(i).Name
  64.         Next i
  65.     End With
  66.     'finished with recordset - close it
  67.     TheQuery.Close
  68.     Exit Sub
  69. Err_Handler:
  70.     MsgBox Err.Description, vbExclamation, "Error " & Err.Number
  71. End Sub
-Stewart
Jun 27 '08 #6
jkwok
32
Hey Stewart,

This worked perfectly. It ran the first time through without a single problem! I can't thank you and PianoMan64 enough for all of your efforts!!!

Thanks again,
Jason
Jun 30 '08 #7
PianoMan64
374 Expert 256MB
Hey Stewart,

This worked perfectly. It ran the first time through without a single problem! I can't thank you and PianoMan64 enough for all of your efforts!!!

Thanks again,
Jason
Glad we could help.

Joe P.
Jul 1 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
3
by: sridevi | last post by:
Hello How to export data from ms-access database to excel worksheet using ASP. mainly i need to export data to multiple worksheets. it is very urgent to us. i have a sample code which works...
0
by: Kevin | last post by:
I'm using a bit of code I wrote awhile ago to render datagrids to Excel spreadsheets through the browser. The code works great until the number of rows in the datagrid gets large (approx. 5000),...
3
by: Scott | last post by:
I want to search 5 or 6 very big excel spreadsheets looking for specific data held in them. The search would be using clients surnames and maybe postcodes to eventually find the correct person. ...
3
by: Bilgehan.Balban | last post by:
Hi, My observation was that a function with `inline' qualifier has file scope in C++ and it's symbol is not exported. Contrary to this, in C an `inline' function symbol is exported, unless it...
21
by: bobh | last post by:
Hi All, In Access97 I have a table that's greater than 65k records and I'm looking for a VBA way to export the records to Excel. Anyone have vba code to export from access to excel and have the...
15
by: Grey Alien | last post by:
I have a class that contains a std::map variable. I need to export the class via a DLL. the class looks something like this: class MyClass { public: MyClass(); MyClass(const MyClass&); ...
2
by: jjwiet | last post by:
Hello, I use access 2003 and attempting to export/copy records between two access databases (almost identical) with multiple tables (both databases having the same relations between the tables)....
2
by: BlackEyedPea | last post by:
Hi I have no coding experience but am using access 2003 on XP in the hope that I can find some code that will.... Search a folder in my network & import any excel spreadsheets it finds within...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.