Results 1 to 6 of 6
  1. #1
    ryanmce92 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    48

    Search form - Runs a query - Export to Excel

    Hi All,



    I have a form which allows the end user to search for a certain supplier using certain fields e.g Supplier Name, Country of Manufacture etc. This form has a button on it which runs a query and brings up the results of their search in the datasheet view.

    Rather than have the user get the results in the datasheet view I was wondering if there was a way to have an export results to excel button?. My users are kind of illiterate when it comes to I.T so I am just trying to make things as simple as possible for them.

    Any help would be great!!.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    the export to excel code is:

    ' send the query and the sheet name
    'usage : Export2XL("qsMyQuery","MyData")


    Code:
    Public Sub Export2XL(ByVal pvQry, Optional pvCaption)
    Dim vDir, vTime, vNam, vFile
    
    If IsMissing(pvCaption) Then pvCaption = pvQry   
    
    vDir = txtPath = Environ(“UserProfile”) & "My Documents"     'get the users mydoc path or fill in a network path to save the file
    vNam = pvQry 
    
    vFile = vDir & vNam & ".xls"
    DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, pvQry, vFile, True, pvCaption
    
    End Sub

  3. #3
    ryanmce92 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    48
    How would I go about assigning this to a button though?.

  4. #4
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi ryanmce92
    this is the code I have that runs on the on Click event of a button that exports a query to excel

    the button is called "ExportAssets"

    the code is below

    Private Sub ExportAssets_Click()
    On Error GoTo ExportAssets_Click_Error
    DoCmd.OutputTo acOutputQuery, "Asset Details Exported From Asset Database", acFormatXLS, DLookup("FilePath", "Settings", "ID = 1") & "\IT" & "\Reports" & "\Excel" & "\Exported Asset Details" & "\Exported Asset Details - " & Me.TodaysTimeAndDate & ".xls"
    Application.FollowHyperlink (DLookup("FilePath", "Settings", "ID = 1")) & "\IT" & "\Reports" & "\Excel" & "\Exported Asset Details" & "\Exported Asset Details - " & Me.TodaysTimeAndDate & ".xls"
    On Error GoTo 0
    Exit Sub
    ExportAssets_Click_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ExportAssets_Click of VBA Document Form_Dashboard"
    End Sub

    this outputs the query as an excel file to the folder located on our network in this case a mapped drive mapped as P

    p:\IT\Reports\Excel\Exported Asset Details

    the file name is then called Exported Asset Details - **with todays time and date***

    it then opens the excel sheet by using application.followhyperlink line of code

    you could adapt this to suit your needs

    hope this helps

    Steve

  5. #5
    ryanmce92 is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2015
    Posts
    48
    Hi Steve,

    Thanks for all this info, I'm sure it will help, I managed to do it myself yesterday by creating a macro and using export with formatting and the message box tools. I should have closed this post but was too happy with myself for getting it correct.

    Thanks everyone for your help!!.

  6. #6
    sdel_nevo is offline Competent Performer
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2013
    Location
    Gloucester, UK
    Posts
    402
    Hi ryanmce92

    no problem

    take care

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

Similar Threads

  1. Replies: 2
    Last Post: 03-28-2014, 11:25 AM
  2. Replies: 1
    Last Post: 03-11-2014, 09:29 AM
  3. Export Filtered Query to Excel Using Combo Box on Form
    By besuchanko in forum Import/Export Data
    Replies: 0
    Last Post: 02-07-2013, 10:10 PM
  4. Access Query export to Excel form
    By Odogggoneit in forum Access
    Replies: 1
    Last Post: 01-14-2013, 05:16 PM
  5. Form that runs query and creates reports
    By Valeda in forum Forms
    Replies: 2
    Last Post: 05-04-2006, 07:01 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