Solved

Access export to excel with VBA

Posted on 2013-11-20
4
452 Views
Last Modified: 2013-11-20
Hello, I want to use the following code to export a query to excel.

With the code I do not get an error, but the file is not created.

Additionally, it would be great if the path where I save the excel file is the current users desktop.

Private Sub Command98_Click()
Dim dbs As DAO.Database
 Dim qdfTemp As DAO.QueryDef
 Dim strSQL As String, strQDF As String
 Set dbs = CurrentDb

 strSQL = "SELECT [DMR Tracking].[DMR#], [DMR Tracking].[DATE ISSUED], [DMR Tracking].[HOLD/SORT], [DMR Tracking].SCRAP," _
 & "[DMR Tracking].RTV, [DMR Tracking].[RMA FROM SUPPLIER], [DMR Tracking].[COMMENT 1], [DMR Tracking].[COMMENT 2]," _
 & "[DMR Tracking].[DMR DATE CLOSED], [DMR Tracking].VOID, [DMR Tracking].ShipperId FROM [DMR Tracking];"

strQDF = "_TempQuery_"
 Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
 qdfTemp.Close
 Set qdfTemp = Nothing

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
       strQDF, "C:\MyFileName.xls"

dbs.QueryDefs.Delete strQDF
 dbs.Close
 Set dbs = Nothing

End Sub

Open in new window

0
Comment
Question by:gogetsome
  • 2
  • 2
4 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39663278
Give this a try:

Private Sub Command98_Click()
Dim dbs As DAO.Database
 Dim qdfTemp As DAO.QueryDef
 Dim strSQL As String, strQDF As String
 Set dbs = CurrentDb

 strSQL = "SELECT [DMR Tracking].[DMR#], [DMR Tracking].[DATE ISSUED], [DMR Tracking].[HOLD/SORT], [DMR Tracking].SCRAP," _
 & "[DMR Tracking].RTV, [DMR Tracking].[RMA FROM SUPPLIER], [DMR Tracking].[COMMENT 1], [DMR Tracking].[COMMENT 2]," _
 & "[DMR Tracking].[DMR DATE CLOSED], [DMR Tracking].VOID, [DMR Tracking].ShipperId FROM [DMR Tracking];"

strQDF = "_TempQuery_"
 Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)
 qdfTemp.Close
 Set qdfTemp = Nothing

 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
       qdfTemp.Name , "C:\MyFileName.xls"

dbs.QueryDefs.Delete qdfTemp 
 dbs.Close
 Set dbs = Nothing

End Sub

Open in new window

0
 

Author Comment

by:gogetsome
ID: 39663308
Thank you once again for helping!

I'm getting a compile error: Type mismatch on this line:

dbs.QueryDefs.Delete qdfTemp
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39663364
Actually, your original code seems to work for me.  Try a folder other than C:\.  

This will write it to the desktop:
Dim dbs As DAO.Database
 Dim qdfTemp As DAO.QueryDef
 Dim strSQL As String, strQDF As String
 
 Dim objWSH As Object
 Set objWSH = CreateObject("WScript.Shell")
     
     'On Error GoTo ErrorHandler
     ' Create a shell object
 Set dbs = CurrentDb

 strSQL = "SELECT [DMR Tracking].[DMR#], [DMR Tracking].[DATE ISSUED], [DMR Tracking].[HOLD/SORT], [DMR Tracking].SCRAP," _
 & "[DMR Tracking].RTV, [DMR Tracking].[RMA FROM SUPPLIER], [DMR Tracking].[COMMENT 1], [DMR Tracking].[COMMENT 2]," _
 & "[DMR Tracking].[DMR DATE CLOSED], [DMR Tracking].VOID, [DMR Tracking].ShipperId FROM [DMR Tracking];"


strQDF = "TempQuery"
 Set qdfTemp = dbs.CreateQueryDef(strQDF, strSQL)


 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
       strQDF, objWSH.SpecialFolders("Desktop") & "\MyFileName.xls", True


 qdfTemp.Close
 Set qdfTemp = Nothing
set objWSH = nothing
dbs.QueryDefs.Delete strQDF

 dbs.Close
 Set dbs = Nothing

End Function

Open in new window

0
 

Author Closing Comment

by:gogetsome
ID: 39663414
Excellent! Thank you so much!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

839 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