Results 1 to 8 of 8
  1. #1
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727

    export query

    All using ms 2010. I have a form with unbound fields to filter to view report. The form uses a query and the report uses only a few fields from the query. This works fine. Now I want to be able to export the query with all the fields based on the filter from the form to excel. All I have been able to export is the fields from the report. Can someone help me export all the fields in the query but only the records from the filtered form?

    Heres the code I used for the report:

    Code:
    Private Sub cmdExport_Click()
    On Error GoTo Err_cmdExport_Click
    Dim strSQL As String, intCounter As Integer
    Dim db As Database, rs As Recordset
    Dim ctl As Control, strname As String, strnewquery As String
    Dim stDocName As String
    Dim strRptSel As String
    Dim stMessage As String
    Set db = CurrentDb
    
          'Build SQL String
          For Each ctl In Me.Form
          If ctl.Tag = "input" Then
          strname = "me." & ctl.Name
          If ctl.Value > "" Then
          strSQL = strSQL & "[" & ctl.Name & "] " & " like " & Chr(34) & ctl.Value & Chr(34) & " And "
          End If
          End If
         Next ctl
          ' Set the value of the parameter.
        
        If Me.cboWEdateFrom & vbNullString <> "" And Me.cboWEdateTo & vbNullString <> "" Then
            strSQL = strSQL & ("[Week Ending] BETWEEN #" & cboWEdateFrom & "# And #" & cboWEdateTo & "# And " & "'")
       End If
       
       
       strnewquery = "Select qryQAReport.* FROM qryQAReport"
        If strSQL <> "" Then
            strSQL = Left(strSQL, (Len(strSQL) - 5))
            strnewquery = strnewquery & " WHERE " & strSQL & ";"
        End If
        Debug.Print strnewquery
        ' Create the recordset
        Set rs = db.OpenRecordset(strnewquery)
        If rs.RecordCount > 0 Then
         DoCmd.OpenReport "rptQAReport", acViewPreview, , strSQL
         DoCmd.Close acForm, "frmReportbuilder"
        Else
            MsgBox "There are no records that match your criteria! Please select new criteria.", vbInformation
            Exit Sub
        End If
    
    Exit_cmdExport_Click:
            Exit Sub
    Err_cmdExport_Click:
        Select Case Err.Number
            Case 2501 'OpenQuery action was cancelled
                Resume Exit_cmdExport_Click
            Case Else
                MsgBox Err.Description
                Resume Exit_cmdExport_Click
                Resume
            End Select
    End Sub
    It's from the cmdPreview and now I am calling it cmdExport so I can export the filtered data to the query in excel.


    Thanks

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Open a query or report that has all the desired fields included in its RecordSource.

    Query object can be filtered by referencing the form controls as criteria parameters.

    Report object can be filtered by the method you are already using in the posted code.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I am not following. Are you saying I need to change my search form's recordsource? Can I use the same search form? Can you show me an example to get me started? I thought I could use the same form. Thanks for your reply

  4. #4
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Ok. I think I got it. You mean reference the query filter in the qry criteria like:

    Code:
    [form]![frmReport]![txtJobTitle]
    When it opens the save dialog box comes up and saves the qry with nothing.. It also is prompt me for
    Code:
    [form]![frmReport]![TXT_AlphaName
    Am I not suppose to put this in all the criteria of the query?
    Thanks

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Almost, try:

    Forms!frmReport!txtJobTitle

    Review http://datapigtechnologies.com/flash...earchform.html
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    I did everything the video showed me and I still get prompted for the criteria. What can be wrong? This is the way the author suggested I put in the query criteria field of each searchfield.

    Code:
    Like [Forms]![frmReportbuilder]![Dept] & "*"

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    The technique works when applied properly. I don't know your db. Is Dept the name of textbox on form?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  8. #8
    slimjen is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Posts
    727
    Yes it is. I don't know what is the problem. I'll try again.

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

Similar Threads

  1. Query Result Export
    By worldwidewall in forum Access
    Replies: 6
    Last Post: 02-08-2012, 09:05 PM
  2. Export query to Excel
    By dev82 in forum Queries
    Replies: 15
    Last Post: 02-10-2011, 11:15 AM
  3. Export query design
    By somm in forum Access
    Replies: 2
    Last Post: 01-20-2011, 03:17 AM
  4. Export query to CSV
    By daniel.preda in forum Import/Export Data
    Replies: 3
    Last Post: 12-30-2010, 08:52 AM
  5. Export query without saving
    By prag in forum Queries
    Replies: 1
    Last Post: 11-18-2009, 06:22 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