Solved

Output query to Excel using query in VB instead of actually query

Posted on 2008-06-12
6
301 Views
Last Modified: 2008-06-13
I have the following code:

Dim sSQL As String

sSQL = "SELECT tblROP_ByWeek.* FROM tblROP_ByWeek WHERE (((tblROP_ByWeek.MonDate)=Forms!frmROP!StartDate)) ORDER BY tblROP_ByWeek.ClientName, tblROP_ByWeek.NewspaperName; "

DoCmd.OutputTo acOutputQuery, "sSQL", acFormatXLS, "C:\WINDOWS\Temp\ROPWeeklyData.xls", True

I want to output this query to Excel.  When I run it, I get error - can't find 'sSQL'.  What do I have to chage to get this to work?
0
Comment
Question by:Travidia
  • 5
6 Comments
 
LVL 28

Expert Comment

by:omgang
ID: 21773992
DoCmd.OutputTo acOutputQuery, sSQL, acFormatXLS, "C:\WINDOWS\Temp\ROPWeeklyData.xls", True
0
 
LVL 28

Expert Comment

by:omgang
ID: 21773998
sSQL is a variable.  You don't wrap variables in quotes.
OM Gang
0
 

Author Comment

by:Travidia
ID: 21775100
I tried taking the quotes off.  Still get error - can't find the object "Select tblROP_ByWeek...   Make sure the obkect exists...
0
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 28

Expert Comment

by:omgang
ID: 21779048
From VBA help
The OutputTo method carries out the OutputTo action in Visual Basic.

expression.OutputTo(ObjectType, ObjectName, OutputFormat, OutputFile, AutoStart, TemplateFile, Encoding)
expression    Required. An expression that returns a DoCmd object.

ObjectType   Required AcOutputObjectType. The type of object to output.

AcOutputObjectType can be one of these AcOutputObjectType constants.
acOutputDataAccessPage Not supported.
acOutputForm
acOutputFunction
acOutputModule
acOutputQuery
acOutputReport
acOutputServerView
acOutputStoredProcedure
acOutputTable

ObjectName   Optional Variant. A string expression that's the valid name of an object of the type selected by the ObjectType argument. If you want to output the active object, specify the object's type for the ObjectType argument and leave this argument blank. If you run Visual Basic code containing the OutputTo method in a library database, Microsoft Access looks for the object with this name, first in the library database, then in the current database.

OutputFormat   Optional Variant. The output format, expressed as an AcFormat constant. If you omit this argument, Microsoft Access prompts you for the output format.

AcFormat can be one of these AcFormat constants.
acFormatASP
acFormatDAP
acFormatHTML
acFormatIIS
acFormatRTF
acFormatSNP
acFormatTXT
acFormatXLS

OutputFile   Optional Variant. A string expression that's the full name, including the path, of the file you want to output the object to. If you leave this argument blank, Microsoft Access prompts you for an output file name.

AutoStart   Optional Variant. Use True (1) to start the appropriate Microsoft Windowsbased application immediately, with the file specified by the OutputFile    argument loaded. Use False (0) if you don't want to start the application. This argument is ignored for Microsoft Internet Information Server (.htx, .idc) files and Microsoft ActiveX Server (*.asp) files. If you leave this argument blank, the default (False) is assumed.

TemplateFile   Optional Variant. A string expression that's the full name, including the path, of the file you want to use as a template for an HTML, HTX, or ASP file.

Encoding   Optional Variant.


The OutputTo action requires a database object, i.e. you can't use an SQL statement explicitly.  You could create a QueryDef object, assign the SQL statement to the QueryDef object and then use that object in the Output to action.  I'll work up an example.
OM Gang
0
 
LVL 28

Accepted Solution

by:
omgang earned 250 total points
ID: 21779160
OM Gang
Public Function OutputToExcel()

On Error GoTo Err_OutputToExcel
 

    Dim qdf As DAO.QueryDef

    Dim sSQL As String, strQueryName As String

    Dim strDestFileName As String

    

    sSQL = "SELECT tblROP_ByWeek.* FROM tblROP_ByWeek WHERE (((tblROP_ByWeek.MonDate)=Forms!frmROP!StartDate)) ORDER BY tblROP_ByWeek.ClientName, tblROP_ByWeek.NewspaperName; "

    

    strQueryName = "qryOutput"

    

    strDestFileName = "c:\temp\MyOutputFile.xls"

    

    Set qdf = CurrentDb.QueryDefs(strQueryName)

    qdf.SQL = sSQL

    

    DoCmd.OutputTo acOutputQuery, strQueryName, acFormatXLS, strDestFileName

    

Exit_OutputToExcel:

    Set qdf = Nothing

    Exit Function

    

Err_OutputToExcel:

    MsgBox Err.Number & ", " & Err.Description, , "Error"

    Resume Exit_OutputToExcel
 

End Function

Open in new window

0
 
LVL 28

Expert Comment

by:omgang
ID: 21779183
Forgot to add - you need to create a query object named qryOutput.  It doesn't matter what the query is because you are going to reassign the SQL statement in the function.  It just needs to exist so the function can utilize it for your OutputTo action.
OM Gang
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In Debugging – Part 1, you learned the basics of the debugging process. You learned how to avoid bugs, as well as how to utilize the Immediate window in the debugging process. This article takes things to the next level by showing you how you can us…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now