Results 1 to 2 of 2
  1. #1
    Elbows is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Oct 2011
    Posts
    8

    Exporting and saving file as [FORM]![FORM1]![FILENAME]

    Is there anyway of writing a Macro that exports the results of a query and saves the file as an xls file but the file name is based on what is written in a box on a form (or alternatively the date would be just as useful)?



    ie

    OpenQuery
    ExportData to folder
    call the file (Now).xls

  2. #2
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2007
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Here is some VBA code. I don't use macros. It gives you the option of having two text boxes. One for the file name and the other to supply the path of where you want the file to go.

    Code:
    Private Sub Command5_Click()
    On Error GoTo Err_Command5_Click
    Dim reportName As String
    Dim theFilePath As String
    
    reportName = me.txtboxname.value
    
           
    theFilePath = Me.txtfilepath.Value
    theFilePath = theFilePath & reportName & "_" & Format(Date, "yyyy-mm-dd") & ".xls"
          
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, reportName, theFilePath, True
    
    MsgBox "Look in your destination folder for the file"
    
       
    
    Exit_Command5_Click:
        Exit Sub
    
    Err_Command5_Click:
        MsgBox Err.Description
        Resume Exit_Command5_Click
        
    End Sub
    It adds the date to the file name and also it was set up for Excel 2002, so you will have to change some code if you want it to be for later versions.

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

Similar Threads

  1. Exporting Report to a word file or PDF or whatever
    By AccessDatabaseGuy in forum Access
    Replies: 1
    Last Post: 05-03-2011, 02:03 PM
  2. File size smaller after saving
    By Pilotwings_64 in forum Access
    Replies: 1
    Last Post: 08-20-2010, 06:21 AM
  3. Exporting to Text File
    By blandow in forum Import/Export Data
    Replies: 2
    Last Post: 08-06-2010, 06:02 PM
  4. Replies: 2
    Last Post: 02-26-2010, 08:14 AM
  5. Exporting data to text file
    By NC_juggler in forum Import/Export Data
    Replies: 0
    Last Post: 11-21-2008, 10:51 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