Exporting from Access to Excel Run Time Error 31532 (1 Viewer)

mccoydo

New member
Local time
Today, 10:40
Joined
Apr 16, 2015
Messages
5
Hi there, I'm trying to export queries from access to excel using the DoCmd option. The code (see below) works to a point - it exports some of the queries before I get a run time error:

"31532: Microsoft Access was unable to export the data".

The worksheet tabs also do not pick up the query name but instead return what looks like a temporary ID (e.g. ~TMPCLP118431). Have tried different file locations and versions of excel but the same thing keeps happening. Any ideas?

Thanks in advance for your help.

Code:
 Sub ExportAllQueries()

Dim qdf As QueryDef

Dim db As Database

    Set db = CurrentDb
    
    For Each qdf In db.QueryDefs
        DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, qdf.Name, "C:\Users\mccoydo\Desktop\AllQueries.xls"
        Debug.Print qdf.Name
    Next
       
End Sub
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:40
Joined
Aug 30, 2003
Messages
36,124
There will be more than just your saved queries in that collection (rowsources, etc). I would test the name for starting with "qry" or whatever, to exclude the other items.
 

mccoydo

New member
Local time
Today, 10:40
Joined
Apr 16, 2015
Messages
5
That sorted it pbaldy. I put in an if statement before the DoCmd "If Left(qdf.Name, 3) = "qry" Then..." and that worked.

Thanks!
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 02:40
Joined
Aug 30, 2003
Messages
36,124
Happy to help!
 

Users who are viewing this thread

Top Bottom