Solved

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

Posted on 2008-06-12
6
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Back Up Your Microsoft Windows Server®

Back up 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

688 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