Results 1 to 2 of 2
  1. #1
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398

    Exporting a query to Excel

    Here's my code for exporting a query to Excel:


    DoCmd.TransferSpreadsheet acExport, 10, strQry, strFileName, True, ""

    This works fine. My computer saves the spreadsheet into the Documents Library.

    If I run this code a second time, it receives an error because the file exits. I'd like to change my code for exporting the query to the following:

    If Dir(strFileName) <> "" Then
    kill strFileName
    DoCmd.TransferSpreadsheet acExport, 10, strQry, strFileName, True, ""
    Else
    DoCmd.TransferSpreadsheet acExport, 10, strQry, strFileName, True, ""
    End If

    The changed code does not find the file in the Dir statement, but in the transfer statement, it errors out because the file exists.

    I'm assuming it's because the Dir statement is looking in the root directory, while the transfer statement is working within the documents library.

    What do I need to do to look within the documents library to see if the file exists?

  2. #2
    crowegreg is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Posts
    398
    I figured it out. Here's what works for me:

    If Dir(Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx") <> "" Then
    Kill Environ$("USERPROFILE") & "\documents\" & strFileName & ".xlsx"
    DoCmd.TransferSpreadsheet acExport, 10, strQry, strFileName, True, ""
    Else
    DoCmd.TransferSpreadsheet acExport, 10, strQry, strFileName, True, ""
    End If

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Exporting a query to Excel File
    By crowegreg in forum Import/Export Data
    Replies: 2
    Last Post: 08-08-2013, 05:25 PM
  2. Exporting query to Excel. Almost there
    By Wombat in forum Import/Export Data
    Replies: 13
    Last Post: 04-10-2012, 01:38 PM
  3. Exporting a Query to Excel
    By tcheck in forum Access
    Replies: 3
    Last Post: 07-22-2011, 05:00 PM
  4. Query Exporting to Excel
    By Coffee in forum Queries
    Replies: 2
    Last Post: 07-18-2011, 07:42 AM
  5. Exporting Query to Excel
    By jvera524 in forum Import/Export Data
    Replies: 0
    Last Post: 12-06-2010, 09:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums