troubleshooting Question

Export of query result to Excel with TransferSpreadsheet into existing sheet fails, but creates unwanted new sheet

Avatar of fritzke
fritzke asked on
Microsoft Access
7 Comments2 Solutions2721 ViewsLast Modified:
I am exporting a number of query results to an existing Excel workbook (see code Example below). The results should be placed in sheets which are named exactly like the queries which works in most cases. However, for a recently added query (say "myQuery") (and corresponding Sheet "myQuery") this does not work. Rather, a new sheet "myQuery1" is created and the query result of the query "myQuery" is placed there.

Since I have other sheets and formulas referencing to the sheet "myQuery", this is not acceptable for me. What could be the reason for this behaviour and how can I fix it?

Code Example:

        Dim queryname
        For Each seg In getSegments
            queryname = "query_" & seg
             Dim startMonth As String
            startMonth = getStartMonth(seg)
                    ' all other cases
                    CurrentDb.QueryDefs(queryname).sql = "SELECT  DateSerial([year],[month],1) AS [Date], MEMBER_RANKING.Segment, " & _
                    "MEMBER_RANKING.A_MEMBER_ID as Member, MEMBER_RANKING.volume as [Member volume],MEMBER_RANKING.rank as Rank, MEMBER_RANKING.Marketvolume as [Market volume]  " & _
                    "FROM MEMBER_RANKING WHERE (MEMBER_RANKING.A_MEMBER_ID='" & membi & "' and MEMBER_RANKING.segment = '" & seg & _
                    "') ORDER BY MEMBER_RANKING.year, MEMBER_RANKING.month;"
            Debug.Print mypath
            DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, queryname, mypath, True
        Next seg

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros