Solved

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

Posted on 2008-06-12
6
286 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
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

706 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

19 Experts available now in Live!

Get 1:1 Help Now