473,769 Members | 5,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Export a report to excel that uses a query and additional filters.

3 New Member
I have a report that opens up using a pretty complex query. I then pop up a form with combo boxes so the user can apply a filter to the existing report and then refresh the report. I need to export the report's records with the applied query to excel in datasheet form and it needs to contain the additional filters that the user selects. The reports datasource is the original query.

Right now I have this and get the datasheet format, but I need the filters that were passed to the report - how can I export the query with the additional filters?

'Export the query as a datasheet in excel - missing filters from user
DoCmd.OutputTo acOutputQuery, "BOM_QTY_Status _qry", acFormatXLS, strFileName, True

Thanks
Oct 27 '06 #1
6 6253
PEB
1,418 Recognized Expert Top Contributor
Hi,

Simply set up those filters directly in your query instaed of your reports

:)
Oct 27 '06 #2
pks00
280 Recognized Expert Contributor
Is it possible to export your report in excel?
I mean run your report in preview mode then analyze it with excel

e.g


DoCmd.OpenRepor t "MyReport", acViewPreview
DoCmd.RunComman d acCmdOutputToEx cel
Oct 30 '06 #3
NeoPa
32,573 Recognized Expert Moderator MVP
It is possible, but crap ;).

Not entirely, but the resultant spreadsheet has data in columns all over the place and is hard work to format into a useful document.

I am working on a similar job ATM and I decided to implement, in code, the application of the various filters into the underlying recordsource of the Report.

It doesn't use the data already run for the report.
I'll copy in the routine I'm using for ideas, but it uses functions you won't have available so it won't work for you directly. Even this one routine is quite large. I will however dig up the other stuff if requested - I don't wanna post a load of code here that no-one wants. I myself am always very cautious about using code developed elsewhere.

Expand|Select|Wrap|Line Numbers
  1. 'ExportToExcel exports recordset {strRS} to {strFolder}\{strName}.Xls
  2. 'Returns the eventual file name used
  3. Public Function ExportToExcel(strName As String, _
  4.                               Optional ByVal strRS As String = "", _
  5.                               Optional ByVal strWhere As String = "", _
  6.                               Optional ByVal strFolder As String = "") As String
  7.     Dim strOut As String, strFile As String, strType As String
  8.     Dim objRS As Object
  9.  
  10.     'If no RecordSet supplied assume "tbl" & strName
  11.     If strRS = "" Then strRS = "tbl" & strName
  12.     'Determine the DataSet type (strType) - (T)able; (Q)uery; (S)QL
  13.     On Error Resume Next
  14.     Set objRS = CurrentDb.TableDefs(strRS)
  15.     If Not objRS Is Nothing Then
  16.         strType = "T"
  17.     Else
  18.         Set objRS = CurrentDb.QueryDefs(strRS)
  19.         strType = IIf(objRS Is Nothing, "S", "Q")
  20.     End If
  21.     On Error GoTo 0
  22.     'Code to insert a WHERE clause into the SQL from strWhere
  23.     If strWhere > "" Then
  24.         strRS = ParamReplace("SELECT * FROM (%S)%A WHERE (%W)", _
  25.                              "%S", strRS, _
  26.                              "%A", IIf(strType = "S", " AS subQ", ""), _
  27.                              "%W", strWhere)
  28.         strType = "S"
  29.     End If
  30.     'If SQL string to be used - create temp query
  31.     If strType = "S" Then _
  32.         Set objRS = TempQuery(strName:=strName, strSQL:=strRS, blnCopy:=True)
  33.     'If no folder supplied, use the 'Temp' folder
  34.     If strFolder = "" Then strFolder = Environ("Temp")
  35.     'For uniformity, remove any trailing "\" from folder name
  36.     If Right(strFolder, 1) = "\" Then _
  37.         strFolder = Left(strFolder, Len(strFolder) - 1)
  38.     strOut = strFolder & "\" & strName & "New.Xls"
  39.     strFile = strFolder & "\" & strName & ".Xls"
  40.     'Delete temp file we will use, if it exists - if not, then not a problem
  41.     On Error Resume Next
  42.     Call Kill(strOut)
  43.     On Error GoTo 0
  44.     'Export to temp file - if this works then rename to required file
  45.     strName = objRS.Name
  46.     Set objRS = Nothing
  47.     Call DoCmd.TransferSpreadsheet(TransferType:=acExport, _
  48.                                    TableName:=strName, _
  49.                                    FileName:=strOut)
  50.     On Error Resume Next
  51.     Call Kill(strFile)
  52.     On Error GoTo 0
  53.     Name strOut As strFile
  54.     If strType = "S" Then Call CurrentDb.QueryDefs.Delete(Name:=strName)
  55.     ExportToExcel = strFile
  56. End Function
Oct 30 '06 #4
smcdonald
3 New Member
To export a report to Excel is messy looking. Export a query instead, it exports in datasheet form. What I did was create a query that the report uses and a button on a form that says 'Export to Excel'. On the click event I call the query for the report being displayed and it has the command:

DoCmd.OutputTo acOutputQuery, "query_name_her e", acFormatXLS, "path_and_file_ name_here", True
Oct 31 '06 #5
NeoPa
32,573 Recognized Expert Moderator MVP
I'm doing a lot of work on exporting to Excel in my main database at work ATM.
I wasn't aware of that construct - I'll check into it tomorrow in case it's better than what I'm currently using (DoCmd.Transfer Spreadsheet).
Oct 31 '06 #6
hunterbowker
1 New Member
My name is hunter bowker.
came here through google.
this forum seems to be ranking high on google.
btw what is the most busiest time on this forum so that i could interact with peak members or is it ever this lonely.
Sep 21 '10 #7

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

Similar topics

1
17418
by: Steven Stewart | last post by:
I have a user who has been using Excel for a while to keep statistics and print reports. She finds using it cumbersome because of long formulas and a lot of copying and pasting. I have designed a database for her which is intended to make things a lot easier; however, I don't have a lot of experience with Access and I find that designing the reports in Access is tedious. I want to be able to print the reports (which are simply based on...
2
4699
by: PerryC | last post by:
Is there a way to export an MS Access Report to Word/Excel that looks EXACTLY like it appears in Access? When I export to Excel, only certain data appears, titles, headings... all missing. The format is not aligned... When export to Word, all my lines (table lines) are gone, making it hard to read. Thanks. Perry
14
6443
by: bonehead | last post by:
Greetings, I'm using the DoCmd.TransferText method to export the results of a MS Access query to a csv file. The csv will then be used to load an Oracle table. In other systems such as TOAD for Oracle, it's possible to force an additional comma delimiter after the last column, if the column is empty on a particular row. Oracle requires this additional comma on empty rightmost columns, for importing purposes.
13
13241
by: Hemant Sipahimalani | last post by:
The following piece of code is being used to export HTML to excel. HttpContext.Current.Response.ContentType = "application/vnd.ms-excel" HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=ABC.xls") HttpContext.Current.Response.Write(strHTML) HttpContext.Current.Response.End() However when the user tries to save it the Default File Type is Web Page(*.htm; *.html)
1
7208
by: JawzX01 | last post by:
Hello All, First, thank you for any help you can provide. I'm trying to do a simple export to excel. I've used the classic code that is all over the internet, and of course it worked without a problem. The problem is that I want to add text above the data grid in the excel sheet. The text already exists on the web page in various panels (there is different text depending on the parameters of the datagrid
3
9700
by: yovation | last post by:
Hi, I have a 3 table database. 1 parent 1 child 1 child of child I would like to get the data into excel as 1 sheet (similar to a grouped report).
2
6414
hemantbasva
by: hemantbasva | last post by:
Note We need to have a template on server for generating report in multiple sheet as we do not had msoffice on server moreover this require a batch job to delete excel file created by the method.... it creates 6 sheets # region Namespaces using System;
7
5154
by: barrhaven01 | last post by:
Hi there, I was wondering if anyone could suggest a method using only access ( ie no additional applications) to batch export reports to excel. Basically i have a report with a query for its source data, and the query is based on the selection of an employee from a drop down list. It would be terrific if i could batch export a report for each employee. Hopefully someone out there has the answer
1
2335
by: chandhseke | last post by:
Hi folks, I am a newbie to this functionality of creating excel report using VB/VB Script. I have been given a code and asked to add filters to the excel sheet that is created using the below code. Can somebody help me out in resloving the issue?? Thanks in advance.
0
9589
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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,...
1
9998
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
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
8876
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
7413
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
5448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.