Results 1 to 3 of 3
  1. #1
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136

    Exporting filtered form data

    Hi all,
    I have a form with a search box at the top that filters the records while you type. A user has requested an export of the filtered form data to Excel. I used the ExportWithFormatting Macro and it works fine, but it also exports the search box and the text that's typed into it. How can I do this via DBA instead?

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    you need a query with the criteria to export.
    build the criteria, then write it to an existing query, then export it.


    Code:
    Public Sub btnReport_Click()
    Dim sSql As String, sWhere As String
    Dim qdf As querydef
    Const kQRY = "qsFormFilter"    'here is the query we use ...in the report too
    
    sWhere = "1=1"
    
    
        'the query is built depending on the various filters the user picks...
    If Not IsNull(cboState) Then sWhere = sWhere & " and [state]='" & cboState & "'"
    If Not IsNull(txtName) Then sWhere = sWhere & " and [Name]='" & txtName & "'"
    If Not IsNull(chkContact) Then sWhere = sWhere & " and [Contact]=" & chkContact.Value
    
    
     'use it to filer the form records
    if sWhere = "1=1" then
      me.filterOn = false
    else
      me.filter = sWhere
      me.filterOn = true
    endif
    
    
    
        'or 'BUILD the query from the 'where'
    Set qdf = currentdb.querydefs(kQRY)
    qdf.Sql = "SELECT * FROM tblCompany WHERE " & sWhere
    qdf.Close
    
    
       'open the query or report here!
    DOCMD.openquery kQRY
    
    'export
    vFile = "c:\temp\MyData.xlsx"
    DoCmd.TransferSpreadsheet acExport,  acSpreadsheetTypeExcel12xls, kQRY, vFile, True,"qry1"
    
    end sub

  3. #3
    matey56 is offline Competent Performer
    Windows 10 Office 365
    Join Date
    Jul 2020
    Posts
    136
    Sorry, what do you mean by build criteria? I'm an extreme novice.

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

Similar Threads

  1. Replies: 3
    Last Post: 06-27-2017, 07:01 AM
  2. Exporting Filtered Data from subform
    By MTSPEER in forum Programming
    Replies: 2
    Last Post: 02-09-2016, 10:57 AM
  3. Exporting filtered results to Excel
    By doublec9 in forum Import/Export Data
    Replies: 2
    Last Post: 06-04-2013, 09:05 PM
  4. Report based of filtered data from a form
    By Fabdav in forum Reports
    Replies: 1
    Last Post: 09-15-2011, 05:28 AM
  5. Exporting Pivot Table to Excell - only want filtered records
    By Jennifer227 in forum Import/Export Data
    Replies: 1
    Last Post: 04-01-2011, 03:10 PM

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