473,606 Members | 3,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Open Excel One Workbook Multiple Sheets from MS Access

124 New Member
I am trying to generate one workbook with multiple worksheets from Access but so far I have not figured out how to do this. How does one go about taking 3 separate Access DAO recordsets and generate the same workbook with a separate worksheet per RS? I imagine it can be done, but am not sure how to go about doing it. By the way, the code for generating the Workbook with one individual worksheet is working fine. I would just like to generate all worksheets at once in the same workbook instead of just one sheet per each workbook.
Mar 20 '18 #1
11 2346
NeoPa
32,566 Recognized Expert Moderator MVP
There are many ways of doing it so posting your existing code would help us to give suggestions that match your approach. Otherwise we could post all sorts of stuff that would be perfectly correct, yet not very helpful for you.
Mar 21 '18 #2
twinnyfo
3,653 Recognized Expert Moderator Specialist
Or, in case you did not know that this existed, there is this basic code to add a worksheet to Excel:

Expand|Select|Wrap|Line Numbers
  1. ActiveWorkbook.Sheets.Add()
But, as NeoPa said, without knowing more about what your end result is to be, this would simply be a starting point.
Mar 21 '18 #3
BikeToWorkHome
2 New Member
Here is the code attempting to open each RS as a separate sheet on the same workbook (failing)

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAllReportsExcel_Click()
  2. Dim RS As DAO.Recordset
  3. Dim strSQL As String
  4. Dim strPath As String
  5. Dim strDB_Name As String
  6. Dim strExcelPath As String
  7. Dim strCaption As String
  8. Dim WKB_Path As String
  9. Dim DB As DAO.Database
  10. Set DB = CurrentDb
  11. Dim WKB As Excel.Workbook
  12. Dim XLApp As Excel.Application
  13.  
  14. 'Six Block Report**********************************
  15. strSQL = "qry_6Block_RPT_RS"
  16. Set RS = DB.OpenRecordset(strSQL)
  17. strDB_Name = DB.Name
  18. strDB_Name = Left(strDB_Name, InStrRev(strDB_Name, "\"))
  19. strExcelPath = strDB_Name & "ExcelTemplate\Funding.xlsx"
  20.  
  21. WKB_Path = Left(strDB_Name, InStrRev(strDB_Name, "\"))
  22. WKB_Path = WKB_Path & "ExcelData\"
  23. Set XLApp = New Excel.Application
  24.  
  25. Set WKB = XLApp.Workbooks.Add(strExcelPath)
  26. strCaption = "WAH"
  27.  
  28. '6 Block Report*************************************
  29. SixBlockReport RS, WKB, strCaption, WKB_Path, True
  30. Set DB = Nothing
  31. Set RS = Nothing
  32. Set WKB = Nothing
  33. '***************************************************
  34.  
  35. 'Funding Report*************************************
  36. strSQL = "qry_Kathleen_Rpt_RS"
  37. Set DB = CurrentDb
  38. Set RS = DB.OpenRecordset(strSQL)
  39.  
  40. strDB_Name = DB.Name
  41. strDB_Name = Left(strDB_Name, InStrRev(strDB_Name, "\"))
  42. strExcelPath = strDB_Name & "ExcelTemplate\Funding.xlsx"
  43. WKB_Path = Left(strDB_Name, InStrRev(strDB_Name, "\"))
  44. WKB_Path = WKB_Path & "ExcelData\"
  45. Set XLApp = New Excel.Application
  46. Set WKB = XLApp.Workbooks.Open(WKB_Path)
  47. strCaption = "WAH"
  48. Funding RS, WKB, strCaption, WKB_Path, True
  49. Set DB = Nothing
  50. Set RS = Nothing
  51. Set WKB = Nothing
  52.  
  53. '***************************************************
  54.  
  55. 'Running Rate Report********************************
  56. strSQL = "qry_RR_Rpt_RS"
  57. Set DB = CurrentDb
  58. Set RS = DB.OpenRecordset(strSQL)
  59.  
  60. strDB_Name = DB.Name
  61. strDB_Name = Left(strDB_Name, InStrRev(strDB_Name, "\"))
  62. strExcelPath = strDB_Name & "ExcelTemplate\Funding.xlsx"
  63.  
  64. WKB_Path = Left(strDB_Name, InStrRev(strDB_Name, "\"))
  65. WKB_Path = WKB_Path & "ExcelData\"
  66.  
  67. Set XLApp = New Excel.Application
  68.  
  69. Set WKB = XLApp.Workbooks.Open(WKB_Path)
  70. strCaption = "WAH"
  71.  
  72. Funding RS, WKB, strCaption, WKB_Path, True
  73. '***************************************************
  74. Set DB = Nothing
  75. Set RS = Nothing
  76. Set WKB = Nothing
  77. End Sub
  78.  
Here is an example of the code to print the data to a spreadsheet. The "writecellt ext" or "writecellmoney " functions just format the data for printing to the spreadsheet:

Expand|Select|Wrap|Line Numbers
  1. Sub Funding(ByRef RS As DAO.Recordset, WKB As Workbook, strCaption As String, WKB_Path As String, ALL As Boolean)
  2.  
  3. Dim WKS As Worksheet
  4. Dim lngRow As Long
  5. Dim rrow As Integer:      rrow = 2
  6. Dim actionFlag As String: actionFlag = ""
  7. Dim strSaveAsPath As String
  8.  
  9.     Set WKS = WKB.Worksheets("Funding")
  10.     'writeCellText wks, rrow - 2, 1, strCaption, actionFlag, "Center"
  11.  
  12.  
  13.     While Not RS.EOF()
  14.  
  15.         writeCellText WKS, rrow, 1, RS!TO2, actionFlag, "LEFT" 'TO2 A
  16.         writeCellText WKS, rrow, 2, RS!DCN, actionFlag, "LEFT" 'DCN B
  17.         writeCellText WKS, rrow, 3, RS!Status, actionFlag, "LEFT" 'STATUS C
  18.         writeCellText WKS, rrow, 4, RS!Active_Inactive, actionFlag, "LEFT" 'Active_Inactive D
  19.         writeCellText WKS, rrow, 5, RS!Division, actionFlag, "LEFT" 'DIVISION E
  20.         writeCellMoney WKS, rrow, 6, RS!Funds_Obligated, actionFlag 'Funds_Obligated F
  21.         writeCellMoney WKS, rrow, 7, RS!Total_Final_Invoiced, actionFlag 'Total_Final_Invoiced G
  22.         writeCellMoney WKS, rrow, 8, RS!Funds_Rem, actionFlag 'Funds_Rem H
  23.         writeCellPercent WKS, rrow, 9, RS!Percent_Spent, actionFlag 'Percent_Spent I
  24.         writeCellPercent WKS, rrow, 10, RS!Percent_Rem, actionFlag 'Percent_Rem J
  25.  
  26.         rrow = rrow + 1
  27.         RS.MoveNext
  28.     Wend
  29.     '*****************************************************
  30.  
  31.     WKS.PageSetup.LeftMargin = 0.5
  32.     WKS.PageSetup.RightMargin = 0.5
  33.     WKS.PageSetup.TopMargin = 0.75
  34.     WKS.PageSetup.BottomMargin = 0.5
  35.  
  36.     WKS.PageSetup.Orientation = xlLandscape
  37.     WKS.PageSetup.PrintArea = "$A$1:$L$" & CStr(rrow + 1)
  38.     'New code**************************
  39.     WKS.PageSetup.Zoom = False
  40.     WKS.PageSetup.FitToPagesWide = 1
  41.     WKS.PageSetup.FitToPagesTall = 100
  42.     '**********************************
  43.  
  44.     strSaveAsPath = WKB_Path
  45.     strSaveAsPath = Left(strSaveAsPath, InStrRev(strSaveAsPath, "\"))
  46.     strSaveAsPath = strSaveAsPath & "Funding_" & Format(Date, "mm-dd-yy") & "_" & Format(Time, "HH-MM") & ".xlsx"
  47.  
  48.     If ALL = False Then
  49.         WKB.SaveCopyAs strSaveAsPath
  50.         OpenExcel strSaveAsPath
  51.         WKB.Close SaveChanges:=False
  52.     End If
  53.  
  54.  
  55. '    Set WKB = Nothing
  56. '    Set RS = Nothing
  57. '    Set WKS = Nothing
  58.  
  59. End Sub
  60.  
Please help. I need to generate each RS to a the same workbook on separate sheets. Thank you in advance for advice.
Mar 21 '18 #4
twinnyfo
3,653 Recognized Expert Moderator Specialist
BikeToWorkHome,

I think what you are asking is very possible, and I don't think too complex. I think one of the issues is that you keep instantiating a "New" Excel Applicaiton (Lines 45, 68). Instead of a New Excel Applicaiton, try using
Expand|Select|Wrap|Line Numbers
  1. ActiveWorkbook.Sheets.Add()
Additionally, rather than writing each line individually, which can take a long time if you have many records, you can declare a Range, and then use .CopyFromRecord set and place the results of the entire query into the SpreadSheet. Then, select the columns you need formatted differently and apply it after the fact.

I can provide additional details if needed, but at this point, I assume some general knowledge of VBA/automating Excel.

Also, not sure if OP and Post #3 are from the same person?
Mar 21 '18 #5
NeoPa
32,566 Recognized Expert Moderator MVP
If you're doing this within Access Automation of Excel then one of the gotchas is that ActiveWorkbook won't work. The full reference in Excel is Application.Act iveWorkbook. In Excel the Application part is defaulted, but in Access it won't be. You'd need to specify it as a property of whatever variable you use to store your Excel Application object
Mar 21 '18 #6
twinnyfo
3,653 Recognized Expert Moderator Specialist
Thanks for the clarification. I had also made some assumptions...

;-)
Mar 21 '18 #7
NeoPa
32,566 Recognized Expert Moderator MVP
TwinnyFo:
Also, not sure if OP and Post #3 are from the same person?
I certainly hope so. BikeToWork is a long-time member and a recognised name. If anyone else were attempting to steal their identity that may be seen as a problem. As it happens, the email addresses used for the two accounts are similar enough to indicate the same person.

@BtW.
Perhaps you could PM me with your reasons for creating a separate account. We actually have a rule here that prohibits that - but that's for reasons that don't really apply to you. I'd be interested in knowing your reasons and could look into merging them into a single account if you wished.

NB. Please only reply as a PM to me.
Mar 21 '18 #8
BikeToWorkHome
2 New Member
Twinnyfo, BikeToWorkHome and BikeToWork are both me. I just created a new BikeToWorkHome account since I am at home and don't have the Bytes pwd from work. Thanks for your help. I already have a workbook created with formatted worksheets, one for each rs, where the data goes. If I print the worksheets one at a time, it works fine. When I try to print all three together it fails. I just need to transfer 3 Access recordsets to the same workbook on three separate worksheets in the workbook. I don't really need to add sheets since there are already existing sheets in the workbook. I don't really need to add sheets, but I can see where creating the Excel.Applicati on three times is a problem. If you could give me some simple example of exporting three Access recordsets to the same workbook on three existing spreadsheets in that workbook I can probably figure it out from there. Thanks again. NeoPa?
Mar 21 '18 #9
twinnyfo
3,653 Recognized Expert Moderator Specialist
Too easy!

Expand|Select|Wrap|Line Numbers
  1. Dim wsSheet As Excel.Worksheet
  2.  
  3. Set wsSheet = WKB.Sheets("YourWorksheetName")
  4. wsSheet.Activate
  5. wsSheet.Cells(1,1) = "Hello BikeToWorkHome!"
Of course, assuming this is tied into all your other code.
Mar 21 '18 #10

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

Similar topics

6
25250
by: Geert-Pieter Hof | last post by:
Hello, My VB 6.0 application read and writes data from and to a MS Excel workbook, using the Microsoft.Jet.OLEDB.4.0 provider. Now I want to protect the Excel workbook with a password, but I figured out that it is not possible to open the workbook for data access with ADO (http://support.microsoft.com/?KBID=211378). Is there another way to use a password protected Excel workbook in my
2
2119
by: Sheshadrinath R | last post by:
Hello, I want to open an excel workbook, which is present in the server. I want to open this excel in the browser using ASP.NET. May I know, how can I do this? Will the File.Open() command work here? Please send me, any code snippet if present. --With Regards,
2
5957
by: k-w | last post by:
Hi all In vb I do: Dim Ex As New Excel.Application Ex.Workbooks.Open "c:\tmp\Book1.xls" Similar code in VC doesn't work:
0
1944
by: shaurya.rastogi | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field containing the Excel File into a Byte Array and pass it to the Excel object in C#,as the file is wrapped in the OLE Wrapper used by Access while inserting the file in database. I have tried locating the Header of Excel file from the byte array and...
0
1375
by: shaurya | last post by:
I need to open an Excel file that has been stored in the Access Database using the insert Object functionality of MSAccess manually. What i am aware of is that i cant just read the field containing the Excel File into a Byte Array and pass it to the Excel object in C#,as the file is wrapped in the OLE Wrapper used by Access while inserting the file in database . I have tried locating the Header of Excel file from the byte array and read...
0
1571
by: jagadishv | last post by:
Hi, I want to export a multiple datasets into a single excel file with multiple spread sheets.I am using C#.net please help me. thanks in advance
2
4036
by: BankGirl | last post by:
hi i am trying to open a excel spreadsheet automatically at the push of a button. i am using the below code that i obtained via another help message but i keep getting an error message saying 'runtime error 5, invalid procedure or call arguement??? can someone help me out? Private Sub cmdOpenExcelFile_Click() Dim stAppName As String stAppName = "h:\sue\bdrm\FBT Fleet\access\FBTDataFleetCollection.xls" Call Shell(stAppName, 1)
2
1683
by: NEHU | last post by:
i m using ms office excel 2007 version how do i open it in database ms acess earlier i used excel5.0 ,,now what?
1
1825
by: attraankit | last post by:
hi, i am just a newbie in asp world.please tell me how to open an excel workbook using asp and also please tell me what should be the sequence of code while editing html scripts for including asp codes inbetween . thanks..
0
1637
by: vinodpaladi | last post by:
how to export the reports data from ms access to excel in multiple sheets
0
7978
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8461
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8448
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8317
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6796
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5470
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4010
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1572
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.