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.
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.
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: - 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.
Here is the code attempting to open each RS as a separate sheet on the same workbook (failing) - Private Sub cmdAllReportsExcel_Click()
-
Dim RS As DAO.Recordset
-
Dim strSQL As String
-
Dim strPath As String
-
Dim strDB_Name As String
-
Dim strExcelPath As String
-
Dim strCaption As String
-
Dim WKB_Path As String
-
Dim DB As DAO.Database
-
Set DB = CurrentDb
-
Dim WKB As Excel.Workbook
-
Dim XLApp As Excel.Application
-
-
'Six Block Report**********************************
-
strSQL = "qry_6Block_RPT_RS"
-
Set RS = DB.OpenRecordset(strSQL)
-
strDB_Name = DB.Name
-
strDB_Name = Left(strDB_Name, InStrRev(strDB_Name, "\"))
-
strExcelPath = strDB_Name & "ExcelTemplate\Funding.xlsx"
-
-
WKB_Path = Left(strDB_Name, InStrRev(strDB_Name, "\"))
-
WKB_Path = WKB_Path & "ExcelData\"
-
Set XLApp = New Excel.Application
-
-
Set WKB = XLApp.Workbooks.Add(strExcelPath)
-
strCaption = "WAH"
-
-
'6 Block Report*************************************
-
SixBlockReport RS, WKB, strCaption, WKB_Path, True
-
Set DB = Nothing
-
Set RS = Nothing
-
Set WKB = Nothing
-
'***************************************************
-
-
'Funding Report*************************************
-
strSQL = "qry_Kathleen_Rpt_RS"
-
Set DB = CurrentDb
-
Set RS = DB.OpenRecordset(strSQL)
-
-
strDB_Name = DB.Name
-
strDB_Name = Left(strDB_Name, InStrRev(strDB_Name, "\"))
-
strExcelPath = strDB_Name & "ExcelTemplate\Funding.xlsx"
-
WKB_Path = Left(strDB_Name, InStrRev(strDB_Name, "\"))
-
WKB_Path = WKB_Path & "ExcelData\"
-
Set XLApp = New Excel.Application
-
Set WKB = XLApp.Workbooks.Open(WKB_Path)
-
strCaption = "WAH"
-
Funding RS, WKB, strCaption, WKB_Path, True
-
Set DB = Nothing
-
Set RS = Nothing
-
Set WKB = Nothing
-
-
-
'***************************************************
-
-
'Running Rate Report********************************
-
strSQL = "qry_RR_Rpt_RS"
-
Set DB = CurrentDb
-
Set RS = DB.OpenRecordset(strSQL)
-
-
strDB_Name = DB.Name
-
strDB_Name = Left(strDB_Name, InStrRev(strDB_Name, "\"))
-
strExcelPath = strDB_Name & "ExcelTemplate\Funding.xlsx"
-
-
WKB_Path = Left(strDB_Name, InStrRev(strDB_Name, "\"))
-
WKB_Path = WKB_Path & "ExcelData\"
-
-
Set XLApp = New Excel.Application
-
-
Set WKB = XLApp.Workbooks.Open(WKB_Path)
-
strCaption = "WAH"
-
-
Funding RS, WKB, strCaption, WKB_Path, True
-
'***************************************************
-
Set DB = Nothing
-
Set RS = Nothing
-
Set WKB = Nothing
-
End Sub
-
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: - Sub Funding(ByRef RS As DAO.Recordset, WKB As Workbook, strCaption As String, WKB_Path As String, ALL As Boolean)
-
-
Dim WKS As Worksheet
-
Dim lngRow As Long
-
Dim rrow As Integer: rrow = 2
-
Dim actionFlag As String: actionFlag = ""
-
Dim strSaveAsPath As String
-
-
Set WKS = WKB.Worksheets("Funding")
-
'writeCellText wks, rrow - 2, 1, strCaption, actionFlag, "Center"
-
-
-
While Not RS.EOF()
-
-
writeCellText WKS, rrow, 1, RS!TO2, actionFlag, "LEFT" 'TO2 A
-
writeCellText WKS, rrow, 2, RS!DCN, actionFlag, "LEFT" 'DCN B
-
writeCellText WKS, rrow, 3, RS!Status, actionFlag, "LEFT" 'STATUS C
-
writeCellText WKS, rrow, 4, RS!Active_Inactive, actionFlag, "LEFT" 'Active_Inactive D
-
writeCellText WKS, rrow, 5, RS!Division, actionFlag, "LEFT" 'DIVISION E
-
writeCellMoney WKS, rrow, 6, RS!Funds_Obligated, actionFlag 'Funds_Obligated F
-
writeCellMoney WKS, rrow, 7, RS!Total_Final_Invoiced, actionFlag 'Total_Final_Invoiced G
-
writeCellMoney WKS, rrow, 8, RS!Funds_Rem, actionFlag 'Funds_Rem H
-
writeCellPercent WKS, rrow, 9, RS!Percent_Spent, actionFlag 'Percent_Spent I
-
writeCellPercent WKS, rrow, 10, RS!Percent_Rem, actionFlag 'Percent_Rem J
-
-
rrow = rrow + 1
-
RS.MoveNext
-
Wend
-
'*****************************************************
-
-
WKS.PageSetup.LeftMargin = 0.5
-
WKS.PageSetup.RightMargin = 0.5
-
WKS.PageSetup.TopMargin = 0.75
-
WKS.PageSetup.BottomMargin = 0.5
-
-
WKS.PageSetup.Orientation = xlLandscape
-
WKS.PageSetup.PrintArea = "$A$1:$L$" & CStr(rrow + 1)
-
'New code**************************
-
WKS.PageSetup.Zoom = False
-
WKS.PageSetup.FitToPagesWide = 1
-
WKS.PageSetup.FitToPagesTall = 100
-
'**********************************
-
-
strSaveAsPath = WKB_Path
-
strSaveAsPath = Left(strSaveAsPath, InStrRev(strSaveAsPath, "\"))
-
strSaveAsPath = strSaveAsPath & "Funding_" & Format(Date, "mm-dd-yy") & "_" & Format(Time, "HH-MM") & ".xlsx"
-
-
If ALL = False Then
-
WKB.SaveCopyAs strSaveAsPath
-
OpenExcel strSaveAsPath
-
WKB.Close SaveChanges:=False
-
End If
-
-
-
' Set WKB = Nothing
-
' Set RS = Nothing
-
' Set WKS = Nothing
-
-
End Sub
-
Please help. I need to generate each RS to a the same workbook on separate sheets. Thank you in advance for advice.
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 - 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?
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
twinnyfo 3,653
Recognized Expert Moderator Specialist
Thanks for the clarification. I had also made some assumptions...
;-)
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.
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?
twinnyfo 3,653
Recognized Expert Moderator Specialist
Too easy! - Dim wsSheet As Excel.Worksheet
-
-
Set wsSheet = WKB.Sheets("YourWorksheetName")
-
wsSheet.Activate
-
wsSheet.Cells(1,1) = "Hello BikeToWorkHome!"
Of course, assuming this is tied into all your other code.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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,
|
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:
|
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...
|
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...
| |
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
|
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)
|
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?
|
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..
|
by: vinodpaladi |
last post by:
how to export the reports data from ms access to excel in multiple sheets
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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();...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |